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

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