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

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