Loop...without the loop

Reply

Join Date: Feb 2003
Posts: 35
Reputation: theQube is an unknown quantity at this point 
Solved Threads: 2
theQube theQube is offline Offline
Light Poster

Loop...without the loop

 
0
  #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.

  1. class Mover extends UrRobot
  2. {
  3. void turnAround()
  4. {
  5. turnLeft();
  6. turnLeft();
  7. }
  8.  
  9. void moveMile()
  10. {
  11. move();
  12. move();
  13. move();
  14. move();
  15. move();
  16. move();
  17. move();
  18. move();
  19. }
  20.  
  21. void moveKiloMile()
  22. {
  23. moveMile();
  24. moveMile();
  25. moveMile();
  26. moveMile();
  27. moveMile();
  28. moveMile();
  29. moveMile();
  30. moveMile();
  31. moveMile();
  32. moveMile();
  33. moveMile();
  34. moveMile();
  35. moveMile();
  36. moveMile();
  37. moveMile();
  38. moveMile();
  39. moveMile();
  40. moveMile();
  41. moveMile();
  42. moveMile();
  43. moveMile();
  44. moveMile();
  45. moveMile();
  46. moveMile();
  47. moveMile();
  48. moveMile();
  49. moveMile();
  50. moveMile();
  51. moveMile();
  52. moveMile();
  53. moveMile();
  54. moveMile();
  55. moveMile();
  56. moveMile();
  57. moveMile();
  58. moveMile();
  59. moveMile();
  60. moveMile();
  61. moveMile();
  62. moveMile();
  63. moveMile();
  64. moveMile();
  65. moveMile();
  66. moveMile();
  67. moveMile();
  68. moveMile();
  69. moveMile();
  70. moveMile();
  71. moveMile();
  72. moveMile();
  73. moveMile();
  74. moveMile();
  75. moveMile();
  76. moveMile();
  77. moveMile();
  78. moveMile();
  79. moveMile();
  80. moveMile();
  81. moveMile();
  82. moveMile();
  83. moveMile();
  84. moveMile();
  85. moveMile();
  86. moveMile();
  87. moveMile();
  88. moveMile();
  89. moveMile();
  90. moveMile();
  91. moveMile();
  92. moveMile();
  93. moveMile();
  94. moveMile();
  95. moveMile();
  96. moveMile();
  97. moveMile();
  98. moveMile();
  99. moveMile();
  100. moveMile();
  101. moveMile();
  102. moveMile();
  103. moveMile();
  104. moveMile();
  105. moveMile();
  106. moveMile();
  107. moveMile();
  108. moveMile();
  109. moveMile();
  110. moveMile();
  111. moveMile();
  112. moveMile();
  113. moveMile();
  114. moveMile();
  115. moveMile();
  116. moveMile();
  117. moveMile();
  118. moveMile();
  119. moveMile();
  120. moveMile();
  121. moveMile();
  122. moveMile();
  123. moveMile();
  124. moveMile();
  125. moveMile();
  126. moveMile();
  127. moveMile();
  128. moveMile();
  129. moveMile();
  130. moveMile();
  131. moveMile();
  132. moveMile();
  133. moveMile();
  134. moveMile();
  135. moveMile();
  136. moveMile();
  137. moveMile();
  138. moveMile();
  139. moveMile();
  140. moveMile();
  141. moveMile();
  142. moveMile();
  143. moveMile();
  144. moveMile();
  145. moveMile();
  146. moveMile();
  147. moveMile();
  148. moveMile();
  149. }
  150.  
  151. main
  152. {
  153. Mover Karel = new Mover (1, 1, East, 0);
  154. Karel.turnAround();
  155. Karel.turnAround();
  156. moveMile();
  157. moveKiloMile();
  158. }
  159. }

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
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,024
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 126
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Loop...without the loop

 
0
  #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:

  1. void moveKiloMile(int i)
  2. {
  3. if (i > 0)
  4. {
  5. moveMile();
  6. i--;
  7. moveKiloMile(i);
  8. }
  9. }

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
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,024
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 126
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Loop...without the loop

 
0
  #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:

  1. loop(1000)
  2. {
  3. moveMile();
  4. }

or, alternatively

  1. int i = 0;
  2. while (i < 1000)
  3. {
  4. moveMile();
  5. i++;
  6. }

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
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 1,135
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Solved Threads: 5
Team Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Loop...without the loop

 
0
  #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.
Check out my blog at http://www.shinylight.com for more stuff about web dev.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,024
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 126
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Loop...without the loop

 
0
  #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
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 1,135
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Solved Threads: 5
Team Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Loop...without the loop

 
0
  #6
Jun 24th, 2003
Then the teacher is whacked.
Check out my blog at http://www.shinylight.com for more stuff about web dev.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,024
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 126
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Loop...without the loop

 
0
  #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
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 1,135
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Solved Threads: 5
Team Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Loop...without the loop

 
0
  #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?
Check out my blog at http://www.shinylight.com for more stuff about web dev.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 35
Reputation: theQube is an unknown quantity at this point 
Solved Threads: 2
theQube theQube is offline Offline
Light Poster

Re: Loop...without the loop

 
0
  #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 Quick reply to this message  
Join Date: Feb 2003
Posts: 35
Reputation: theQube is an unknown quantity at this point 
Solved Threads: 2
theQube theQube is offline Offline
Light Poster

Re: Loop...without the loop

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC