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.

WriteLoops.java 12KB

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