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

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