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

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