Help with simple program

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 21
Reputation: Tootlol is an unknown quantity at this point 
Solved Threads: 0
Tootlol's Avatar
Tootlol Tootlol is offline Offline
Newbie Poster

Help with simple program

 
0
  #1
Sep 10th, 2008
Basically, I am trying to solve a problem a textbook. The book requires me to use karel J robot to develop programming principles. So the problem is correctly the following code so that the robot will do this: attached



This is the code I am suppose to fix. Currently the robot moves in a straight direction and doesn't turn. I do not know why. Please give me some hints, any help could be appreciated.

  1. import cs1Lib.karel.Robot;
  2. import cs1Lib.karel.World;
  3.  
  4. public class PlantSquare extends Object
  5. {
  6. public static void main(String[] args)
  7. { World world = new World();
  8.  
  9. Robot karel = new Robot(12);
  10. world.addRobot(karel, 1, 1, World.EAST);
  11.  
  12. int side = 0;
  13. while (side < 4)
  14. {
  15.  
  16.  
  17. int step = 0;
  18. while (step < 3)
  19. {
  20.  
  21. karel.move();
  22. karel.putBeeper();
  23.  
  24. }
  25. karel.turnLeft();
  26. }
  27. karel.turnOff();
  28.  
  29. }
  30. }
Last edited by cscgal; Sep 11th, 2008 at 2:04 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1
Reputation: inprobable is an unknown quantity at this point 
Solved Threads: 0
inprobable inprobable is offline Offline
Newbie Poster

Re: Help with simple program

 
0
  #2
Sep 10th, 2008
Your while loops don't have any end to them. The first one would execute the second one, and since the second loop never ends, the code
  1. karel.turnLeft()
never gets used. Try making the loops end so the other code can start.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: Help with simple program

 
0
  #3
Sep 10th, 2008
I think its because you never increase the step integer so karel never stops the karel.move(); and karel.putBeeper(); routine. If the move(); method doesnt increase the step counter, then karel will never exit. Put something like step = step + 1; in the while loop to see if that fixes your problem.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC