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.

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