The question requires you to have completed the Random program (page 13). Modify
ID: 3748426 • Letter: T
Question
The question requires you to have completed the Random program (page 13).
Modify the body of the getNumber method so it looks like the following.
return (int)(2+3*Math.random());
/ Output a random integer [0,91 and its square public class Random Class name (public static void main (String args) int numbergetNumber int result = squareNumber (number); show (number, result); Methods public static int getNumber f return (int) (10 java.lang.Math.random)) Class body public static int squareNumber (int number) return (int)Math.pow (number, 2); public static void show (int number, int result) t System.out.println ("Random Numberlt" +number"") System.out.println ("Number Squared " +result +"")Explanation / Answer
The value of the random number generated by the updated program will be always 2, 3 or 4.
Since , the java.lang.Math.random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
if java.lang.Math.random() falls in range [0,1/3) then (int)(2+3*Math.random()) will be 2.
if java.lang.Math.random() equals 1/3 then (int)(2+3*Math.random()) will be 3.
if java.lang.Math.random() falls in range (1/3,1) then (int)(2+3*Math.random()) will be 4.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.