This program will use the Random class to generate random temperatures (integers) between some minimum (exclusive) and some maximum (exclusive) values. You will need to calculate the minimum and maximum given a starting temperature (integer) and a possible change in temperature (integer).
(1) Finish implementing the getTemp method in the TemperaturePredictor class according to the description in the method header comments.
(2) Complete the main method following the instructions within the method.
The output for the first three calls should be:
Temperature: 5 F
Temperature: 4 F
Temperature: 4 F
For testing purposes, we require being able to predict the ‘random’ results. Setting a seed for the random number generator accomplishes this because it allows for a pseudo-random number to be generated in a predictable order. For example, a seed of 85 for range of [3 to 9] may result in 4, 4, 9 for the first three values, while a seed of 2 for the same range may result in 5, 3, 4 for the first three values. With everyone using the same seed value, we are able to determine if your code produces the correct ‘random’ values. Without setting the seed, the results of the random number generator will always be different, making it impossible to verify that your code works. As discussed above in (2), Config.SEED is a constant named SEED declared in the Config.java file. Don’t use the number in your RandomGenerator program, just use this constant.
Note:
(1) A NoSuchElementException is a common error that indicates that your program is trying to read in an element from the scanner when no such value exists. Refer to the line shown in the error message to debug.
(2) The last automated test case below checks to ensure at least two additional unique tests have been added. Use integer literals as the arguments, not variables. Make sure there is no whitespace between the method name and parentheses to pass this unit test. For example: getTemp(rand, -10, 5) is valid; however, getTemp (rand, -10, 5) is invalid due to the whitespace.
import java.util.Random;
public class TemperaturePredictor {
/**
* Generates a random temperature (int) within a range when given the current temperature
* and the possible temperature change.
*
* Example: for a temperature of 5 and a change of 2, the method should produce a
* random temperature between 3 and 7 inclusive.
*
* @param rand The random number generator
* @param temperature The current temperature
* @param changeInTemp The possible change in temperature
* @return A random value between the temperature – changeInTemp and temperature + changeInTemp, inclusive.
*/
public static int getTemp(Random rand, int temperature, int changeInTemp) {
// int temp = temperature
//FILL IN BODY
//Do not create an instance of Random within this method. Utilize the Random instance in the parameter.
return rand.nextInt(((temperature + changeInTemp) – (temperature – changeInTemp)) + 1) + (temperature – changeInTemp); //replace with returning the correct value
}
/**
* The creates a random number generator and demonstrates calling getTemp passing the same arguments
* 3 times and then different arguments another 2 times.
*
* @param args unused
*/
public static void main(String[] args) {
//Create a single instance of Random with a seed of Config.SEED.
Random rand = new Random(SEED);
//Uncomment the following and modify the following 3 statements to use the same instance of
//the random number generator. For all 3 calls to getTemp pass a temperature of 5 and
//a change in temperature of 2.
System.out.println(“Temperature: ” + getTemp(rand, 65, 10) + ” F”);
System.out.println(“Temperature: ” + getTemp(rand, 65, 10) + ” F”);
System.out.println(“Temperature: ” + getTemp(rand, 65, 10) + ” F”);
// Additional tests: ***DO NOT REMOVE THIS LINE***
//Add additional calls to getTemp, each on their own line, with different int literal (not variable)
//arguments.
}
}