Wednesday, February 26, 2014

More CS106 Learning :)

And this is the code which makes the Target Draw in Java!

import acm.graphics.*;
import java.awt.*;
import acm.program.*;


public class TargetProgram extends GraphicsProgram {

    public void run() {
        drawCircle1();
        drawCircle2();
        drawCircle3();
    }

    private void drawCircle1() {
        GOval c1 = new GOval(240, 80, 215, 215);
        c1.setFilled(true);
        c1.setColor(Color.RED);
        add(c1);
    }
   
    private void drawCircle2() {
        GOval c2 = new GOval(273, 113, 150, 150);
        c2.setFilled(true);
        c2.setColor(Color.WHITE);
        add(c2);
    }
   
    private void drawCircle3() {
        GOval c3 = new GOval(310, 150, 75, 75);
        c3.setFilled(true);
        c3.setColor(Color.RED);
        add(c3);
    }
       
}


Here is the RobotProgram and my robot!
/* RobotProgram.java creates a robot face from the ACM package graphics */

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class RobotProgram extends GraphicsProgram{

    public void run() {
        drawFace();
        drawEyes();
        drawNose();
        drawMouth();
    }
   
    private void drawFace() {
        GRect face = new GRect(220, 40, 250, 350);
        face.setFilled(true);
        face.setColor(Color.LIGHT_GRAY);
        add(face);
    }
   
    private void drawEyes() {
        GOval lEye = new GOval(260, 95, 50, 50);
        GOval rEye = new GOval(380, 95, 50, 50);
        lEye.setFilled(true);
        lEye.setColor(Color.ORANGE);
        rEye.setFilled(true);
        rEye.setColor(Color.ORANGE);
        add(lEye);
        add(rEye);
    }
   
    private void drawNose() {
        GRect nose = new GRect(335, 185, 20, 40);
        nose.setFilled(true);
        nose.setColor(Color.BLACK);
        add(nose);
    }
   
    private void drawMouth() {
        GRect mouth = new GRect(270, 280, 155, 50);
        mouth.setFilled(true);
        mouth.setColor(Color.WHITE);
        add(mouth);
    }
   
}
now on for some reading time to read chapter 3

Sunday, February 23, 2014

More Java Learning Chapter 2 exercises... 1-5 done :)

But first a CUTE picture of LOGAN got to hang with him today, trying to get him and puddi to be good friends so when the new baby comes I can watch him while Charles and Debbie are in the hospital.

Now for the Assignment to draw a house using the functions available in the acm.jar.

it should look like this:
import acm.graphics.*;
import acm.program.*;

public class HouseProgram extends GraphicsProgram{

    /**
     * @param args
     */
    public void run() {
       
        drawRoof();
        drawHouse();
        drawWindows();
        drawDoor();
    }

    private void drawRoof(){
        add(new GLine(80, 100, 235, 20));
        add(new GLine(235, 20, 380, 100));
    }
   
    private void drawHouse() {
        add(new GRect(80, 100, 300, 200));
    }
   
    private void drawWindows() {
        add(new GRect(105, 130, 70, 70));
        add(new GRect(285, 130, 70, 70));
    }
   
    private void drawDoor() {
        add(new GRect(195, 180, 70, 120));
        add(new GOval(245, 230, 10, 10));
    }
}

And then my Robot Face: the robot has orange eyes, a black nose and and a white mouth:

/* RobotProgram.java creates a robot face from the ACM package graphics */

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class RobotProgram extends GraphicsProgram{

    public void run() {
        drawFace();
        drawEyes();
        drawNose();
        drawMouth();
    }
   
    private void drawFace() {
        GRect face = new GRect(220, 40, 250, 350);
        face.setFilled(true);
        face.setColor(Color.LIGHT_GRAY);
        add(face);
    }
   
    private void drawEyes() {
        GOval lEye = new GOval(260, 95, 50, 50);
        GOval rEye = new GOval(380, 95, 50, 50);
        lEye.setFilled(true);
        lEye.setColor(Color.ORANGE);
        rEye.setFilled(true);
        rEye.setColor(Color.ORANGE);
        add(lEye);
        add(rEye);
    }
   
    private void drawNose() {
        GRect nose = new GRect(335, 185, 20, 40);
        nose.setFilled(true);
        nose.setColor(Color.BLACK);
        add(nose);
    }
   
    private void drawMouth() {
        GRect mouth = new GRect(270, 280, 155, 50);
        mouth.setFilled(true);
        mouth.setColor(Color.WHITE);
        add(mouth);
    }   
}

this is all just using the basic functions and making a guess and coordinates so a lot of draw and look type programming.. we will see how it changes when the class teaches to figure out the coordinates so it draws it without the guess work :)

Enjoy

Sunday, February 2, 2014

More Karel sovling problems in his little world... finding the middle...

Ok so if I had to do this in the real world with all syntax and methods available I would probably do something like:
int count = 1
findCount() {
while frontIsClear() {
move();
int count = count + 1
}
turnAround();

int steps = count/2

while steps != 0 {
move():
int steps = steps-1

putBeeper

however the rules of this assignment state that Count and int are not a part of the karel program so you have to do it with logic.

The simplest method I could think of was to fill in the row with Beepers
from farthest point in so go to wall put beeper turn around go to wall or Beeper  stop turn around and put beeper back and forth till Karel is standing in the middle spot
he moves forward one spot and starts picking up the beepers then when he hits a wall turns around and moves till he hits a beeper again (this one is the middle) he moves one more step and picksup the beepers till he hits a wall
then turns around and moves back to the middle by stopping on the beeper.

Final solution with some help with the && statement from Arnos Blog:

* File: MidpointFindingKarel.java
 * -------------------------------
 * When you finish writing it, the MidpointFindingKarel class should
 * leave a beeper on the corner closest to the center of 1st Street
 * (or either of the two central corners if 1st Street has an even
 * number of corners).  Karel can put down additional beepers as it
 * looks for the midpoint, but must pick them up again before it
 * stops.  The world may be of any size, but you are allowed to
 * assume that it is at least as tall as it is wide.
 */

import stanford.karel.*;

public class MidpointFindingKarel extends SuperKarel {

    // You fill in this part

    public void run() {
        findWalls();
        while (noBeepersPresent()){
            fillToMiddle();
        }
        pickUpExtraBeepers();
        keepMiddle();
        pickUpExtraBeepers();
           
    }
    // this method defines the far west and east of Karel's world. 
    // Precondition karel facing east
    // Postcondition karel will be one avenue to the west of farthest wall on the east facing west
    private void findWalls() {
        putBeeper();
        while (frontIsClear()){
            move();
        }
        putBeeper();
        turnAround();
        move();
        }
   
    // this will fill in the row east to west and back till Karel can't move due to beepers or wall
    private void fillToMiddle() {
        while (frontIsClear() && noBeepersPresent()) {
            move();
        }
        turnAround();
        move();
        putBeeper();
        move();
    }
   
    //Karel is standing on a beeper one avenue past the middle
    private void pickUpExtraBeepers() {
        while (frontIsClear()) {
            pickBeeper();
            move();
        }
        pickBeeper();
        turnAround();
        while (noBeepersPresent()) {
            move();
        }
    }
   
    //keepMiddle just moves karel one past the middle beeper so it is kept to the otherside of it.
    private void keepMiddle() {
        move();
    }
}

So that completes my first Assignment... now on the Lecture 4 and delving into actual java.