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

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