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

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