Write bunch of java to solve a series of requirements and pass a series of tests to prove your code works the way it should.

WriteLoops.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import com.sun.org.apache.xpath.internal.SourceTree;
  2. /**
  3. * Writeloops get you thinking about how to do different things with loops.
  4. *
  5. * @author kyounger
  6. * @version 1.1
  7. */
  8. public class WriteLoops {
  9. private static final int _3 = 3;
  10. public int oneToFive() {
  11. int w = 0;
  12. // Write a FOR loop that counts from 1 to 10.
  13. for (int i = 1; i <= 5; i++) {
  14. // calling
  15. w = w + 1;
  16. // each time through the loop
  17. }
  18. // this will tell the test how many times the loop executed.
  19. return w;
  20. }
  21. public int oneToTen() {
  22. int w = 0;
  23. // Write a FOR loop that counts from 1 to 10.
  24. // calling
  25. w = w + 1;
  26. // each time through the loop
  27. return w;
  28. }
  29. public int startAtTwentyOne() {
  30. int w = 0;
  31. // Write a FOR loop that makes 10 iterations, start at 21.
  32. // calling
  33. w = w + 1;
  34. // each time through the loop
  35. return w;
  36. }
  37. public int countDown() {
  38. int w = 0;
  39. // Write a FOR loop that counts down from 100 to 0.
  40. // calling
  41. w = w + 1;
  42. // each time through the loop
  43. return w;
  44. }
  45. public int byTwoTo32() {
  46. int w = 0;
  47. // Write a FOR loop from 0 to 32 by 2s.
  48. // calling
  49. w = w + 1;
  50. // each time through the loop
  51. return w;
  52. }
  53. public int countDownFrom5000() {
  54. int w = 0;
  55. // Write a FOR loop from 1 to less than 5001 by 11s.
  56. // calling
  57. w = w + 1;
  58. // each time through the loop
  59. return w;
  60. }
  61. public int nestedFors() {
  62. int w = 0;
  63. // Write a nested FOR loop(s), where one counts from
  64. // 0 to less than 20 and the inner one counts from 0 to 4
  65. // calling
  66. w = w + 1;
  67. // each time through the inner loop
  68. return w;
  69. }
  70. public int helloZipCode() {
  71. int w = 0;
  72. // Write a FOR loop that counts from 5 to 105. Put an IF
  73. // statement inside the loop that checks the
  74. // loop index counter and if it’s greater than 51,
  75. // prints “Hello Zipcode” instead of the statement w = w + 1;
  76. // calling
  77. w = w + 1;
  78. // each time through the inner loop
  79. return w;
  80. }
  81. public void simpleLoops() {
  82. int i = 0;
  83. // sample while loop
  84. while (i <= 5) {
  85. System.out.println("Eww.");
  86. i = i + 1;
  87. }
  88. // sample do...while loop
  89. i = 8;
  90. do {
  91. System.out.println("Eww.");
  92. i = i - 1;
  93. } while (i > 0);
  94. // what's the primary difference between them?!?
  95. }
  96. // Write a WHILE loop that checks “gpsCurrentLocation()”
  97. // and if that is not equal to “Home” then and it calls “driveSomeMore()”.
  98. // After the loop is done, print “Honey, I’m Home!”
  99. public int driveHome() {
  100. int w = 0;
  101. // you need to use a .equals for two Strings.
  102. // calling
  103. w = w + 1;
  104. // each time through the inner loop
  105. System.out.println("Honey, I’m Home!");
  106. return w;
  107. }
  108. // Getting harder...
  109. // First declare and set “highestScore” to 236. Then set “currentScore” to
  110. // “gameNextScore()”. Then write a WHILE loop that checks “currentScore”
  111. // is less than “highestScore” and if it is, adds “currentScore” to
  112. // "runningScore"
  113. // and then sets “currentScore” to “gameNextScore()”
  114. public boolean checkGameScore() {
  115. int w = 0;
  116. int highestScore = 236;
  117. int currentScore = gameNextScore();
  118. int runningScore = 0;
  119. // do your while loop here
  120. // do your ifs here
  121. // calling
  122. w = w + 1;
  123. // each time through the inner loop
  124. return w > 3;
  125. }
  126. // Rewrite the previous WHILE loop as a DO..WHILE loop.
  127. // Notice how the “runningScore” variable usage is different.
  128. public boolean checkGameScoreDoWhile() {
  129. int w = 0;
  130. int highestScore = 236;
  131. int currentScore = gameNextScore();
  132. int runningScore = 0;
  133. // do your while loop here
  134. // do your ifs here
  135. // calling
  136. w = w + 1;
  137. // each time through the inner loop
  138. return w > 3;
  139. }
  140. // Write a WHILE loop that checks “serverIsRunning()” and if true
  141. // calls “waitFor(5)” After the loop, write an IF and check “serverIsRunning()”
  142. // is false, and if so, call “sendEmergencyText(“Help!”, adminPhoneNumber)”
  143. // and also calls “tryServerRestart()”
  144. public int checkServerStatus() {
  145. int w = 0;
  146. String adminPhoneNumber = "+1 202 456 1111"
  147. // calling
  148. w = w + 1;
  149. // each time through the inner loop
  150. return w;
  151. }
  152. // Declare an “int” i. Set i to 7.
  153. // Write a WHILE loop that checks “i” is less than 50,
  154. // and if it is, add 7 to “i”
  155. public int loop50by7() {
  156. int w = 0;
  157. // calling
  158. w = w + 1;
  159. // each time through the inner loop
  160. return w;
  161. }
  162. // Foo is method that add the first 7 factors of three together and prints
  163. // out the sum of them all.
  164. public int foo() {
  165. int w = 0;
  166. // this is an array of ints. it is of length 7 (from 0 -> 6)
  167. int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
  168. int sumOfThrees = 0;
  169. // this is a so called Enhanced for loop
  170. for (int index : threes_array) {
  171. sumOfThrees = sumOfThrees + threes_array[index];
  172. // calling
  173. w = w + 1;
  174. // each time through the inner loop
  175. }
  176. System.out.print("The Sum is ");
  177. System.out.println(sumOfThrees);
  178. return w;
  179. }
  180. // Ponder this: can all FOR loops be rewritten as WHILE loops?
  181. // rewrite the loop inside of "foo()" as a standard for loop
  182. // with 'i' as its index variable.
  183. public int rewriteFooAsFor() {
  184. int w = 0;
  185. int sumOfThrees = 0;
  186. // calling
  187. w = w + 1;
  188. // each time through the inner loop
  189. System.out.print("The Sum is ");
  190. System.out.println(sumOfThrees);
  191. return w;
  192. }
  193. // Ponder this: can all WHILE loops be rewritten as FOR loops?
  194. // rewrite the loop inside of "foo()" as a 'while' loop
  195. public int rewriteFooAsWhile() {
  196. int w = 0;
  197. int sumOfThrees = 0;
  198. // calling
  199. w = w + 1;
  200. // each time through the inner loop
  201. System.out.print("The Sum is ");
  202. System.out.println(sumOfThrees);
  203. return w;
  204. }
  205. // Declare a boolean “yardNeedsMowed” and initialize to true.
  206. // Write WHILE loop that checks for “isSummer()”.
  207. // inside the loop, write an IF that checks “yardNeedsMowed” and if true calls
  208. // “yellAtJuniorToMowLawn()”
  209. // After loop, call
  210. // “sendJuniorBackToSchool(onTime)”
  211. public int manageYardAndJunior() {
  212. int w = 0;
  213. boolean onTime = true;
  214. // ADD YOUR CODE here.
  215. // be sure to call
  216. w = w + 1;
  217. // each time inside the loop
  218. return w;
  219. }
  220. String voteTallies[] = { "Lincoln", "Washington", "Adams",
  221. "Lincoln", "Washington", "Adams", "Lincoln", "Washington", "Adams",
  222. "Lincoln", "Washington", "Adams", "Roosevelt"
  223. };
  224. // Given an array voteTallies[], write a FOR loop that prints out each value in
  225. // the array.
  226. public int tallyVote1() {
  227. int w = 0;
  228. int numberOfVotes = voteTallies.length;
  229. // calling
  230. w = w + 1;
  231. // each time through the inner loop
  232. return w;
  233. }
  234. // Given an array voteTallies[], write a WHILE loop that prints out each value
  235. // in the array. You should declare and use an index “idx” to keep track of
  236. // where you are.
  237. public int tallyVote2() {
  238. int w = 0;
  239. int numberOfVotes = voteTallies.length;
  240. // calling
  241. w = w + 1;
  242. // each time through the inner loop
  243. return w;
  244. }
  245. /**
  246. * CONGRATS, you've written all the code. Does it all pass their tests?!?
  247. *
  248. *
  249. * If not, why not? :-)
  250. *
  251. *
  252. */
  253. /**
  254. * IGNORE the CODER behind the CURTAIN. These are the support routines to make
  255. * all the examples interesting.
  256. */
  257. // instance variables - replace the example below with your own
  258. private int x;
  259. /**
  260. * Constructor for objects of class WriteLoops
  261. */
  262. public WriteLoops() {
  263. // initialise instance variables
  264. x = 0;
  265. }
  266. private int gps = 0;
  267. private String gpsCurrentLocation() {
  268. if (this.gps > 5) {
  269. return "Home";
  270. }
  271. return "Not Home";
  272. }
  273. private void driveSomeMore() {
  274. this.gps += 1;
  275. }
  276. private int scr = 31;
  277. private int gameNextScore() {
  278. return this.scr = this.scr + ThreadLocalRandom.current().nextInt(20, 99 + 1);
  279. }
  280. private boolean isSummer = () -> {
  281. int i = 0;
  282. return () -> {
  283. i = i + 1;
  284. return (i >= _3);
  285. };
  286. };
  287. private void sendEmergencyText(String mesg, String phone) {}
  288. private void tryServerRestart(String mesg, String phone) {}
  289. int serverStatus = 5;
  290. private boolean serverIsRunning() {return (serverStatus > 20);}
  291. private void waitFor(int interval) {serverStatus += interval;}
  292. }