Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder Taro's Avatar
    Join Date
    Oct 2011
    Location
    Geraldton, Ontario
    Posts
    155
    Thanks
    1
    Thanked 1 Time in 1 Post

    Java Method Shortcuts

    Hello,

    Is there a way to write the methods without having to repeat the entire original command? For example, can you say something like "repeat the above command 2 more times" but in Java? I have to write lisa.move(); multiple times; this is more specific than looping the main subclass.
    PHP Code:
    import com.jojoba.karel2.Display;
    import com.jojoba.karel2.Robot;

    public class 
    Lab01
    {
        public static 
    void main(String[] args)
        {
            
    Display.openWorld("maps/school.map");
            
    Display.setSize(1010);
            
    Display.setSpeed(5);
            
            
    Robot lisa = new Robot();
            
    Robot pete = new Robot(45Display.SOUTH0);
            
            
    lisa.move();
            
    lisa.move();
            
    lisa.turnLeft();
            
    lisa.move();
            
    lisa.turnLeft();
            
    lisa.move();
            
    lisa.pickBeeper();
            
    lisa.turnLeft();
            
    lisa.turnLeft();
            
    lisa.move();
            
    lisa.turnLeft();
            
    lisa.turnLeft();
            
    lisa.turnLeft();
            
    lisa.move();
            
    lisa.turnLeft();
            
    lisa.move();
            
    lisa.move();
            
    lisa.move();
        }

    *Note that the robot named Pete is not on the list, that is not part of my problem. Additionally, I'm working on developing a turnRight(); action, probably using an inherited event.
    Last edited by Taro; 09-15-2012 at 10:31 PM.
    Element ID

    Webs Support Helper

    Your friendly neighborhood Taroman.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yeah, you simply loop. If its something that may be used later within the code, I'd write it as a function, then simply loop and call that function x number of times. To loop thrice, you simply call the code in a for (int i = 0; i < 3; ++i) block.


  •  

    Tags for this Thread

    Posting Permissions

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