Question

Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:

Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:

  • Addition

  • Subtraction

  • Multiplication

  • Division

  • Modulo

  • Exponent

The following source files are already complete and CANNOT be changed:

  • MathQuiz.java (contains the main() method)

  • OperationType.java (defines an enumeration for valid math operations and related functionality)

The following source files are incomplete and need to be coded to meet the indicated requirements:

  • MathQuestionable.java

    • Random object called RANDOM has already been declared and assigned, and you must not change this assignment in any way.

    • Declare and assign an interface integer called MAX_SMALL and assign it the value 25.

    • Declare and assign an interface integer called MAX_LARGE and assign it the value 50.

    • Declare and assign an interface integer called MAX_MULTIPLE and assign it the value 12.

    • Declare and assign an interface integer called MAX_BASE and assign it the value 12.

    • Declare and assign an interface integer called MAX_EXPONENT and assign it the value 4.

    • Declare (but do not define) the method getQuestion() that has an empty parameter list and returns a String.

    • Declare (but do not define) the method getCorrectAnswer() that has an empty parameter list and returns an integer.

    • MathQuestionable is an interface, and you will need to define each of the following elements to make them available to all classes that implement the interface (in this case, anything that is a MathQuestion):

  • MathQuestion.java

    • Private member integer constants x and y.

    • A no-argument (default) constructor that assigns x to RANDOM.nextInt(MAX_LARGE) + 1 and y to RANDOM.nextInt(MAX_SMALL) + 1.

    • A parameterized constructor that accepts two integer values and assigns the first to x and the second to y.

    • Appropriate public accessor methods getX() and getY() which simply return values of x and y respectively.

    • MathQuestion is an abstract class, and the required derived classes (AdditionQuestion, DivisionQuestion, ExponentQuestion, ModuloQuestion, MultiplicationQuestion, and SubtractionQuestion) already have starting code defined within the MathQuestion source file. A public method to generate a math question as a string (getQuestion()) has already been defined in the MathQuestion class.

    • The MathQuestion class must define the following:

    • Each of the derived classes (except for AdditionQuestion) has private static integer variables declared and set to the required randomly generated values (_x and _y). Additionally, you will need to create the appropriate constructors and methods as indicated in the TODO comments contained within each class.

The following inputs will be used to test your solution:

15 7 30 48 45 0 2 3 8 1

This should result in a score of 100

14 6 31 47 43 1 3 5 9 2

This should result in a score of 0

15 8 30 49 45 0 3 4 8 1

This should result in a score of 60

import java.util.Scanner;

public class MathQuiz {
    private final int NUMBER_OF_QUESTIONS = 10;
    private final MathQuestion[] questions = new MathQuestion[NUMBER_OF_QUESTIONS];
    private final int[] userAnswers = new int[NUMBER_OF_QUESTIONS];

    public static void main(String[] args) {
        MathQuiz app = new MathQuiz();
        System.out.println(app.welcomeAndInstructions());
        app.createQuiz();
        app.administerQuiz();
        app.gradeQuiz();
    }

    private String welcomeAndInstructions() {
        return "Welcome to Math Quiz!\n" +
               "There are currently six types of math questions possible:\n" +
               "Addition, Subtraction, Multiplication, Exponents, Division, and Modulo.\n" +
               "The answer to each question will always be an integer.\n\n" +
               "Good luck!\n";
    }

    private void createQuiz() {
        int i = 0;
        OperationType op;

        while (i < NUMBER_OF_QUESTIONS) {
            op = OperationType.getRandom();

            switch (op) {
                case ADD:
                    questions[i++] = new AdditionQuestion();
                    break;
                case SUBTRACT:
                    questions[i++] = new SubtractionQuestion();
                    break;
                case MULTIPLY:
                    questions[i++] = new MultiplicationQuestion();
                    break;
                case EXPONENT:
                    questions[i++] = new ExponentQuestion();
                    break;
                case DIVIDE:
                    questions[i++] = new DivisionQuestion();
                    break;
                case MODULO:
                    questions[i++] = new ModuloQuestion();
                    break;
                default:
                    throw new IllegalArgumentException();
            }
        }
    }

    private void administerQuiz() {
        Scanner scanner = new Scanner(System.in);
        int questionNum = 1;
        for (MathQuestion q : questions) { //POLYMORPHISM!!
            System.out.printf("Question %2d:  %s ", questionNum, q.getQuestion());
            userAnswers[questionNum - 1] = scanner.nextInt();
            questionNum++;
        }
    }

