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.
    

Monday, January 20, 2014

Decided to learn some Computer Programming... Java so let us begin!

Ok so there are some online course from Stanford that  I saw when I had an Ipad and was looking through their ITunes University.

So the beginning of the year I decided to start it up and learn some new information... this is my First post dealing with my Coding Solution for the First Assignment!

First Assignment

The program Karel is a simple programming environment to teach problem solving.
the language and program are simple but still java so you learn a litte about java as you go.

Part 1 CollectNewspaperKarel:
Karel lives in a house and has to go out the door get the newspaper (beeper) and return to his starting position.

The code was pretty simple but they want you to think in breaking down into processes so that was a bit more difficult to do so here is my solution:

 import stanford.karel.*;

public class CollectNewspaperKarel extends SuperKarel {

    // You fill in this part
    public void run(){
        moveToPaper();
        move();
        pickBeeper();
        returnHome();
    }
// this method moves karel to the door to get paper
    // precondition Karel is in house facing east with wall north and west of him.
    private void moveToPaper() {
        moveToWall();
        turnRight();
        move();
        turnLeft();
    }
    // allows karel to move forward as long as there is no wall in front of him.
    private void moveToWall() {
        while (frontIsClear()) {
            move();
        }
    }
// returns Karel back to the point and state he started
// precondition facing east just outside the door where the newspaper beeper was.
    private void returnHome() {
        turnAround();
        move();
        turnRight();
        moveToWall();
        turnLeft();
        moveToWall();
        turnAround();
   
    }
}
Part 2  StoneMasonKarel:
I first had little karel fixing the columns one row at a time and found that only worked in two of the scenario worlds where the columns and ceilings were all the same level
So I had to change the methods to go up and down to account for the varied levels of ceilings since the columns are always 4 moves a part:
/*
 * File: StoneMasonKarel.java
 * --------------------------
 * The StoneMasonKarel subclass as it appears here does nothing.
 * When you finish writing it, it should solve the "repair the quad"
 * problem from Assignment 1.  In addition to editing the program,
 * you should be sure to edit this comment so that it no longer
 * indicates that the program does nothing.
 */

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

    // You fill in this part

    public void run() {
        while (frontIsClear()) {
            fillInColumnUp();
            descendColumn();
            moveToNextColumn();
        }
        fillInColumnUp();
        descendColumn();
    }
    // this method precondition is karel facing east at bottom of world
    // karel will check for empty beepers in the column and replace them
   
    private void fillInColumnUp() {
        turnLeft();
        if (noBeepersPresent()) {
            putBeeper();
        }
            while (frontIsClear()) {
                move();
                if (noBeepersPresent()) {
                    putBeeper();
                }
            }
    }
    // this method precondition is karel has ascended a column and is stuck at the ceiling facing north
    // karel will go back down the newly repaired Column.
   
    private void descendColumn() {
        turnAround();
        while (frontIsClear()) {
            move();
        }
        turnLeft();
    }
    // this method assumes that Karel has just finished going up and down a column and is at the bottom again
    // facing east again, he will make 4 moves to next standard column location.
   
    private void moveToNextColumn() {
        for(int i=0; i<4; i++) {
            move();
        }
    }
}


Part 3 CheckerboardKarel:
I have to admit this one took me a bit once I figure that east and west didn't do exactly the same thing depending on the world you loaded it finally got it.

Here is my solution:
import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {

    // You fill in this part
   
    public void run() {
        placeBeepersEast();
            while (leftIsClear()) {
                goUpOneRowFaceWest();
                placeBeepersWest();
                    if (rightIsClear()) {
                        goUpOneRowFaceEast();
                        placeBeepersEast();
                    } else {
                        turnAround();
                    }
            }       
    }
// fills everyother spot in a east west row with a beeper (bug on 7*7)
    //Karel is In home position Facing East
   
    private void placeBeepersEast() {
        putBeeper();
            while (frontIsClear()) {
                move();
                    if (frontIsClear()) {
                        move();
                        putBeeper();
                    }
            }
    }
   
    private void placeBeepersWest() {
            while (frontIsClear()) {
                putBeeper();
                move();
                    if (frontIsClear()) {
                        move();
                    }
            }
    }
   
    // moves karel up one east west row and turns him facing West
    // Precondition: Karel facing East facing wall
    private void goUpOneRowFaceWest() {
        if (noBeepersPresent()) {
            turnLeft();
            move();
            turnLeft();
        } else {
            turnLeft();
            move();
            turnLeft();
            if (frontIsClear()) {
                move();
            }
        }
    }
   
    // moves karel up one row and turns him facing East
    // Precondition: Karel facing West facing wall   
    private void goUpOneRowFaceEast() {
        turnRight();
        move();
        turnRight();
    }
}
it works in all the worlds for testing and I was stoked when it worked on the 1x8 world especially.

I still have to perfect Stone Mason Karel so back to the computer programing world of Karel!