In Java assume that an instance of the Random class called r

In Java, assume that an instance of the Random class, called rand, has already been created. What is the range of values that the variable num1 could have in each assignment statement below: a) int num1 = rand.nextInt(10) + 4; b) int num1 = rand.nextInt(25) – 35/8; c) int num1 = 17 – rand.nextInt(15) * 3; d) int num1 = rand.nextInt(250);

Solution

a) Range of values for int num1 = rand.nextInt(10) + 4; is from 4 to 14, because whatever the random value that is generated by the rand.nextInt(10) will be in between 0 to 10, but after adding 4 in each random values the range increases from 4 to 14.

b) Range of values for int num1 = rand.nextInt(25) – 35/8; is from -4 to 21, because whatever the random value that is generated by the rand.nextInt(25) will be in between 0 to 25, but after subtracting 4 in each random values the range increases from -4 to 21. Please note that 35/8=4 for integer.

c) Range of values for int num1 = 17 – rand.nextInt(15) * 3; is from -28 to +17, because whatever the random value that is generated by the rand.nextInt(15) will be in between 0 to 15, but after multiplying by 3 in each random values the range increases from 0 to 45. and when we sutract the range from 17 final range will be -28 to +17

d) Range of values for int num1 = rand.nextInt(250); is from 0 to 250.

In Java, assume that an instance of the Random class, called rand, has already been created. What is the range of values that the variable num1 could have in ea

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site