Question

Explain the code: Exercise 22: Write a program that creates three random double variables a, b,...

Explain the code:

  1. Exercise 22: Write a program that creates three random double variables a, b, and c and assigns them values between 0 and 1 using the Math.random( ) method mentioned in the preceding exercise. It then does all of the following:

It prints out the three values.

It prints "All are tiny" if all three values are less than 0.5.

It prints out "One is tiny" if exactly one of the three values is less than 0.5.

It prints out "Only two are tiny" if exactly two of the three values are less than 0.5.

it prints if “None is tiny” if none of the values are less than 0.5.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class ThreeRandom {

    public static void main(String[] args) {
        // Generate three random numbers
        double a = Math.random(), b = Math.random(), c = Math.random();
        // print those random numbers
        System.out.println("Numbers are " + a + ", " + b + " and " + c);
        
        if (a < 0.5 && b < 0.5 && c < 0.5) {    // If all numbers are less than 0.5
            System.out.println("All are tiny"); // print that all are tiny
        } else if((a < 0.5 && b < 0.5) || (b < 0.5 && c < 0.5) || (a < 0.5 && c < 0.5)) {   // if two of these numbers are less than 0.5
            System.out.println("Only two are tiny");    // print that two of them are tiny
        } else if(a < 0.5 || b < 0.5 || c < 0.5) {  // // if only one of these numbers are less than 0.5
            System.out.println("One is tiny");  // print that one of them is tiny
        } else {    // if none of them are less than 0.5
            System.out.println("None is tiny"); // print that none are tiny
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Explain the code: Exercise 22: Write a program that creates three random double variables a, b,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Variables lab Write a program that creates three variables: an int, a double, and a String. Put the value 113 into t...

    Variables lab Write a program that creates three variables: an int, a double, and a String. Put the value 113 into the first variable, the value .71828 into the second, and the value Computer Science" into the third. It does not matter what you call the variables... this time. Then, display the values of these three variables on the screen, one per line. This is room # 113 e is close to 2.71828 I am learning a bit about Computer...

  • 1- Write a program that creates a dictionary of the English words for all single digit...

    1- Write a program that creates a dictionary of the English words for all single digit numbers as follows: 1: 'one', 2: 'two', 3: 'three', etc. 2- Write a function called numConvert that receives the above dictionary and an integer number as parameters, and prints the English words for all the number's digits. For example, if the function receives 2311, it will print: two three one one 3- Test your program by adding a main function, which calls numConvert. 4-...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • IN JAVA 3 ZIPS, What Order?: Write a program named ZipOrder that the reads in three...

    IN JAVA 3 ZIPS, What Order?: Write a program named ZipOrder that the reads in three zip codes from standard input and prints to standard output one of three words: "ASCENDING", "DESCENDING", "UNSORTED". The zip codes are guaranteed to be distinct from each other. In particular: if each zip code read in is greater than to the previous one, "ASCENDING"  is printed. if each zip code read in is less than to the previous one, "DESCENDING" is printed. otherwise, "UNSORTED" is...

  • Java question Q1) Use the following code snippet which generates a random sized array with random...

    Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() {         int size = (int) (Math.random() * 10) + 1;         int[] array = new int [size];         for (int i = 0; i < array.length; i++) {             array[i] = (int) (Math.random() * 10 ) + 1;         }         return array;     } Assignment...

  • See the image below and write the code using C++, without using variables, only recursion. 3....

    See the image below and write the code using C++, without using variables, only recursion. 3. 5 a [DO NOT USE ANY VARIABLES Write a C++ function that takes two integer parameters (a and b) and prints a list of all the integers between a and b-1 (inclusive). one (2, 12) should print 2 34567 8 9 10 11. one (2, 13) should print 2 34567 8 9 10 11 12 b. [D NOT USE ANY VARIABLES Write a C++...

  • Exercise 2 Using javascript (visual studio code) (a) Write code that creates 5 item objects (records)...

    Exercise 2 Using javascript (visual studio code) (a) Write code that creates 5 item objects (records) (b) Place the five item objects into an array (c) Write a function that prints an array of your items it has one parameter an array of items (d) Write a function that counts items that match some attribute value. (e) Write a function that filters an array of items. It has three parameter an array of item, an attribute name and a value....

  • In Java. Write a program in a single file that: Main: Creates 10 random doubles, all...

    In Java. Write a program in a single file that: Main: Creates 10 random doubles, all between 1 and 11, Calls a class that writes 10 random doubles to a text file, one number per line. Calls a class that reads the text file and displays all the doubles and their sum accurate to two decimal places. There has to be three classes, main for create random numbers, one to write , one to read all in the same file...

  • Write a program to pack boxes with blobs. Write two classes and a driver program. A...

    Write a program to pack boxes with blobs. Write two classes and a driver program. A Blob class implements the following interface. (Defines the following methods.) public interface BlobInterface { /** getWeight accessor method. @return the weight of the blob as a double */ public double getWeight(); /** toString method @return a String showing the weight of the Blob */ public String toString(); } A Blob must be between 1 and 4 pounds, with fractional weights allowed. A default constructor...

  • In a file called First SecondNext.java, write a program that: • Asks the user to enter...

    In a file called First SecondNext.java, write a program that: • Asks the user to enter a string that contains at least five letters. It is OK if your program crashes when the user enters a string with length less than 5. • Stores that string in a variable called s. • Creates a variable called "first", and sets it equal to the first letter of s. • Creates a variable called "second", and sets it equal to the second...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT