Results 1 to 3 of 3
- 12-02-2010, 04:21 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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(); } } }
- 12-02-2010, 03:38 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 12
- Rep Power
- 0
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!
- 12-02-2010, 05:53 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
CS106A Stanford University
By Learning Java in forum New To JavaReplies: 116Last Post: 07-09-2011, 04:43 PM -
BigInteger Rational (CS106A-Stanford university)
By ccie007 in forum New To JavaReplies: 16Last Post: 10-01-2010, 07:16 PM -
Class exercise CS106A (Stanford university)
By ccie007 in forum New To JavaReplies: 2Last Post: 09-11-2010, 01:47 AM -
How to run java class in ECLIPSE(CS106A Stanford)
By ccie007 in forum New To JavaReplies: 17Last Post: 09-11-2010, 12:18 AM -
Java Checkerboad question CS106A Stanford univerity
By ccie007 in forum New To JavaReplies: 69Last Post: 05-25-2010, 12:35 PM


LinkBack URL
About LinkBacks

Bookmarks