Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-15-2012, 10:10 PM   PM User | #1
Taro
Regular Coder

 
Taro's Avatar
 
Join Date: Oct 2011
Location: Geraldton, Ontario
Posts: 155
Thanks: 1
Thanked 1 Time in 1 Post
Taro is an unknown quantity at this point
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.
__________________
Element ID

Webs Support Helper

Your friendly neighborhood Taroman.

Last edited by Taro; 09-15-2012 at 10:31 PM..
Taro is offline   Reply With Quote
Old 09-15-2012, 10:23 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
action, jojoba, robot

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:30 PM.


Advertisement
Log in to turn off these ads.