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 11KB

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