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

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