943,447 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7383
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 24th, 2003
0

Loop...without the loop

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 3
Light Poster
theQube is offline Offline
35 posts
since Feb 2003
Jun 24th, 2003
0

Re: Loop...without the loop

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:

Java Syntax (Toggle Plain Text)
  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!
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Jun 24th, 2003
0

Re: Loop...without the loop

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:

Java Syntax (Toggle Plain Text)
  1. loop(1000)
  2. {
  3. moveMile();
  4. }

or, alternatively

Java Syntax (Toggle Plain Text)
  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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Jun 24th, 2003
0

Re: Loop...without the loop

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.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Jun 24th, 2003
0

Re: Loop...without the loop

According to what I saw of the how-to, no labels are supported either. Just functions and function calls!
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Jun 24th, 2003
0

Re: Loop...without the loop

Then the teacher is whacked.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Jun 24th, 2003
0

Re: Loop...without the loop

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?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Jun 24th, 2003
0

Re: Loop...without the loop

Quote originally posted by theQube ...
Hi. I'm in a Java class at harvard...
But a while is a loop. This is a Harvard teacher?
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Jun 24th, 2003
0

Re: Loop...without the loop

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.
Reputation Points: 10
Solved Threads: 3
Light Poster
theQube is offline Offline
35 posts
since Feb 2003
Jun 24th, 2003
0

Re: Loop...without the loop

I'm not actually looking for code, either. More of just the techique as to how to do it.
Reputation Points: 10
Solved Threads: 3
Light Poster
theQube is offline Offline
35 posts
since Feb 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help With An Online Game!
Next Thread in Java Forum Timeline: Help with java prog...!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC