Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a static method called printTwoDigit that takes a Random object and an int

ID: 660784 • Letter: W

Question

Write a static method called printTwoDigit that takes a Random object and an integer n as parameters and that prints a series of n randomly generated two-digit numbers. The method should use the Random object to select numbers in the range of 10 to 99inclusive where each number is equally likely to be chosen. The method should indicate whether the number 42 was selected. For example, given the following lines of code:

You would expect output like the following:

As the final line of output above indicates, that particular sequence did not include the number 42. Suppose that we then call the method as follows:

We might get output like the following where 42 is included in the sequence:

You may assume the integer value passed to your method is greater than or equal to 0.

(Because this problem uses random numbers, our test cases check only the general format of your output. You must still examine the output yourself to make sure the answer is correct.)

Method problem: For this problem, you are supposed to write a Java method as described. You should notwrite a complete Java class; just write the method(s) described in the problem statement.

Explanation / Answer

public static void printTwoDigit(Random r, int n){
   boolean found = false;
   for(int i=0; i<n; i++){
       int k = r.nextInt(90)+10;
       if(k == 42) found = true;
       System.out.println("next = "+k);
   }
   if(found)
       System.out.println("We saw a 42");
   else
       System.out.println("no 42");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote