Question

Java Programming: Math Quiz

Screen Shot 2021-10-24 at 1.49.57 PM.png

Starting codes:


MathQuiz.java


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));

    }

}


MathQuestion.java


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!)


}


MathQuestionable.java


import java.util.Random;


public interface MathQuestionable {

    Random RANDOM = new Random(5); //DO NOT CHANGE THIS LINE

    

   // TODO: Add properties and method headers


}


OperationType.java


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();

        }

    }

}


0 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:
Java Programming: Math Quiz
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
  • 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...

  • 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...

  • 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...

  • 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...

  • I need this in java please Create an automobile class that will be used by a...

    I need this in java please Create an automobile class that will be used by a dealership as a vehicle inventory program. The following attributes should be present in your automobile class: private string make private string model private string color private int year private int mileage. Your program should have appropriate methods such as: default constructor parameterized constructor add a new vehicle method list vehicle information (return string array) remove a vehicle method update vehicle attributes method. All methods...

  • 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...

  • Write a Java class named Employee to meet the requirements described in the UML Class Diagram...

    Write a Java class named Employee to meet the requirements described in the UML Class Diagram below: Note: Code added in the toString cell are not usually included in a regular UML class diagram. This was added so you can understand what I expect from you. If your code compiles cleanly, is defined correctly and functions exactly as required, the expected output of your program will solve the following problem: “An IT company with four departments and a staff strength...

  • It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming...

    It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming Challenge 2 Employee Class.pdf Program Template: // Chapter 13, Programming Challenge 2: Employee Class #include <iostream> #include <string> using namespace std; // Employee Class Declaration class Employee { private: string name; // Employee's name int idNumber; // ID number string department; // Department name string position; // Employee's position public: // TODO: Constructors // TODO: Accessors // TODO: Mutators }; // Constructor #1 Employee::Employee(string...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Please help me with my JAVA programming Exercise. a. The Talk-A-Lot Cell Phone Company provides phone...

    Please help me with my JAVA programming Exercise. a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class named PhoneCall that includes a String field for a phone number and a double field for the price of the call. Also include a constructor that requires a phone number parameter and that sets the price to 0.0. Include a set method for the price. Also include three abstract get methods—one that returns the phone number,...

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