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.

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