Results 1 to 3 of 3
  1. #1
    chmza is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Stanford CS106A, my work- your thoughts?

    For anyone who is familiar with Karel and could give me some feedback on my coding I would be very grateful, I'm very new to programming so I could use any advice I can get.

    I started watching the lectures and doing the reading for this course online and I'm doing the first assignment right now. The first two problems were fairly simple but the third one took a bit of thought. The problem calls for writing a program that will leave a checkerboard pattern of beepers on any sized board, starting from 1st ave and 1st st. There are no walls within the board and the program has to work on both a single row, and single column.

    Here's a link to the assignment, scroll down to problem 3:
    http://www.stanford.edu/class/cs106a/handouts/07-assignment-1.pdf

    This is my solution and it works on all the boards that are provided to test on.
    Does this look fairly efficient? What could be improved?


    Java Code:
    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * CheckerboardKarel will draw a checkerboard pattern with beepers on
     * any given size open rectangle or square board when starting at
     * 1st avenue and 1st street.
     */
    
    import stanford.karel.*;
    
    public class CheckerboardKarel extends SuperKarel {
    	
    	// Main routine.
    	public void run() {
    		turnLeft();
    		while(facingNorth()) {
    			dropRowA();
    			if(facingNorth()) {
    				dropRowB();
    			}
    		}
    	}
    	
    	// dropRowA method draws rows with a beeper on the first avenue corners
    	private void dropRowA() {
    		turnRight();
    		dropChipsA();
    		turnAround();
    		comeBack();
    		turnRight();
    		moveUp();
    	}
    	
    	// dropRowB method creates rows with no beeper on the first avenue corners
    	private void dropRowB() {
    		turnRight();
    		dropChipsB();
    		turnAround();
    		comeBack();
    		turnRight();
    		moveUp();
    	}
    	
    	// the dropChipsA method places an initial chip down then will move two
    	// spaces if they are open and then drop a beeper.  If Karel encounters 
    	// a wall the method ends and the dropRowA method continues.
    	private void dropChipsA() {
    		putBeeper();
    		while(frontIsClear()) {
    			move();
    			if(frontIsClear()) {
    				move();
    				putBeeper();
    			}
    		}
    	}
    	
    	// the dropChipsB method moves one space if it is open, drops a beeper
    	// and then moves one more time if the space is open.  Like dropChipsA,
    	// this method will end if Karel encounters a wall and dropRowB will
    	// continue.
    	private void dropChipsB() {
    		while(frontIsClear()) {
    			move();
    			putBeeper();
    			if(frontIsClear()) {
    				move();
    			}
    		}
    	}
    	
    	// comeBack is the method to return back to it's rows' first avenue corner.
    	private void comeBack() {
    		while(frontIsClear()) {
    			move();
    		}
    	}
    	
    	// moveUp is the final method in both dropRowA and dropRowB. Karel checks to
    	// see if the space in front of him (to the north) is open.  If the space is
    	// open Karel will move and drop the next row of chips.  If the row above
    	// Karel is closed, he will turn East and end the loop, stopping the program.
    	private void moveUp() {
    		if(frontIsClear()) {
    			move();
    		} else {
    			turnRight();
    		}
    	}
    }

  2. #2
    erversteeg is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    I am not familiar with Karel, but I get the gist of it by reading the assignment. Your code looks great! My prof always says that good code should read like a story and yours does because of excellent method names. Nice work!

  3. #3
    chmza is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks for your input! The one thing I just learned that I should have added into my comments is pre and post conditions of each methods. I'll be sure to add those in for future work.

Similar Threads

  1. Java Checkerboad question CS106A Stanford univerity
    By ccie007 in forum New To Java
    Replies: 71
    Last Post: 09-20-2013, 04:02 AM
  2. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 04:43 PM
  3. BigInteger Rational (CS106A-Stanford university)
    By ccie007 in forum New To Java
    Replies: 16
    Last Post: 10-01-2010, 07:16 PM
  4. Class exercise CS106A (Stanford university)
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 09-11-2010, 01:47 AM
  5. How to run java class in ECLIPSE(CS106A Stanford)
    By ccie007 in forum New To Java
    Replies: 17
    Last Post: 09-11-2010, 12:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •