Menu

Java Object Oriented Programming Import Javautilrandom Public Class Loops Loop N Equal M P Q43854642

Java – object oriented programming
Consider the Loops class. Compile and run the program. Each of the methods in the Loons class has a comment explaining what t

:

import java.util.Random;

public class Loops {
  
           //loop until nis equal to m
           private staticvoid loop1() {
              int m = 1;
              int n = m + 2;
              while (n > m) {
                  System.out.println(“Inloop.”);
                  m–;
              }
              System.out.println(“Out of loop.”);
           }
           /*
           * PROBLEM: theloop is infinite, once started it does not stop.
           * DIAGNOSIS: mis decremented in the loop, meaning that m gets smaller while nstays the same.
           The effect thatthe guard is always true (n is always bigger than m), so the looponce started will not stop
           * CORRECTION:decrement n instead of m
           */

                 /*************************************************************/  
  
  
  
  
       //loop 5 times
       public static void loop2() {
           int i = 0;
           while(i <5)
              System.out.println(“In loop “+i);
              i++;
          System.out.println(“Out of loop”);
       }
       /*
       * PROBLEM:  
       * DIAGNOSIS:
       * CORRECTION:
      */
  
  
                 /*************************************************************/  
  
  
  
  
  
       /*
       Draws a trapezoid (AmericanEnglish)/trapezium (British English) using asterisks, the shape has7 lines, the top line has 6 stars, the next row 1 more (ie 7), rowsincrease by 1 until the bottom row has 12 stars as follows:
       ******
                  *******
                  ********
                  *********
                  **********
                  ***********
                  ************         
       */
       private static void loop3() {
           int k = 7;
           int m =13;
           int y = 6;
           for (int i = 0;i < k; i++) {
              for (int j = 0; j < m; j++) {
                  if (j<y)System.out.print(“*”);
              }
              System.out.println();
              //y++; DELETE ME
           }
       }
           /*
           * PROBLEM:          
           *DIAGNOSIS:
           *CORRECTION:
           */     
  
  
                 /*************************************************************/  

  
  
           //loop until xis equal to y
           private staticvoid 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 Fibonaccinumbers
       NOTE: Fibonacci numbers start 0, 1and 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<50; i++){
              fibNext = fib1 + fib2;
              System.out.println(fibNext);
              fib1 = fib2;
              fib2 = fibNext;  
           }
       }
       /*
       * PROBLEM:
       * DIAGNOSIS:
       * CORRECTION:
       */
      
                 /*************************************************************/  
  
  
  
      
           //print avertical line of 10 stars
           public staticvoid loop6() {
              for (int i = 0; i < 10; i++); {
                 System.out.println(“*”);
              }
           }
           /*
           * PROBLEM:
           *DIAGNOSIS:
           *CORRECTION:
           */
            
            
                 /*************************************************************/  
  

            
  
       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] Show transcribed image text 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]

Expert Answer


Answer to Java – object oriented programming : import java.util.Random; public class Loops { //loop until n is equal to m private …

OR