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

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