RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 5149 | Replies: 11
Reply
Join Date: Feb 2003
Posts: 35
Reputation: theQube is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 3
theQube theQube is offline Offline
Light Poster

Loop...without the loop

  #1  
Jun 24th, 2003
Hi. I'm in a Java class at harvard, and they gave us this assignment to create a function moveKiloMile(); , which moves an on-screen robot 1000 units. We can declare the function by using a function moveMile(); , which moves the robot 8 units. Below is the code that I wrote. I'm trying to find out how to do the kilomile function without using a loop, since the professor says that its relatively short.

class Mover extends UrRobot
{
	void turnAround()
	{
		turnLeft();
		turnLeft();
	}

	void moveMile()
	{
		move();
		move();
		move();
		move();
		move();
		move();
		move();
		move();
	}

	void moveKiloMile()
	{
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();		
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();		
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();		
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
		moveMile();
	}

	main
		{
			Mover Karel = new Mover (1, 1, East, 0);
			Karel.turnAround();
			Karel.turnAround();			
			moveMile();
			moveKiloMile();
		}
}
			

To download the program we created this in, visit http://www.fas.harvard.edu/~libs111 and click the "Karal" button on the left column.

Thanks!

Ian
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 11,067
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 33
Solved Threads: 118
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Loop...without the loop

  #2  
Jun 24th, 2003
Oh my goodness gracious! What I suggest is to possibly use a recursive function, since you can't use a loop. This calls a function multiple times until a condition is met. (You pass the parameters for the condition into the function). Tell me if this works:

void moveKiloMile(int i)
{
     if (i > 0)
     {
          moveMile();
          i--;
          moveKiloMile(i);
      }
}

However, in the main function, you will have to call moveKiloMile(1000) to indicate you want to run moveMile 1000 times. Otherwise, you could do moveKiloMile(5) to run moveMile 5 times, and, well, you get the idea. Other than that, it works!
Dani the Computer Science Gal
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 11,067
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 33
Solved Threads: 118
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Loop...without the loop

  #3  
Jun 24th, 2003
Just noticed you wrote this isn't in Java, it's in JKarel. I downloaded the JKarel Users Manual you provided a link to, and it says to do this:

loop(1000)
{
     moveMile();
}

or, alternatively

int i = 0;
while (i < 1000)
{
     moveMile();
     i++;
}

Actually, I would assume if your teacher/professor said not to use a "loop" he was referring to the former and wants you to use "while" instead. However, in real programming, the former refers to a for loop while the later refers to a while loop.

In any case, I'm not sure if you can use the code for the "while" I've provided only because I don't see that JKarel supports variables of any kind - only calling functions.
Dani the Computer Science Gal
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 4
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Loop...without the loop

  #4  
Jun 24th, 2003
I don't see how someone can tell you to write something efficient without a loop (in this language anyways). Another way to do it besides recursion is to create a label and jump to it if your language supports it. (Kind of like Basic's GOTO.) Jumps are used more in lower level languages like assembly. This seems to be a higher level one, so a jump wouldn't be the best way but it would work.
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 11,067
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 33
Solved Threads: 118
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Loop...without the loop

  #5  
Jun 24th, 2003
According to what I saw of the how-to, no labels are supported either. Just functions and function calls!
Dani the Computer Science Gal
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 4
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Loop...without the loop

  #6  
Jun 24th, 2003
Then the teacher is whacked.
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 11,067
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 33
Solved Threads: 118
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Loop...without the loop

  #7  
Jun 24th, 2003
According to the how-to, which is basically meant as an "intro to the java language" there is something called loop(x) where x is an integer value, not a variable. Then there is something else called while(condition). When the teachers says don't use a loop, I think he/she means to use a while. According to the how-to, the "while" isn't associated with a "loop". See what I'm saying?
Dani the Computer Science Gal
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 4
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Loop...without the loop

  #8  
Jun 24th, 2003
Originally Posted by theQube
Hi. I'm in a Java class at harvard...

But a while is a loop. This is a Harvard teacher?
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Join Date: Feb 2003
Posts: 35
Reputation: theQube is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 3
theQube theQube is offline Offline
Light Poster

Re: Loop...without the loop

  #9  
Jun 24th, 2003
Yes, and quite a good one at that, so I hear. And they count "while's" as loops too. Additionally, there aren't any multiplication functions or variables that can be used.
Reply With Quote  
Join Date: Feb 2003
Posts: 35
Reputation: theQube is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 3
theQube theQube is offline Offline
Light Poster

Re: Loop...without the loop

  #10  
Jun 24th, 2003
I'm not actually looking for code, either. More of just the techique as to how to do it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 4:52 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC