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.

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