    private void gradeQuiz() {
        int numberCorrect = 0;
        String question;
        int answer;

        System.out.println("\nHere are the correct answers:\n");

        for (int i = 0; i < NUMBER_OF_QUESTIONS; i++) {
            question = questions[i].getQuestion();
            answer = questions[i].getCorrectAnswer();
            System.out.printf("Question number %d:\n    %s\n", i + 1, question);
            System.out.printf("    Correct answer:  %d\n", answer);
            if (userAnswers[i] == answer) {
                System.out.println("    You were CORRECT.");
                numberCorrect++;
            } else {
                System.out.printf("    You said %d, which is INCORRECT.\n", userAnswers[i]);
            }
        }

        System.out.printf("\nYou got %d questions correct.\n", numberCorrect);
        System.out.printf("Your grade on the quiz is %d\n", (numberCorrect * 10));
    }
}
/*
 The current errors in this class (except for the ones on line 22) will be fixed once you
 complete the MathQuestionable interface. Complete MathQuestionable before doing any further 
 work on MathQuestion or the derived classes. Remove this comment before making your final commit
*/

public abstract class MathQuestion implements MathQuestionable {
    // TODO: Declare private member integer constants x and y

    // TODO: Define a default (no parameters) constructor that assigns
    // x to RANDOM.nextInt(MAX_LARGE) + 1
    // y to RANDOM.nextInt(MAX_SMALL) + 1

    // TODO: Define a parameterized constructor that accepts two integer values
    // The first parameter must be assigned to x
    // The second parameter must be assigned to y

    // TODO: Define public accessor methods getX() and getY()


    public String getQuestion(OperationType op) {
        return "What is " + x + ' ' + op.getSymbol() + ' ' + y + '?';
    }

}

class AdditionQuestion extends MathQuestion {
    // A video walk-through will be provided for how to write the code for this class
}

class DivisionQuestion extends MathQuestion {
    private static int _x = RANDOM.nextInt(MAX_LARGE) + 1;
    private static int _y = RANDOM.nextInt(_x) + 1;

    // TODO: Define default constructor to call parent class constructor and passing _x and _y as arguments
    // You will also need to reassign new random values to both _x and _y as shown in the initial assignment statements

    // TODO: Define a parameterized constructor that accepts two integers and passes these in a call to the parent constructor

    // TODO: Define getQuestion that returns a String and makes a call to the parent getQuestion method.
    // You will need to pass OperationType.DIVIDE as the argument

    // TODO: Define getCorrectAnswer that returns an integer result of x / y (this is not as simple as it looks!)

}

class ExponentQuestion extends MathQuestion {
    private static int _x = RANDOM.nextInt(MAX_BASE) + 1;
    private static int _y = RANDOM.nextInt(MAX_EXPONENT);

    // TODO: Define default constructor to call parent class constructor and passing _x and _y as arguments
    // You will also need to reassign new random values to both _x and _y as shown in the initial assignment statements

    // TODO: Define a parameterized constructor that accepts two integers and passes these in a call to the parent constructor

    // TODO: Define getQuestion that returns a String and makes a call to the parent getQuestion method.
    // You will need to pass OperationType.EXPONENT as the argument

    // TODO: Define getCorrectAnswer that returns an integer result of Math.pow(x, y) (this is not as simple as it looks!)

}

class ModuloQuestion extends MathQuestion {
    private static int _x = RANDOM.nextInt(MAX_LARGE) + 1;
    private static int _y = RANDOM.nextInt(_x) + 1;

    // TODO: Define default constructor to call parent class constructor and passing _x and _y as arguments
    // You will also need to reassign new random values to both _x and _y as shown in the initial assignment statements

    // TODO: Define a parameterized constructor that accepts two integers and passes these in a call to the parent constructor

    // TODO: Define getQuestion that returns a String and makes a call to the parent getQuestion method.
    // You will need to pass OperationType.MODULO as the argument

    // TODO: Define getCorrectAnswer that returns an integer result of x % y (this is not as simple as it looks!)
}

class MultiplicationQuestion extends MathQuestion {
    private static int _x = RANDOM.nextInt(MAX_MULTIPLE) + 1;
    private static int _y = RANDOM.nextInt(MAX_MULTIPLE) + 1;

    // TODO: Define default constructor to call parent class constructor and passing _x and _y as arguments
    // You will also need to reassign new random values to both _x and _y as shown in the initial assignment statements

    // TODO: Define a parameterized constructor that accepts two integers and passes these in a call to the parent constructor

    // TODO: Define getQuestion that returns a String and makes a call to the parent getQuestion method.
    // You will need to pass OperationType.MULTIPLY as the argument

