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 13KB

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