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

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