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.

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