In: Computer Science
Write program#2 upload .java file.
2A) Write a java program that uses the Random class to generate a number in the range 21 to 64. Print the generated number.
2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value.
2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two divided by integer seven, print the result formated to ten decimal places. Hint: the result of the division needs data conversion to support a double.
<END>
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Question 2A :
//import package
import java.util.Random;
//Java class
public class RandomNumber {
//entry point of the program ,main() method
public static void main(String[] args) {
//creating object of Random
class
Random random=new Random();
//generate random number between 21
to 64
int randomNumber=random.nextInt(64
- 21 + 1) + 21;
//print the random number
System.out.println("Random number
between 21 to 64 :"+randomNumber);
}
}
================
Output Screen :RandomNumber.java
****************************
Question 2B:
DieRoll.java :
//import package
import java.util.Random;
//Java class
public class DieRoll {
// entry point of the program ,main() method
public static void main(String[] args) {
// creating object of Random
class
Random random = new Random();
// generate random numbers
int randomNumber1 =
random.nextInt(6 - 1 + 1) + 1;
int randomNumber2 =
random.nextInt(6 - 1 + 1) + 1;
// add value
int totalValue = randomNumber1 +
randomNumber2;
// print random numbers
System.out.println("Value for die
1:" + randomNumber1);
System.out.println("Value for die
2:" + randomNumber2);
// print total value
System.out.println("Total Value :"
+ totalValue);
}
}
=======================
Screen output :DieRoll.java
********************************
Question 2C:
squareRoot.java :
//Java class
public class squareRoot {
//Entry point of the class , main method
public static void main(String[] args) {
//calculate square root
double num=Math.sqrt(22/7);
//print square root of a
number
System.out.printf("Square Root of
22/7 is : %.10f", num);
}
}
====================
Output :squareRoot.java
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.