188k views
5 votes
ANSWERED CORRECT BELOW

In this exercise, we are going to create a static class Randomizer that will allow users to get random integer values from the method nextInt() and nextInt(int min, int max).


Remember that we can get random integers using the formula int randInteger = (int)(Math.random() * (range + 1) + startingNum).


nextInt() should return a random value from 1 - 10, and nextInt(int min, int max) should return a random value from min to max. For instance, if min is 3 and max is 12, then the range of numbers should be from 3 - 12, including 3 and 12.


This is what I have so far:



public class RandomizerTester

{

public static void main(String[] args)

{


System.out.println("Results of Randomizer.nextInt()");

for(int i = 0; i < 10; i++)

{

System.out.println(Randomizer.nextInt());

}


//Initialize min and max for Randomizer.nextInt(min,max)

int min = 5;

int max = 10;

System.out.println("\\Results of Randomizer.nextInt(5,10)");

for(int i = 0; i < 10; i++)

{

System.out.println(Randomizer.nextInt(min ,max));

}


}

}






public class Randomizer
{
private static int range;
private static int startingNum;
private static int nextInt;
private static int max;
private static int min;


public static int nextInt()
{
//Implement this method to return a random number from 1-10
//Randomizer randInteger = new Randomizer();
int randInteger = (int)(Math.random() * (10) + 1);
return randInteger;
}

public static int nextInt(int min , int max)
{
//Implement this method to return a random integer between min and max
int randInteger = (int)(Math.random() * (max-min+1) + min);
return randInteger;

}
}

1 Answer

2 votes

Answer:

Step-by-step explanation:

The Java code provided in the question works as intended. The nextInt() correctly outputs the random value from 1-10 while the nextInt(int min, int max) correctly outputs random values between the int and max parameters. I changed the int/max arguments to 3 and 12 and ran the program to demonstrate that the program is running as intended. Output can be seen in the attached picture below.

ANSWERED CORRECT BELOW In this exercise, we are going to create a static class Randomizer-example-1
User FortuneFaded
by
3.1k points