Results 1 to 6 of 6
  1. #1
    stormed is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Question Karel- how to make it loop

    Java Code:
    import stanford.karel.*;
    
    public class StoneMasonKarel extends SuperKarel {
    
    	public void run () {
    		fillStoneRow();
    		returnToBottom();
    		moveToNextRow();
    		returnHome();
    	}
    	
    	private void fillStoneRow() {
    		if (noBeepersPresent()) {
    			putBeeper();
    		}
    		turnLeft();
    		while (frontIsClear()) {
    			move();
    			if (noBeepersPresent()) {
    				putBeeper();
    			}
    		}
    		if (noBeepersPresent()) {
    				putBeeper();
    		}
    		}
    	
    	private void returnToBottom() {
    		turnAround();
    		while (frontIsClear()) {
    			move();
    		}
    	}
    	private void moveToNextRow() {
    		turnLeft();		
                     if (frontIsClear()) {
    			for (int i = 0; i < 4; i++)
    			move();
    		}
    	}
    	private void returnHome() {
    		while (frontIsBlocked()) {
     			while (facingEast()) {
    				turnRight();
    			}
    		}
    	}
    }
    Is there a reason why this code won't loop? Won't the code in public void 'run' automatically repeat until the preconditions are not being satisified for any more?

    I don't think I need to explain the actual problem requiring to be solved, so due to its irrelevancy I won't bother to explain. Can't really explain Karel to someone who doesn't know what it is.
    Last edited by stormed; 07-12-2012 at 03:41 AM.

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Karel- how to make it loop

    Quote Originally Posted by stormed View Post
    Won't the code in public void automatically repeat until the preconditions are not in-place for any more?
    Are you talking about your 'run' method? What pre-conditions are you referring to?

  3. #3
    stormed is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: Karel- how to make it loop

    Quote Originally Posted by awinston View Post
    Are you talking about your 'run' method? What pre-conditions are you referring to?
    Yes, sorry if it is hard to understand what I'm meaning. Still very new to coding. The pre-conditions I'm referring to are the requirements for an operation to be executed, like 'while (frontIsClear())' for example.
    Last edited by stormed; 07-12-2012 at 03:43 AM.

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Karel- how to make it loop

    It's fine. At the moment, your run method will execute each of your 4 other methods once. If you wish to repeat the execution of these methods, then you will have to add a while() statement into your run method like this:

    Java Code:
      public void run () {
            while(someCondition)
            {
                    fillStoneRow();
                    returnToBottom();
                    moveToNextRow();
                    returnHome();
            }
        }

  5. #5
    stormed is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: Karel- how to make it loop

    Quote Originally Posted by awinston View Post
    It's fine. At the moment, your run method will execute each of your 4 other methods once. If you wish to repeat the execution of these methods, then you will have to add a while() statement into your run method like this:

    Java Code:
      public void run () {
            while(someCondition)
            {
                    fillStoneRow();
                    returnToBottom();
                    moveToNextRow();
                    returnHome();
            }
        }
    Thank you very much kind sir. I'm alittle surprised because I originally tried this but only had it wrapped around fillStoneRow(); and that didn't cause a loop. Any particular reason why?

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Karel- how to make it loop

    Quote Originally Posted by stormed View Post
    Thank you very much kind sir. I'm alittle surprised because I originally tried this but only had it wrapped around fillStoneRow(); and that didn't cause a loop. Any particular reason why?
    I'm a bit confused with you saying that "it didn't cause a loop". If you have a while() statement, then you have a loop even if the body of the statement is empty and it doesn't do anything. If fillStoneRow() is the only content within the loop's body, then that is the only method that will be repeated until the condition is no longer met. Since I'm not sure what project you are working on, I can't tell you why it didn't work.

    I think that you should check out this tutorial before you move on: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Good luck!

Similar Threads

  1. Replies: 2
    Last Post: 06-04-2012, 12:20 AM
  2. Make a loop that prints the lastnumber only
    By ramin in forum New To Java
    Replies: 2
    Last Post: 04-06-2012, 04:11 AM
  3. Karel not Loading in Eclipse?
    By Discontent in forum Eclipse
    Replies: 3
    Last Post: 10-16-2011, 06:53 PM
  4. Karel the robot
    By vulerious in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 10:48 PM
  5. How can I make a loop, that writes a....
    By Torgero in forum New To Java
    Replies: 3
    Last Post: 03-10-2009, 05:45 PM

Posting Permissions

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