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

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