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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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=0; i<5; i++){
  19. w = w + 1;
  20. }
  21. // each time through the loop
  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. // calling
  29. for (int i=0; i<10; i++){
  30. w = w + 1;
  31. }
  32. // each time through the loop
  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=22; i<33; i++){
  40. w = w + 1;
  41. }
  42. // each time through the loop
  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. }
  52. // each time through the loop
  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. }
  62. // each time through the loop
  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=5001; i>0; i-=11) {
  70. w = w + 1;
  71. }
  72. // each time through the loop
  73. return w;
  74. }
  75. public int nestedFors() {
  76. int w = 0;
  77. // Write a nested FOR loop(s), where one counts from
  78. // 0 to less than 20 and the inner one counts from 0 to 4
  79. // calling
  80. for (int i=0; i<20; i++){
  81. for (int b=0; b<=4; b++){
  82. w = w + 1;}}
  83. // each time through the inner loop
  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. // calling
  93. for (int i=0; i<150; i++){
  94. if (i>46){System.out.println("Hello ZipCode");}
  95. else{
  96. w = w + 1;}
  97. }
  98. // each time through the inner loop
  99. return w;
  100. }
  101. public void simpleLoops() {
  102. int i = 0;
  103. // sample while loop
  104. while (i <= 5) {
  105. System.out.println("Eww.");
  106. i = i + 1;
  107. }
  108. // sample do...while loop
  109. i = 8;
  110. do {
  111. System.out.println("Eww.");
  112. i = i - 1;
  113. } while (i > 0);
  114. // what's the primary difference between them?!?
  115. }
  116. // Write a WHILE loop that checks “gpsCurrentLocation()”
  117. // and if that is not equal to “Home” then and it calls “driveSomeMore()”.
  118. // After the loop is done, print “Honey, I’m Home!”
  119. public int driveHome() {
  120. int w = 0;
  121. while (!gpsCurrentLocation().equals("Home")) {
  122. // you need to use a .equals for two Strings.
  123. driveSomeMore();
  124. // calling
  125. w = w + 1;
  126. // each time through the inner loop
  127. }
  128. System.out.println("Honey, I'm Home!");
  129. return w;
  130. }
  131. // Getting harder...
  132. // First declare and set “highestScore” to 236. Then set “currentScore” to
  133. // “gameNextScore()”. Then write a WHILE loop that checks "runningScore"
  134. // is less than “highestScore” and if it is, adds “currentScore” to
  135. // "runningScore"
  136. // and then sets “currentScore” to “gameNextScore()”
  137. public int checkGameScore() {
  138. int w = 0;
  139. int highestScore = 236;
  140. int currentScore = gameNextScore();
  141. int runningScore = 0;
  142. while (runningScore<highestScore){
  143. // do your while loop here
  144. runningScore+= currentScore;
  145. // calling
  146. w = w + 1;
  147. // each time through the inner loop
  148. }
  149. return w; // >= 3;
  150. }
  151. // Rewrite the previous WHILE loop as a DO..WHILE loop.
  152. // Notice how the “runningScore” variable usage is different.
  153. public boolean checkGameScoreDoWhile() {
  154. int w = 0;
  155. int highestScore = 236;
  156. int currentScore = gameNextScore();
  157. int runningScore = 0;
  158. // do your while loop here
  159. do {
  160. runningScore+= currentScore;
  161. // calling
  162. w = w + 1;
  163. } while (runningScore<highestScore);
  164. // each time through the inner loop
  165. return w >= 3;
  166. }
  167. // Write a WHILE loop that checks “serverIsRunning()” and if true
  168. // calls “waitFor(5)” After the loop, write an IF and check “serverIsRunning()”
  169. // is false, and if so, call “sendEmergencyText(“Help!”, adminPhoneNumber)”
  170. // and also calls “tryServerRestart()”
  171. public int checkServerStatus() {
  172. int w = 0;
  173. String adminPhoneNumber = "+1 202 456 1111";
  174. while (serverIsRunning()){
  175. // calling
  176. waitFor(5);
  177. w = w + 1;}
  178. // each time through the inner loop
  179. if (!serverIsRunning()){
  180. sendEmergencyText("Help!", adminPhoneNumber);
  181. tryServerRestart("Restart my server please.", adminPhoneNumber);
  182. }
  183. return w;
  184. }
  185. // Declare an “int” i. Set i to 7.
  186. // Write a WHILE loop that checks “i” is less than 50,
  187. // and if it is, add 7 to “i”
  188. public int loop50by7() {
  189. int w = 0;
  190. int i = 7;
  191. while (i<59) {
  192. // calling
  193. i += 7;
  194. // each time through the inner loop
  195. }
  196. return w;
  197. }
  198. int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
  199. // Foo is method that add the first 7 factors of three together and prints
  200. // out the sum of them all.
  201. public int foo() {
  202. int w = 0;
  203. // this is an array of ints. it is of length 7 (from 0 -> 6)
  204. int sumOfThrees = 0;
  205. // this is a so called Enhanced for loop
  206. for (int index : threes_array) {
  207. sumOfThrees = sumOfThrees + index;
  208. // calling
  209. w = w + 1;
  210. // each time through the inner loop
  211. }
  212. System.out.print("The Sum is ");
  213. System.out.println(sumOfThrees);
  214. return w;
  215. }
  216. // Ponder this: can all FOR loops be rewritten as WHILE loops?
  217. // rewrite the loop inside of "foo()" as a standard for loop
  218. // with 'i' as its index variable.
  219. public int rewriteFooAsFor() {
  220. int sumOfThrees = 0;
  221. for (int w=0; w<threes_array.length; w++ ){
  222. // calling
  223. sumOfThrees = sumOfThrees + 1;
  224. // each time through the inner loop
  225. }
  226. System.out.print("The Sum is ");
  227. System.out.println(sumOfThrees);
  228. return sumOfThrees;
  229. }
  230. // Ponder this: can all WHILE loops be rewritten as FOR loops?
  231. // rewrite the loop inside of "foo()" as a 'while' loop
  232. public int rewriteFooAsWhile() {
  233. int w = 0;
  234. int sumOfThrees = 0;
  235. while (sumOfThrees < threes_array.length){
  236. // calling
  237. sumOfThrees = sumOfThrees + w;
  238. w = w + 1;
  239. // each time through the inner loop
  240. }
  241. System.out.print("The Sum is ");
  242. System.out.println(sumOfThrees);
  243. return w;
  244. }
  245. // Declare a boolean “yardNeedsMowed” and initialize to true.
  246. // Write WHILE loop that checks for “isSummer()”.
  247. // inside the loop, write an IF that checks “yardNeedsMowed” and if true calls
  248. // “yellAtJuniorToMowLawn()”
  249. // After loop, call
  250. // “sendJuniorBackToSchool()” with an argument that decribes the day junior goes
  251. // back.
  252. public int manageYardAndJunior() {
  253. int w = 0;
  254. boolean onTime = true;
  255. boolean yardNeedsMowed = true;
  256. while(isSummer()){
  257. // ADD YOUR CODE here.
  258. if(yardNeedsMowed){
  259. yellAtJuniorToMowLawn();}
  260. // be sure to call
  261. w = w + 1;
  262. // each time inside the loop
  263. }
  264. sendJuniorBackToSchool("Go to school");
  265. return w;
  266. }
  267. String voteTallies[] = { "Lincoln", "Washington", "Adams", "Lincoln", "Washington",
  268. "Adams", "Lincoln",
  269. "Washington", "Adams", "Lincoln", "Washington", "Adams", "Roosevelt" };
  270. // Given an array voteTallies[], write a FOR loop that prints out each value in
  271. // the array.
  272. public int tallyVote1() {
  273. int w = 0;
  274. int numberOfVotes = voteTallies.length;
  275. for (int idx=0; idx< voteTallies.length; idx++){
  276. // calling
  277. System.out.println(voteTallies[idx]);
  278. w = w + 1;
  279. // each time through the inner loop
  280. }
  281. return w;
  282. }
  283. // Given an array voteTallies[], write a WHILE loop that prints out each value
  284. // in the array. You should declare and use an index “idx” to keep track of
  285. // where you are.
  286. public int tallyVote2() {
  287. int w = 0;
  288. int numberOfVotes = voteTallies.length;
  289. while (w<numberOfVotes){
  290. // calling
  291. System.out.println(voteTallies[w]);
  292. w = w + 1;
  293. // each time through the inner loop
  294. }
  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. }