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