WriteLoops.java 10KB

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