Question: Java - object oriented programming : import java.util.Random; public class Loops { //loop until n is equal to m private static void loop1() { int

Java - object oriented programming Java - object oriented programming : import java.util.Random; public class Loops {

:

import java.util.Random;

public class Loops { //loop until n is equal to m private static void loop1() { int m = 1; int n = m + 2; while (n > m) { System.out.println("In loop."); m--; } System.out.println("Out of loop."); } /* * PROBLEM: the loop is infinite, once started it does not stop. * DIAGNOSIS: m is decremented in the loop, meaning that m gets smaller while n stays the same. The effect that the guard is always true (n is always bigger than m), so the loop once started will not stop * CORRECTION: decrement n instead of m */

/*************************************************************/ //loop 5 times public static void loop2() { int i = 0; while(i

//loop until x is equal to y private static void loop4(){ Random r = new Random(); int x = 0; int y = 10; for (;;){ x = r.nextInt(y); if (x==y)break; System.out.println("x = "+x+" and y = "+y); } System.out.println("out of loop"); } /* * PROBLEM: * DIAGNOSIS: * CORRECTION: */ /*************************************************************/

/* print the first 52 Fibonacci numbers NOTE: Fibonacci numbers start 0, 1 and after this the next number is the sum of the previous two hence 0, 1, 1 (=0+1), 2 (=1+1), 3 (=1+2), 5, 8, 13... */ public static void loop5() { int fibNext = 0; int fib1 = 0; int fib2 = 1; System.out.println(fib1); System.out.println(fib2); for(int i = 0; i

public static void runLoops() { loop1(); System.out.println(); loop2(); System.out.println(); loop3(); System.out.println(); loop4(); System.out.println(); loop5(); System.out.println(); loop6(); System.out.println(); } public static void main(String[] args) { runLoops(); } }

Consider the Loops class. Compile and run the program. Each of the methods in the Loons class has a comment explaining what the method is intended to do. None of the methods work as intended. Please complete the following task: Directly under each of the methods loop20, loop30, loop40, loop50) and loop60) write three comments: The first comment should start with "PROBLEM" and it should detail what the method is doing that it should not be doing, or conversely, what the method is not doing that it should be doing. The second comment should start "DIAGNOSIS" and should explain why the method has the problem described in the first comment The third comment should start "CORRECTION" and should explain how the method can be corrected so that it works as intended. You may find that for most methods you will only need to write a sentence or two for each comment, some may take more work to document, but do not write more than five sentences for each comment Note that the first method in the Loops class, loop10. has been completed for you as an example. [30 marks]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!