Problem 1

To start off with, I am using Eclipse and have attached my settings to get it to work with this assignment.

And now to actual run the code you need to include some things from the acm.jar file, since that is what is mainly being used to hide allot of the Java main implementations. So if you goto the the Package Explorer on the left hand side, and double click on the Package->default packages->CollectNewsPaper.java then to setup the environment for running this java file you need to goto Run->Run Configurations.. on the left options make sure that Java Applications -> New configuration is selected and then select the Project name as the Package and then tick the “include system libraries when searching for the main class” and also the “include inherited mains when searching for the main class”, then on the “main class” option, click search and find the CollectNewsPaper. Here is a image of how it should look.

Eclipse CS106A settings
Eclipse CS106A settings

Now, you should just see a robot (Karel) in the top left of the house and that is about it, we need to write the run() method for him to pick up the newspaper, so alter the code to the below.

/*
 * File: CollectNewspaperKarel.java
 * --------------------------------
 * At present, the CollectNewspaperKarel subclass does nothing.
 * Your job in the assignment is to add the necessary code to
 * instruct Karel to walk to the door of its house, pick up the
 * newspaper (represented by a beeper, of course), and then return
 * to its initial position in the upper left corner of the house.
 */
 
import stanford.karel.*;
 
public class CollectNewspaperKarel extends SuperKarel {
 
	private void getPaper()
	{
		move();
		turnRight();
		move();
		turnLeft();
		move();
		move();
	}
 
	private void pickUpPaper()
	{
		pickBeeper();
	}
 
	private void goBack()
	{
		// turn around basically
		turnRight();
		turnRight();
		move();
		move();
		move();
		turnRight();
		move();
		turnRight();
	}
 
	public void turnLeft()
	{
		turnRight();
		turnRight();
		turnRight();
	}
 
	// You fill in this part
	public void run()
	{
		getPaper();
 
		pickUpPaper();
 
		goBack();
	}
}

and then run again and if you click “start” he will go and pick up the newspaper and return back to where he started from.

I am finding this CS106A fun. In the above zip file there is the full source code for all of the parts to assignment 1, and also the assignment in a PDF file. But I shall place each part on a different post.

6 thoughts on “Problem 1”

  1. Nice article!

    Just one thing: when you inherit from SuperKarel, turnLeft() and turnAround() are already defined.

  2. on ubuntu linux with eclipse 1.5, I needed to add something else to the run configuration you described…
    on the “Arguments” tab manually type in “code=CollectNewspaperKarel.class” in the box called “Program arguments:”
    code=CollectNewspaperKarel.class for some reason…

    I discovered this fix through a youtube video
    http://www.youtube.com/watch?v=jWMW2ZfYPo4

  3. Mine:

    import stanford.karel.*;
     
    public class CollectNewspaperKarel extends SuperKarel {
     
    	public void run() {
    		moveToNewspaper();
    		pickUpNewspaper();
    		moveBackToStart();
    	}
     
    	private void moveToNewspaper() {
    		moveToWall();
    		turnRight();
    		move();
    		turnLeft();
    		move();
    	}
     
    	private void pickUpNewspaper() {
    		if(beepersPresent()) {
    			pickBeeper();
    		}
    	}
     
    	private void moveBackToStart() {
    		turnAround();
    		moveToWall();
    		turnRight();
    		move();
    		turnRight();
    	}
     
    	private void moveToWall() {
    		while(frontIsClear()) {
    			move();
    		}
    	}
    }
  4.  
    public void run(){
    		goOutHouse();
    		if (beepersPresent()){
    			pickBeeper();
    		}
    			goBackHouse();
     
    	}
    	private void goOutHouse(){
    		turnRight();
    		move();
    		turnLeft();
    		move();
    		move();
    		move();
    	}
    	private void goBackHouse (){
    		turnAround();
    		while (frontIsClear()){
    			move();
    			}
    			turnRight();
    			while (frontIsClear()){
    				move();
    				}	
    	turnRight();
     
    	}
  5. /*
     * File: CollectNewspaperKarel.java
     * --------------------------------
     * At present, the CollectNewspaperKarel subclass does nothing.
     * Your job in the assignment is to add the necessary code to
     * instruct Karel to walk to the door of its house, pick up the
     * newspaper (represented by a beeper, of course), and then return
     * to its initial position in the upper left corner of the house.
     */
     
    import stanford.karel.*;
     
    public class CollectNewspaperKarel extends SuperKarel {
     
     
    public void run() {
    	findDoor();
    	getOut();
        pickNewspaper();
    	goBackToInitialPosition();
    }
    /** This makes Karel find the door
     *  Start condition: Upper left corner of the house, facing East
     *  End condition: One corner West to the door, facing South.
     */
    private void findDoor(){
    	while (frontIsClear()&&leftIsBlocked()){
    		move();
    		if (frontIsBlocked()){
    			turnRight();
    		}
     
    	}
    }
     
    /**	This makes Karel get out of the house
     *  Start condition: Same as the end condition of the previous method; findDoor()
     *  End condition: One corner East to the door, facing East.
     */
    private void getOut() {
    	turnLeft();
    	while (noBeepersPresent()) {
    		move();
    	}
    }
     
    /**	This makes Karel pick up the newspaper
     *	Start condition: Same as the end condition of the previous method; getOut()
     *	End condition: No change from the start condition
     */
    private void pickNewspaper() {
    	if(beepersPresent()){
    		pickBeeper();
    	}
    }
     
    /** This makes Karel go back to where it started
     *  Start condition: Same as the end condition of the previous methods; getOut() and pickNewspaper()
     *  End condition: Upper left corner of the house, facing East (The initial position)
     */
    private void goBackToInitialPosition(){
    	turnAround();
    	for (int i=0; i<2; i++){	
    		while (frontIsClear()){
    			move();
    		}
    		turnRight();
    	}
    }
     
    }

Leave a Reply

Your email address will not be published.