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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. w = w + 1;
  19. // each time through the loop
  20. // this will tell the test how many times the loop executed.
  21. for (int i = 1; i < 5; i ++)
  22. {
  23. w = w+1;
  24. }
  25. return w;
  26. }
  27. public int oneToTen() {
  28. int w = 0;
  29. // Write a FOR loop that counts from 1 to 10.
  30. // calling
  31. w = w + 1;
  32. // each time through the loop
  33. for(int i = 1; i < 10; i ++){
  34. w = w + 1;
  35. }
  36. return w;
  37. }
  38. public int startAtTwentyOne() {
  39. int w = 0;
  40. // Write a FOR loop that makes 10 iterations, start at 21.
  41. // calling
  42. for(int i = 21; i < 32; i ++)
  43. {
  44. w = w + 1;
  45. }
  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 = 0; i < 100; i ++){
  54. w = w + 1;
  55. // each time through the loop
  56. }
  57. return w;
  58. }
  59. public int byTwoTo32() {
  60. int w = 0;
  61. for(int i = 0; i < 32; i += 2)
  62. {
  63. //w = w + 1;
  64. }
  65. return w;
  66. }
  67. public int countDownFrom5000() {
  68. int w = 0;
  69. for(int i = 1; i < 5001; i += 11)
  70. {
  71. w = w + 1;
  72. }
  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. //w = w + 1;
  81. // each time through the inner loop
  82. for(int i = 0; i < 20; i ++)
  83. {
  84. for(int j = 0; j <= 4; j ++)
  85. {
  86. w = w + 1;
  87. }
  88. }
  89. return w;
  90. }
  91. public int helloZipCode() {
  92. int w = 0;
  93. // Write a FOR loop that counts from 5 to 105. Put an IF
  94. // statement inside the loop that checks the
  95. // loop index counter and if it’s greater than 51,
  96. // prints “Hello Zipcode” instead of the statement w = w + 1;
  97. // calling
  98. // each time through the inner loop
  99. for(int i = 5; i < 105; i ++)
  100. {
  101. if(i > 51)
  102. {
  103. System.out.println("Hello Zipcode");
  104. } else {
  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. }
  125. // Write a WHILE loop that checks “gpsCurrentLocation()”
  126. // and if that is not equal to “Home” then and it calls “driveSomeMore()”.
  127. // After the loop is done, print “Honey, I’m Home!”
  128. public int driveHome() {
  129. int w = 0;
  130. // you need to use a .equals for two Strings.
  131. while(!gpsCurrentLocation().equals("Home"))
  132. {
  133. // calling
  134. w = w + 1;
  135. // each time through the inner loop
  136. driveSomeMore();
  137. }
  138. System.out.println("Honey, I'm Home!");
  139. return w;
  140. }
  141. // Getting harder...
  142. // First declare and set “highestScore” to 236. Then set “currentScore” to
  143. // “gameNextScore()”. Then write a WHILE loop that checks "runningScore"
  144. // is less than “highestScore” and if it is, adds “currentScore” to
  145. // "runningScore"
  146. // and then sets “currentScore” to “gameNextScore()”
  147. public int checkGameScore() {
  148. int w = 0;
  149. int highestScore = 236;
  150. int currentScore = gameNextScore();
  151. int runningScore = 0;
  152. // do your while loop here
  153. while(runningScore < highestScore){
  154. if(runningScore < highestScore)
  155. {
  156. runningScore += currentScore;
  157. w = w + 1;
  158. }
  159. currentScore = gameNextScore();
  160. }
  161. return w; // >= 3;
  162. }
  163. // Rewrite the previous WHILE loop as a DO..WHILE loop.
  164. // Notice how the “runningScore” variable usage is different.
  165. public boolean checkGameScoreDoWhile() {
  166. int w = 0;
  167. int highestScore = 236;
  168. int currentScore = gameNextScore();
  169. int runningScore = 0;
  170. do {
  171. if(runningScore < highestScore)
  172. {
  173. runningScore += currentScore;
  174. w = w + 1;
  175. }
  176. currentScore = gameNextScore();
  177. }while(runningScore < highestScore);
  178. return w >= 3;
  179. }
  180. // Write a WHILE loop that checks “serverIsRunning()” and if true
  181. // calls “waitFor(5)” After the loop, write an IF and check “serverIsRunning()”
  182. // is false, and if so, call “sendEmergencyText(“Help!”, adminPhoneNumber)”
  183. // and also calls “tryServerRestart()”
  184. public int checkServerStatus() {
  185. int w = 0;
  186. String adminPhoneNumber = "+1 202 456 1111";
  187. while(serverIsRunning())
  188. {
  189. if(serverIsRunning() == true)
  190. {
  191. waitFor(5);
  192. }
  193. w = w + 1;
  194. }
  195. if(serverIsRunning() == false){
  196. sendEmergencyText("Help", adminPhoneNumber);
  197. tryServerRestart("Help", adminPhoneNumber);
  198. }
  199. return w;
  200. }
  201. // Declare an “int” i. Set i to 7.
  202. // Write a WHILE loop that checks “i” is less than 50,
  203. // and if it is, add 7 to “i”
  204. public int loop50by7() {
  205. int w = 0;
  206. int i = 7;
  207. while(i < 50){
  208. i += 7;
  209. w = w + 1;
  210. }
  211. // calling
  212. // each time through the inner loop
  213. return w;
  214. }
  215. int[] threes_array = { 3, 6, 9, 12, 15, 18, 21 };
  216. // Foo is method that add the first 7 factors of three together and prints
  217. // out the sum of them all.
  218. public int foo() {
  219. int w = 0;
  220. // this is an array of ints. it is of length 7 (from 0 -> 6)
  221. int sumOfThrees = 0;
  222. // this is a so called Enhanced for loop
  223. for (int index : threes_array) {
  224. sumOfThrees = sumOfThrees + threes_array[index];
  225. // calling
  226. w = w + 1;
  227. // each time through the inner loop
  228. }
  229. System.out.print("The Sum is ");
  230. System.out.println(sumOfThrees);
  231. return w;
  232. }
  233. // Ponder this: can all FOR loops be rewritten as WHILE loops?
  234. // rewrite the loop inside of "foo()" as a standard for loop
  235. // with 'i' as its index variable.
  236. public int rewriteFooAsFor() {
  237. int w = 0;
  238. int sumOfThrees = 0;
  239. // calling
  240. w = w + 1;
  241. // each time through the inner loop
  242. System.out.print("The Sum is ");
  243. System.out.println(sumOfThrees);
  244. return w;
  245. }
  246. // Ponder this: can all WHILE loops be rewritten as FOR loops?
  247. // rewrite the loop inside of "foo()" as a 'while' loop
  248. public int rewriteFooAsWhile() {
  249. int w = 0;
  250. int sumOfThrees = 0;
  251. // calling
  252. w = w + 1;
  253. // each time through the inner loop
  254. System.out.print("The Sum is ");
  255. System.out.println(sumOfThrees);
  256. return w;
  257. }
  258. // Declare a boolean “yardNeedsMowed” and initialize to true.
  259. // Write WHILE loop that checks for “isSummer()”.
  260. // inside the loop, write an IF that checks “yardNeedsMowed” and if true calls
  261. // “yellAtJuniorToMowLawn()”
  262. // After loop, call
  263. // “sendJuniorBackToSchool()” with an argument that decribes the day junior goes
  264. // back.
  265. public int manageYardAndJunior() {
  266. int w = 0;
  267. boolean onTime = true;
  268. // ADD YOUR CODE here.
  269. // be sure to call
  270. w = w + 1;
  271. // each time inside the loop
  272. return w;
  273. }
  274. String voteTallies[] = { "Lincoln", "Washington", "Adams", "Lincoln", "Washington", "Adams", "Lincoln",
  275. "Washington", "Adams", "Lincoln", "Washington", "Adams", "Roosevelt" };
  276. // Given an array voteTallies[], write a FOR loop that prints out each value in
  277. // the array.
  278. public int tallyVote1() {
  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. // Given an array voteTallies[], write a WHILE loop that prints out each value
  287. // in the array. You should declare and use an index “idx” to keep track of
  288. // where you are.
  289. public int tallyVote2() {
  290. int w = 0;
  291. int numberOfVotes = voteTallies.length;
  292. // calling
  293. w = w + 1;
  294. // each time through the inner loop
  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. }