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

/***************************************Assignment6.java************************/

import java.util.Random;
import java.util.Scanner;

public class Assignment6 {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);
       Random r = new Random();
       char option = ' ';
       do {
           int n1 = r.nextInt(100) + 1;
           int n2 = r.nextInt(100) + 1;

           System.out.print("What is the value of " + n1 + "*" + n2 + ": ");
           int result = scan.nextInt();
           scan.nextLine();
           if (result == n1 * n2) {

               printGoodComment();
           } else {

               printBadComment();
           }

           System.out.print("Do you want to continue(y/n): ");
           option = scan.nextLine().toLowerCase().charAt(0);
       } while (option != 'n');
       scan.close();
   }

   public static void printGoodComment() {

       Random r = new Random();
       int num = r.nextInt(3);
       switch (num) {
       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() {

       Random r = new Random();
       int num = r.nextInt(2);
       switch (num) {
       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************************/

What is the value of 69*63: 2345
Sorry, try next time!
Do you want to continue(y/n): y
What is the value of 20*9: 180
Good job!
Do you want to continue(y/n): y
What is the value of 30*8: 240
Terrific!
Do you want to continue(y/n): y
What is the value of 98*60: 987
OOPs, you need more work!
Do you want to continue(y/n): n

Please let me know if you have any doubt or modify the answer, Thanks :)


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