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

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