Question

The following is the class definition for Multiplier. This class has five (5) members: two private...

The following is the class definition for Multiplier. This class has five (5) members:

  • two private instance variables
  • two private methods
  • one public method
    • this is only public method for the class
    • it calls other two private methods

import java.util.*; //We need to use Scanner & Random in this package

public class Multiplier {

       private int answer; //for holding the correct answer

       private Random randomNumbers = new Random(); //for creating random numbers

       //ask the user to work on multiplication problems, a starter

       public void quiz() {

              int guess; //for holding the user's guess

              Scanner input = new Scanner(System.in);

              createQuestion();

              do {

                     System.out.println("Enter your answer or -1 to exit: ");

                     guess = input.nextInt();

                     if (guess == -1) {

                           System.out.println("You have decided to exit!");

                           break;

                     }

                     checkResponse(guess);

              } while (guess != answer);

       } //end quiz method

       //print a new question and store the corresponding answer

       private void createQuestion() {

              //get two random numbers between 0 and 9, inclusively

              int digit1 = randomNumbers.nextInt(10);

              int digit2 = randomNumbers.nextInt(10);

              answer = digit1 * digit2;

              System.out.printf("How much is %d times %d?\n", digit1, digit2);

       } //end createQuestion method

       //check if the user has answered correctly

       private void checkResponse(int guess) {

              if (guess == answer) {

                     System.out.println("Very good!\n");

                     createQuestion();

              }

              else {

                     System.out.println("Wrong answer! Try again!");

              }

       } //end checkResponse method

} //end class Multiplier


Write a Java application to invoke the quiz defined in this class. Note: please write the program in a compact form since there are only six text lines in the answer area.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM :

Multiplier.java

import java.util.*;

public class Multiplier {

private int answer;

private Random randomNumbers = new Random();

public void quiz(){

int guess;

Scanner input = new Scanner(System.in);

createQuestion();

do{

System.out.println("Enter your answer or -1 to exit: ");

guess = input.nextInt();

if(guess==-1){

System.out.println("You have decided to exit!");

break;

}

checkResponse(guess);

}while(guess!=answer);

}

private void createQuestion(){

int digit1 = randomNumbers.nextInt(10);

int digit2 = randomNumbers.nextInt(10);

answer = digit1 * digit2;

System.out.printf("How much is %d times %d?\n", digit1, digit2);

}

private void checkResponse(int guess){

if(guess==answer){

System.out.println("Very good!\n");

createQuestion();

}

else{

System.out.println("Wrong answer! Try again!");

}

}

}

MultiplierDemo.java

public class MultiplierDemo {

public static void main(String[] args) {

Multiplier mul = new Multiplier();

mul.quiz();

}

}

OUTPUT :

How much is 8 times 6? Enter your answer or -1 to exit: 48 Very good! How much is 9 times 6? Enter your answer or -1 to exit:

Add a comment
Know the answer?
Add Answer to:
The following is the class definition for Multiplier. This class has five (5) members: two private...
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
  • Within DrJava, create a class called StudentQuizScores and do the following using a do-while loop. 1....

    Within DrJava, create a class called StudentQuizScores and do the following using a do-while loop. 1. You program will read in a student’s first name. The same will be done for the student’s last name. Your program will use respective methods (described below) to accomplish this. 2. Your program will then read in three quiz scores, respectively. This should be done by using the same method three times. This method is described below. 3. Your program will then calculate the...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • 02. Second Assignment – The FacebookUser Class We’re going to make a small change to the...

    02. Second Assignment – The FacebookUser Class We’re going to make a small change to the UserAccount class from the last assignment by adding a new method: public abstract void getPasswordHelp(); This method is abstract because different types of user accounts may provide different types of help, such as providing a password hint that was entered by the user when the account was created or initiating a process to reset the password. Next we will create a subclass of UserAccount...

  • Examine the following class definition: public class Date private int year; private int month; private int...

    Examine the following class definition: public class Date private int year; private int month; private int day; public Date() { ...) public void set (int x, int y, int z) { ...) public int getYear() { ...) // returns year public int getMonth() { } // returns month public int get Day () { ...) // returns day //more methods here -- 1 Which of the following statements in a client program correctly prints out the day of the object...

  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

  • This is a basic Java Question referencing Chapter 5 methods. The goal is to make a...

    This is a basic Java Question referencing Chapter 5 methods. The goal is to make a quick program that has the computer guess a number, after which the user will input if they would like to play the easy, medium or hard level. After this point the user will be allowed to guess a certain amount of times, before the computer tells them they are correct, incorrect, and gives them the guessed number. The Question is as follows: Define a...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • public class Pet {    //Declaring instance variables    private String name;    private String species;...

    public class Pet {    //Declaring instance variables    private String name;    private String species;    private String parent;    private String birthday;    //Zero argumented constructor    public Pet() {    }    //Parameterized constructor    public Pet(String name, String species, String parent, String birthday) {        this.name = name;        this.species = species;        this.parent = parent;        this.birthday = birthday;    }    // getters and setters    public String getName() {        return name;   ...

  • I wrote two java classes. One is main and other one is getter and setter. I...

    I wrote two java classes. One is main and other one is getter and setter. I brought methods from Digit class to Main class to print result. However, for some reason, only welcome print statement got printed and others got ignored. On the output screen, only welcome statement appear. nDigit should be input from user. import java.util.Scanner; public class Main {    public static void main(String[] args)    {        Digit digitGet = new Digit();              ...

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