I did the code, make a random number between 1 and 100 as task A), then how I understood if the first value if more than 50 generate second random number between 1 and 50 as said ( how I think) task B)
PLEASE can any one explain what the task C and D is, not understand how to do it at all ..(((pls help with advice or explanation task C and D.
Thanks….
TASK:
Write a program that generates random numbers:
a) +Write a method that returns a random integer in the range of 1 to 100.
b) Then add another method that takes a parameter specifying the top number – i.e. if you pass it 50 it returns a random number between 1 and 50. Test your random method and make sure it works.
c) Write another method so that you pass it two values – the top and bottom of the range you want the highest value from – i.e. if you pass it 10 and 20 it returns a random number between 10 and 20.
d) Then write another method so that if repeatedly called it doesn’t return two numbers the same? So once a random number has been generated and returned, the method doesn’t return that number again. To do this you will have to store every number generated.
import java.util.Random;
public class ranGen {
public Integer random (Integer integer){
Random rand = new Random();;
int min=0, max=100;
int randomNum = rand.nextInt(max - min + 1) + min;
System.out.println(randomNum);
return randomNum;
}
public void random50 (Integer integer){
Random rand = new Random();;
int min=0, max=50;
int randomNum = rand.nextInt(max - min +1) + min;
System.out.println(randomNum);
}
public static void main(String[] args) {
ranGen process = new ranGen();
if(process.random(null) > 50){
process.random50(null);
}
}
}
The goal of the homework, it seems, is to make you learn what method parameters are. The first step is to write a method that returns a random integer in the range of 1 to 100. So this method doesn’t have any parameter. Its signature should be
The second step is to write a method which takes a parameter specifying the top number. Its signature should thus be:
The third step is to write a method taking two values as parameter: the bottom and the top value. Its signature should thus be:
The last step is a bit more tricky. It asks you to remember the values a method has already generated, and to avoid generating them again. There is no parameter. Its signature should be
and its implementation should basically do the following:
Of course, the set of already generated numbers is empty at the beginning.
Note that you should use
intrather thanInteger. There’s no reason to accept null as argument, and the methods should never return null. So the primitiveinttype should be preferred.Also note that the methods should not print the generated number, but
returnit. So they should haveintas their return type, and notvoid.And classes, by convention, start with an upper-case letter and don’t abbriviate words. So your class should be named
RandomGenerator.