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

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