943,779 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 596
  • Java RSS
Sep 10th, 2008
0

Help with simple program

Expand Post »
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.

java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tootlol is offline Offline
21 posts
since Aug 2008
Sep 10th, 2008
0

Re: Help with simple program

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
Java Syntax (Toggle Plain Text)
  1. karel.turnLeft()
never gets used. Try making the loops end so the other code can start.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
inprobable is offline Offline
1 posts
since Oct 2006
Sep 10th, 2008
0

Re: Help with simple program

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.
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Static or not problem.
Next Thread in Java Forum Timeline: hibernate mapping classes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC