150k views
1 vote
add a method to your randomtester class that takes two parameters, min and max, and generates a random number in the range min to max (inclusive). rewrite the body of the method you wrote for the previous exercise so that it now calls this new method to generate its result. note that it should not be necessary to use a loop in this method.

1 Answer

5 votes

Final answer:

To add a method to the RandomTester class that generates a random number within a given range, create a new method called generateRandomNumber.

Step-by-step explanation:

To add a method to the RandomTester class that generates a random number within a given range, we can create a new method called generateRandomNumber. This method should take two parameters, min and max, which represent the minimum and maximum values in the desired range.

Here is an example of the code:

public class RandomTester {
public static int generateRandomNumber(int min, int max) {
return (int)(Math.random() * (max - min + 1)) + min;
}

public static void main(String[] args) {
int randomNumber = generateRandomNumber(1, 10);
System.out.println(randomNumber);
}
}
User Jay Jung
by
8.3k points