Question: Create an application program called UseRandom.java Use the program Listing 3.2 seen at the end of this lab and your answers above to help you
Create an application program called UseRandom.java
Use the program Listing 3.2 seen at the end of this lab and your answers above to help you with creating the program below.
Use the random number generator to get an integer. Put it in a variable called num1. Print out the integer num1 with the words,
Here is the number from the random number generator
Use the random number generator to get a double, multiply it by 100, and put it in a variable called num2. Print out a nicely formatted message giving the value, along with the square root, cos, floor, and ceil of that value (Hint: see the Java API docs for the Math package, e.g. Math.sqrt() for the square root)
Again, use the random number generator to get an integer in the range from 1 to 10 and store this in an integer num3. Print out the positive integer num3 with the words,
Here is a positive number in the range of 1-10 (Hint: See Question 7 in Part A)
3. Multiply the integer num3 by 20,000, and store the results in a variable totalSeconds. Print out the totalSeconds with the word,
Here is the Total Number of Seconds
We now have the total number of seconds
Find out how many hours, how many minutes and how many seconds there are in totalSeconds. (hint: You can use division and/or the % operator or any other means you know. The Algorithm that you wrote will give you the method to get the hours before you ever have to code it in Java) Hint: Think about how many seconds in a minute and in an hour
Print out the hours, minutes, seconds with appropriate headings: NOTE: Seconds will be a number between 0 and 59. Minutes will be a number between 0 and 59.
Hours =
Minutes =
Seconds =
Print out your: Name, section number, and Project 5 in a box. You can copy the code from earlier projects and just change the project number. You can be more creative than I have been. See the example below
System.out.println (*************************************************);
System.out.println( * Name *);
System.out.println( * Class *);
System.out.println( * Project *);
System.out.println( *************************************************);
12 Remember, the answers to the 7 questions are put as comments into the program.
13. Compile and run and make certain that the program calculations are correct.
RANDOM METHODS
Random ( )
Constructor: creates a new pseudorandom number generator.
float nextFloat ( )
Returns a random number between 0.0 (inclusive) and 1.0 (exclusive).
int nextInt ( )
Returns a random number that ranges over all possible int values (positive and negative).
int nextInt (int num)
Returns a random number in the range 0 to num-1.
NOTE: see the Java API docs for more
Listing 3.2 page 127&128
//********************************************************************
// RandomNumbers.java Author: Lewis/Loftus
// Demonstrates the creation of pseudo-random numbers using the
// Random class.
//********************************************************************
import java.util.Random;
public class RandomNumbers
{
//-----------------------------------------------------------------
// Generates random numbers in various ranges.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Random generator = new Random();
int num1;
float num2;
num1 = generator.nextInt();
System.out.println ("A random integer: " + num1);
num1 = generator.nextInt(10);
System.out.println ("From 0 to 9: " + num1);
num1 = generator.nextInt(10) + 1;
System.out.println ("From 1 to 10: " + num1);
num1 = generator.nextInt(15) + 20;
System.out.println ("From 20 to 34: " + num1);
num1 = generator.nextInt(20) - 10;
System.out.println ("From -10 to 9: " + num1);
num2 = generator.nextFloat();
System.out.println ("A random float (between 0-1): " + num2);
num2 = generator.nextFloat() * 6; // 0.0 to 5.999999
num1 = (int)num2 + 1;
System.out.println ("From 1 to 6: " + num1);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
