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

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