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

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