    // TODO: Define getCorrectAnswer that returns an integer result of x * y (this is not as simple as it looks!)

}

class SubtractionQuestion extends MathQuestion {
    private static int _x = RANDOM.nextInt(MAX_SMALL) + MAX_SMALL + 1;
    private static int _y = RANDOM.nextInt(MAX_SMALL) + 1;

    // TODO: Define default constructor to call parent class constructor and passing _x and _y as arguments
    // You will also need to reassign new random values to both _x and _y as shown in the initial assignment statements

    // TODO: Define a parameterized constructor that accepts two integers and passes these in a call to the parent constructor

    // TODO: Define getQuestion that returns a String and makes a call to the parent getQuestion method.
    // You will need to pass OperationType.SUBTRACT as the argument

    // TODO: Define getCorrectAnswer that returns an integer result of x - y (this is not as simple as it looks!)

}
import java.util.Random;

public interface MathQuestionable {
    Random RANDOM = new Random(5); //DO NOT CHANGE THIS LINE
    
   // TODO: Add properties and method headers

}
import java.util.Random;

public enum OperationType {
    ADD, SUBTRACT, MULTIPLY, EXPONENT, DIVIDE, MODULO;

    private static final Random RANDOM = new Random(5);

    public static OperationType getRandom() {
        OperationType[] values = OperationType.values();
        int length = values.length;
        return values[RANDOM.nextInt(length)];
    }

    public char getSymbol() {
        switch (this) {
            case ADD:
                return '+';
            case SUBTRACT:
                return '-';
            case MULTIPLY:
                return '*';
            case EXPONENT:
                return '^';
            case DIVIDE:
                return '/';
            case MODULO:
                return '%';
            default:
                throw new IllegalArgumentException();
        }
    }
}


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

Request Answer!

We need at least 9 more requests to produce the answer.

1 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:

    Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:AdditionSubtractionMultiplicationDivisionModuloExponentThe following source files are already complete and CANNOT be changed:MathQuiz.java (contains the main() method)OperationType.java (defines an enumeration for valid math operations and related functionality)The following source files are incomplete and need to be coded to meet the indicated requirements:MathQuestionable.javaA Random object called RANDOM has already been declared and assigned, and you must not change this assignment in any way.Declare and assign an interface integer called MAX_SMALL and assign it the...

  • Adv. Java program - create a math quiz

    Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:AdditionSubtractionMultiplicationDivisionModuloExponentThe following source files are already complete and CANNOT be changed:MathQuiz.java (contains the main() method)OperationType.java (defines an enumeration for valid math operations and related functionality)The following source files are incomplete and need to be coded to meet the indicated requirements:MathQuestionable.javaA Random object called RANDOM has already been declared and assigned, and you must not change this assignment in any way.Declare and assign an interface integer called MAX_SMALL and assign it the...

  • Java Programming: Math Quiz

    Starting codes:MathQuiz.javaimport java.util.Scanner;public class MathQuiz {    private final int NUMBER_OF_QUESTIONS = 10;    private final MathQuestion[] questions = new MathQuestion[NUMBER_OF_QUESTIONS];    private final int[] userAnswers = new int[NUMBER_OF_QUESTIONS];    public static void main(String[] args) {        MathQuiz app = new MathQuiz();        System.out.println(app.welcomeAndInstructions());        app.createQuiz();        app.administerQuiz();        app.gradeQuiz();    }    private String welcomeAndInstructions() {        return "Welcome to Math Quiz!\n" +         ...

  • Task 3: Main Program Create a main program class that: Creates three or more Nurse instances...

    Task 3: Main Program Create a main program class that: Creates three or more Nurse instances assigned to different shifts. Creates three or more Doctor instances. Creates three or more Patient instances with pre-determined names and manually assigned physicians chosen from the pool of Doctor instances previously created. Generates another 20 Patient instances using randomly generated names and randomly assigns them physicians chosen from the pool of Doctor instances previously created. Prints the toString() values for all employees. Prints the...

  • For Java please.Artwork. javaimport java.util.Scanner;public class ArtworkLabel {public static void main(String[] args)...

     Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods...

    java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods commented with: // TODO: TIP: Do not just go from memory of your assignment implementation, be sure to consider carefully the constructor and method implementation provided to you. NOTE: You do not have to provide an implementation for any methods intentionally omitted public class Heap PriorityQueue implements PriorityQueue private final static int DEFAULT SIZE 10000 private Comparable [ ] storage private int currentSize: public...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

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