Write a method named randomStar that takes two parameters, a Random object r and an integer num. The method should keep printing lines of * characters, where each line has between 5 and 19 * characters inclusive, until it prints a line with greater than or equal to num characters. num is guaranteed to be between 5 and 19. For example, the method call randomStar(14) will print:

Respuesta :

Answer:

The method is as follows:

public static void randomStar(Random r, int num) {

   int n;

   do {

       n = 5 + r.nextInt(15);

       for(int count = 0; count < n; count++){

           System.out.print("*");

       }

       System.out.println();

   } while(n < num);

}

Explanation:

This defines the method

public static void randomStar(Random r, int num) {

This declares n as integer

   int n;

The following is repeated until n >= num

   do {

This generates a random number for n (5 and 19)

       n = 5 + r.nextInt(15);

The following loop prints n *'s

       for(int count = 0; count < n; count++){

           System.out.print("*");

       }

This prints n

       System.out.println();

   } while(n < num); The loop is repeated as long as the condition is valid

}