Question

In: Computer Science

Write a JAVA program that generates three random numbers from 1 to 6, simulating a role...

Write a JAVA program that generates three random numbers from 1 to 6, simulating a role of three dice. It will then add, subtract and multiply these two numbers. It will also take the first number to the power of the second and that result to the power of the third. Display the results for each calculation. Please note that the sample run is based on randomly generated numbers so your results will be different.

Sample run:

6 + 2 + 5 = 13

6 - 2 – 5 = -1

6 * 2 * 5 = 60

6 to the power of 2 to the power of 5 = 60,466,176

A second sample run:

2 + 3 + 5 = 10

2 – 3 - 5 = -6

2 * 3 * 5 = 30

2 to the power of 3 to the power of 5 = 32,768

Solutions

Expert Solution

Program code to copy:-

import java.util.Random;

import java.lang.Math;

public class RandomNumbersMath

{

      /**

      * This program will generates three random numbers from 1 to 6.

      * It will then add, subtract and multiply these numbers.

      * It will also take the first number to the power of the second and that result to the power of the third.

      * Display the results for each calculation.

      */

      public static void main(String[] args)

      {

            // TODO Auto-generated method stub

            int[] num = new int[3];           // Declaring array of 3 integer

          Random r = new Random();         //Creating an object of random class

           

          for (int i = 0; i < 3; i++)

          num[i] = r.nextInt(6)+1;   // storing random integer number in an array

              int add = num[0] + num[1] + num[2] ;    //adding the numbers

              int sub = num[0] - num[1] - num[2] ;    //Subtracting the numbers

              int mul = num[0] * num[1] * num[2] ;   //Multiply the numbers

              //Calculate the first number to the power of the second and that result to the power of the third.

              //By default Math.pow() function returns double type so casting is done to convert double into int

              int power = (int) Math.pow(Math.pow(num[0], num[1]), num[2]);  

              //Printing desired output of calculation done for add, subtract and multiply

              System.out.println(num[0] + " + " + num[1] + " + " + num[2] + " = " + add);

              System.out.println(num[0] + " - " + num[1] + " - " + num[2] + " = " + sub);

              System.out.println(num[0] + " * " + num[1] + " * " + num[2] + " = " + mul);

             

              //Printing power of numbers with thousand separator by using String.format() function

              System.out.println(num[0] + " to the power of " + num[1] + " to the power of " + num[2] + " = " + String.format("%,d", power));

      }

}

Sample output:-


Related Solutions

3. Write a Java program that generates a set of random strings from a given string...
3. Write a Java program that generates a set of random strings from a given string of same length where no character is ever repeated and characters belong to the original string. Examples Input: “Hello World” Output: “World oHlel”
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers...
In Assembly Language MASM Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the...
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the following three methods in the class RandomFile: ​public static void randomBinaryFile(String fileName, int length) ​public static void randomNumberFile(String fileName, intlength) ​public static void randomCharFile(String fileName, int length) The parameter “fileName” specifies the file name, and “length” specifies the length of the sequence in the file. ● randomBinaryFile will generate a file containing a sequence of 0 or 1 of the specified length; ● randomNumberFile...
Write a program in PERL language that does the following: a. Generates 100 random numbers between...
Write a program in PERL language that does the following: a. Generates 100 random numbers between -17 and +31. b. Calculates the average of the square of the generated numbers using a function that you implement. c. Calculates the number of numbers greater than the average using a function that you implement. d. Prints the results single line separated by spaces using a print function that makes call to the previously defined functions in parts b) and c).
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT