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.

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