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

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