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.

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