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.

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