GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Jul-2009, 16:37
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Newbie "if" Nested in "while" Question


I'm working on the same problem a couple other students have posted here.

It's a Karel program, drawing a checkerboard pattern. The trick is that it must work regardless of the dimensions of the world.

My program works perfectly when there are an odd number of rows, but doesn't when there are an even number.

After Karel completes the first odd row, I have a while loop that only continues to draw an even row if his rightIsClear. Within that while loop, following his drawing an even row, he will draw the next odd row, but only if his rightIsClear once again.

Draws like a dream and stops just fine when his world contains an odd number of rows. So the while loop is working fine.. But with even numbers of rows, he attempts to move on to an additional row even though "if (rightIsClear)" should return false.

Well, if anyone could tell me why this doesn't work, it would really help me learn how to use these conditional statements correctly. Many thanks to my virtual professors!

Here is the code:
JAVA Code:
/* This program creates a perfect checkerboard pattern, regardless of the world's dimensions.
 */

public class CheckerboardKarel extends SuperKarel {


public void run() {
		fillFirstRow();
		while (rightIsClear()) {
			moveToNextRow();
			fillEvenRow();
			if (rightIsClear()); {
				moveToNextRow();
				fillOddRow();
			}
		}
	}
	
	/* fillFirstRow fills every other square with a beeper,
	 * starting with the westernmost square.
	 * Precondition is Karel is on the westernmost square of a row, facing east.
	 * Postcondition is that Karel is on the westernmost square of his row, facing west.
	 */
	
	private void fillFirstRow() {
		putBeeper();
		while (frontIsClear()) {
			move();
			if (frontIsClear()) {
				move();
				putBeeper();
			}
		}
		turnAround();
		while (frontIsClear()) {
			move();
		}
	}
	
	/* moveToNextRow moves Karel up a row.  
	 * Precondition is that he must be in the westernmost square facing west after completing a row.
	 * Postcondition is that he ends up one row up in the westernmost square, facing east.
	 */
	
	private void moveToNextRow() {
		turnRight();
		move();
		turnRight();
	}
	
	/* fillEvenRow fills a beeper on every other corner starting one square east of the westernmost point.
	 * Precondition is Karel is on an even row at the westernmost point, facing east.
	 * Postcondition is Karel is back in the westernmost point, facing west.
	*/
	
	private void fillEvenRow() {
		while (frontIsClear()) {
			move();
			putBeeper();
			if (frontIsClear()) {
				move();
			}
		}
		turnAround();
		while (frontIsClear()) {
			move();
		}
	}
	
	/*fillOddRow fills a beeper on every other corner, starting with the westernmost corner of his row.
	 * Precondition is that he is on the westernmost corner of an odd row, facing east.
	 * Postcondition is that he is on back on the westernmost corner of that row, facing west.
	 */
	
	private void fillOddRow() {
		putBeeper();
		while (frontIsClear()) {
			move();
			if (frontIsClear()) {
				move();
				putBeeper();
			}
		}
		turnAround();
		while (frontIsClear()) {
			move();
		}
		
	}
}
  #2  
Old 08-Jul-2009, 06:27
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Newbie "if" Nested in "while" Question


I think the problem is a 'stray' semicolon:
JAVA Code:
	if (rightIsClear()); {  // <== HERE, remove the ;
(inside the while of the run() function)
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 08-Jul-2009, 13:04
ebot9000 ebot9000 is offline
New Member
 
Join Date: Jul 2009
Posts: 20
ebot9000 is on a distinguished road

Re: Newbie "if" Nested in "while" Question


Good eye! Works like a charm now!

Thank you so much.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question on saving files goose21 C Programming Language 3 09-Sep-2007 22:45
Help with nested for loops...please. sorry newbie keperry C++ Forum 14 16-Oct-2004 12:25
Grouping data from MySQL with PHP - Newbie question. giobbi MySQL / PHP Forum 12 27-Feb-2004 01:34
Newbie Question about Borland andrewcarraway C++ Forum 4 24-Jan-2004 12:17
Code problem (a newbie question) monkster87 C++ Forum 3 11-Aug-2003 13:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:57.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.