Question

In: Computer Science

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 ≤ n ≤ 1112

e) –1 ≤ n ≤ 1

f) –3 ≤ n ≤ 11

Put these statements in Java program and run it.

3. Write a complete Java application to prompt the user for the double radius of a sphere, and

call method sphereVolume to calculate and display the volume of the sphere. Use the following statement

to calculate the volume:

double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 )

Solutions

Expert Solution

import java.util.*;

class Hello {

    public static void main(String[] args) {

        Random r = new Random();

        // r.ints(x,y+1).findFirst().getAsInt() will return a random integer in the

        // range [x,y]

        int a = r.ints(1, (3 + 1)).findFirst().getAsInt();

        // a will get a random value in the range [1,3]

        int b = r.ints(1, (200 + 1)).findFirst().getAsInt();

        // b will get a random value in the range [1,3]

        int c = r.ints(0, (9 + 1)).findFirst().getAsInt();

        // c will get a random value in the range [1,3]

        int d = r.ints(1000, (2112 + 1)).findFirst().getAsInt();

        // d will get a random value in the range [1,3]

        int e = r.ints(-1, (5 + 1)).findFirst().getAsInt();

        // e will get a random value in the range [1,3]

        System.out.println(a);

        System.out.println(b);

        System.out.println(c);

        System.out.println(d);

        System.out.println(e);

    }

}

output:


Related Solutions

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
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
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...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
For this assignment, write a program that will generate three randomly sized sets of random numbers...
For this assignment, write a program that will generate three randomly sized sets of random numbers using DEV C++ To use the random number generator, first add a #include statement for the cstdlib library to the top of the program: #include <cstdlib> Next, initialize the random number generator. This is done by calling the srand function and passing in an integer value (known as a seed value). This should only be done ONE time and it MUST be done before...
Write a simple java program to list roman numeral for a given range of numbers. Roman...
Write a simple java program to list roman numeral for a given range of numbers. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M. I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 Roman numerals are usually written largest to smallest from left to right. But a number like 4 is not written as IIII. It is written as IV. Because...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
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...
For this problem, you will write a program using two queues. Generate n random numbers between...
For this problem, you will write a program using two queues. Generate n random numbers between 10 and 100 (both inclusive), where n>9. The value of n should be taken as input from the user and n should be >9. The numbers could be duplicated. Enqueue all these numbers to the first queue. The objective is to find the numbers whose sum of digits is odd and enqueue them to the second queue. The remaining numbers (whose sum of digits...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT