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.

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