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!