Question

In: Computer Science

Write a program in java which randomly generates two integer number n1 and n2 (suppose the...

Write a program in java which randomly generates two integer number n1 and n2 (suppose the range for each integer is [1, 100]), then asks the user what is the value of n1*n2, if the user’s answer is correct, call method printGoodComment to print out something nice, otherwise, call printBadComment to print out something “mean”.

The method signatures are:

                  public static void printGoodComment()

and

                  public static void printBadComment()

in your printGoodComment method, it will randomly print one sentence from the following lists:

                              good job

                              excellent

                              terrific

                              nice work

in your printBadComment method, it will randomly print one sentence from the following lists:

                              sorry, try next time

                              oops, you need more work

                              hmm, it is not correct

Hint: use Math.random and a switch statement in the printGoodComment and printBadComment methods.

  • In the printGoodComment method, use Math.random( ) to generate a random int in the rage of [0, 3] and use a switch statement to select one of the four good moments based on the value of the random int.
  • In the printBadComment method, use Math.random( ) to generate a random int in the rage of [0, 2] and use a switch statement to select one of the three bad moments based on the value of the random int.

Solutions

Expert Solution

The solution to the above question is:-

code for above program is:-

import java.util.*;
import java.lang.Math; //used to import Math library.
class Random //name of the class.
{
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in); //object used for taking input.
       int max=100; // maximum number for generation of the random number.
       int min=1; // minimum number for generation of the random number.
       int range = max-min+1; // range which is used for generating random numbers between the max and min terms.
       int n1 = (int)(Math.random()*range)+min; // generating the 1st random number.
       int n2 =(int)(Math.random()*range)+min; // generating the 2nd random number.
       //System.out.println("rand 1:"+n1);
       //System.out.println("rand 2:"+n2);
       System.out.println("Enter your guess answer:");
       int guess = sc.nextInt(); // taking the guess from the user.
       if(guess == n1*n2) // checking the condition wheather your guess is correct or not.
       {
           printGoodComment(); // if guess is correct calling printGoodComment() method.
       }
       else
       {
           printBadComment(); // if guess is wrong calling printBadComment() method.
       }
      
   }
   public static void printGoodComment()
   {
       int min=0; // defining minimum range
       int max=3; // defining maximum range.
       int range = max-min+1; // taking range.
       int n1= (int)(Math.random()*range)+min; // generating random number within the range.
       switch(n1) //using switch to generate the output randomly.
       {
           case 0:
               System.out.println("good job");
               break;
           case 1:
               System.out.println("Excellent");
               break;
           case 2:
               System.out.println("terrific");
               break;
           case 3:
               System.out.println("nice work");
               break;
       }
   }
   public static void printBadComment()
   {
       int min=0;                         // defining the minimum range.
       int max=2;                                    // defining the maximum range.
       int range = max-min+1;                       // setting the range.
       int n1= (int)(Math.random()*range)+min; // generating the random number.
       switch(n1) // using the switch to generate output.
       {
           case 0:
               System.out.println("sorry,try next time ");
               break;
           case 1:
               System.out.println("oops,you need more work");
               break;
           case 2:
               System.out.println("hmm, it is not correct");
               break;
       }
   }
}

output for above code is:-


   Thank you.


Related Solutions

Write a program in java which randomly generates two integer number n1 and n2 (suppose the...
Write a program in java which randomly generates two integer number n1 and n2 (suppose the range for each integer is [1, 100]), then asks the user what is the value of n1*n2, if the user’s answer is correct, call method printGoodComment to print out something nice, otherwise, call printBadComment to print out something “mean”. The method signatures are:                   public static void printGoodComment() and                   public static void printBadComment() in your printGoodComment method, it will randomly print one sentence from the...
1- Write a Java program called ArabicMonth that randomly generates an integer between 1 and 12...
1- Write a Java program called ArabicMonth that randomly generates an integer between 1 and 12 and displays the month name Jan, Feb, …, December for the number 1, 2, …, 12, accordingly.. Sample Run: 9 Oct 2- Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars to Saudi Riyals. Prompt the user to enter 0 to convert from U.S. dollars to Saudi Riyals and 1 to convert from Saudi Riyal and...
Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can choose the next input intelligently. Here is a sample run:
Write a java program which can randomly generate a permutation of the integer {1, 2, 3,...
Write a java program which can randomly generate a permutation of the integer {1, 2, 3, ..., 48,49,50}. Use the most efficient sorting algorithm to sort the list in an acceding order.
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...
Write a program in C which randomly generates two integers X and Y between 10 and...
Write a program in C which randomly generates two integers X and Y between 10 and 50, and then creates a dynamic array which can hold as many numbers as there are between those two random numbers. Fill the array with numbers between X and Y and print it on screen. Do not forget to free the allocated memory locations whenever they are no longer needed. Example: Randomly generated numbers are: 43 23 Expected Output : 23 24 25 26...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime =...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT