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

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