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

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