Question

Your program should meet all of the following requirements: The class name should be MathQuiz All...

Your program should meet all of the following requirements:

  • The class name should be MathQuiz
  • All 8 problems should use int values.
  • Use Math.random() for generating your random values.
  • The range of random numbers for each problem should be based on whether the user asks for the "easy" or the "hard" options.
    • "easy" means that all numbers, including the answers, will be no more than 100. It should be possible to get values all the way up to and including 100. It should be possible to get numbers as small as 1. Zero should not be possible.
    • "hard" means that all numbers, including the answers, will be no more than 1000. It should be possible to get values all the way up to and including 1000. It should be possible to get numbers as small as 1. Zero should not be possible.
  • For division problems, there should never be a remainder. So, 48 divided by 4 should be possible, but 48 divided by 5 should not be possible.
  • Display the problems using the multiplication and division symbols shown in the sample run (note that the multiplication symbol is not the letter x, use the letter x as for the multiplication symbol). It is the standard multiplication symbol.
  • Display the ongoing scores and final scores exactly as shown above, including rounding percentages to two places after the decimal point.
  • Display the total time it took the user to answer all 8 questions.

Submit your source code file as a zip file.

Your program should match this format as closely as possible. Note that text shown in red is there because the user typed it. You are not supposed to print those.

Welcome to my math quiz!

This quiz will give you four multiplication problems,

followed by four division problems.

An easy quiz will include numbers up to 100.

A hard quiz will include numbers up to 1000.

-----------------------------------------------------

Do you want an easy quiz or a hard quiz?

Enter the word hard or easy: easy

-MULTIPLICATION--------------------------------------

You have chosen easy.

5 x 10 = 50

Correct! 1 answers correct so far.

8 x 7 = 56

Correct! 2 answers correct so far.

8 x 9 = 71

Sorry, 8 × 9 = 72. 2 answers correct so far.

6 x 5 = 30

Correct! 3 answers correct so far.

-DIVISION--------------------------------------------

72 ÷ 9 = 9

No, 72 ÷ 9 = 8. 3 answers correct so far.

10 ÷ 10 = 1

Correct! 4 answers correct so far.

1 ÷ 1 = 1

Correct! 5 answers correct so far.

45 ÷ 5 = 9

Correct! 6 answers correct so far.

-RESULTS---------------------------------------------

You answered the questions in 47 seconds.

Multiplication score: 3 out of 4 (75.00%)

Division score: 3 out of 4 (75.00%)

Overall score: 6 out of 8 (75.00%)

Homework Answers

Answer #1

Source Code:

import java.lang.Math;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;

public class MathQuiz {

    public void startDisplay(){
        String text = "Welcome to my math quiz!\n" +
                "This quiz will give you four multiplication problems,\n" +
                "followed by four division problems.\n" +
                "An easy quiz will include numbers up to 100.\n" +
                "A hard quiz will include numbers up to 1000.\n" +
                "-----------------------------------------------------\n" +
                "Do you want an easy quiz or a hard quiz?";
        System.out.println(text);
    }

    public int[] generateMultiplicationQuestion(String difficulty){
        int factor;
        if(difficulty.equals("easy"))
            factor = 100;
        else
            factor = 1000;
        Random random = new Random();
        int[] res = new int[3];
        int a = random.nextInt()%factor;
        int b = random.nextInt()%factor;
        while(a <= 0 || b <= 0 || a * b > factor){
            a = random.nextInt()%factor;
            b = random.nextInt()%factor;
        }
        System.out.print("" + a + " x " + b + " = ");
        res[0] = a;
        res[1] = b;
        res[2] = a*b;
        return res;
    }

    public int[] generateDivisionQuestion(String difficulty){
        int factor;
        if(difficulty.equals("easy"))
            factor = 100;
        else
            factor = 1000;
        Random random = new Random();
        char qmark = (char)0x00F7;
        int[] res = new int[3];
        int a = random.nextInt()%factor;
        int b = random.nextInt()%factor;
        while(a <= 0 || b <= 0 || a % b != 0){
            a = random.nextInt()%factor;
            b = random.nextInt()%factor;
        }
        System.out.print("" + a + " " + qmark + " " + b + " = ");
        res[0] = a;
        res[1] = b;
        res[2] = a/b;
        return res;
    }

    public static String percent(int score,int questions){
        double res = (double)score * 100 / questions;
        String s = String.format("%.2f", res);
        s = s + "%";
        return s;
    }

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        MathQuiz mq = new MathQuiz();
        mq.startDisplay();
        char qmark = (char)0x00F7;
        int cnt = 0;    //Stores total number of correct answers
        int mulScore = 0;   //Stores number of correct answers in multiplication
        System.out.print("Enter the word hard or easy: ");
        String inputWord = scanner.next();
        System.out.println("-MULTIPLICATION-----------------------------------------");
        System.out.println("You have chosen " + inputWord + ".");
        long startTime = System.nanoTime();
        for(int i=0;i<4;i++){
            int[] res = mq.generateMultiplicationQuestion(inputWord);
            int userAnswer = scanner.nextInt();
            if(res[2] == userAnswer){
                System.out.print("Correct! ");
                cnt++;
                System.out.println("" + cnt + " answers correct so far.");
            }
            else{
                System.out.print("Sorry, " + res[0] + " x " + res[1] + " = " + res[2] + ".");
                System.out.println("" + cnt + " answers correct so far.");
            }
        }
        mulScore = cnt;
        System.out.println("-DIVISION------------------------------------------------");
        for(int i=0;i<4;i++){
            int[] res = mq.generateDivisionQuestion(inputWord);
            int userAnswer = scanner.nextInt();
            if(res[2] == userAnswer){
                System.out.print("Correct! ");
                cnt++;
                System.out.println("" + cnt + " answers correct so far.");
            }
            else{
                System.out.print("No, " + res[0] + " " + qmark + " " + res[1] + " = " + res[2] + ".");
                System.out.println("" + cnt + " answers correct so far.");
            }
        }
        long endTime = System.nanoTime();
        long time = endTime - startTime;
        int seconds = (int)(time / 1000000000);
        int divScore = cnt - mulScore;
        System.out.println("-RESULTS-----------------------------------------");
        System.out.println("You answered the questions in " + seconds + " seconds.");
        System.out.println("Multiplication score:" + mulScore + " out of 4 (" + percent(mulScore,4) + ")");
        System.out.println("Division score:" + divScore + " out of 4 (" + percent(divScore,4) + ")");
        System.out.println("Overall score:" + cnt + " out of 8 (" + percent(cnt,8) + ")");
    }

}

Output:

Please appreciate the solution if you find it helpful.

If you have any doubts in the solution, feel free to ask me in the comment section.

Know the answer?
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
Similar Questions
Hi, I need a program in C, where it will prompt a user for a math...
Hi, I need a program in C, where it will prompt a user for a math quiz, either easy or medium. If the user choses difficulty easy, it will prompt the user for how many questions they would like from 1-5. If the user enters medium it will prompt the user on how many questions they would like from 1-10. After those prompts are entered questions will be displayed using rand(), the questions must be multiple choice or division, aswell...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do depending upon what the user enters, please refer to the examples below to use to test your program. Run your final program one time for each scenario to make sure that you get the expected output. Be sure to format the output of your program so that it follows what is included in the examples. Remember, in all examples bold items are entered by...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
Complete the following activities and then post your responses in the Activity #5 discussion forum (link...
Complete the following activities and then post your responses in the Activity #5 discussion forum (link below). Consider a short multiple choice quiz with three items, and each item has four choices (only one of the choices is correct). Suppose that you are taking this quiz but you are completely and utterly unprepared for it. That means that you only option for the quiz is to guess the answers. Suppose you are thinking about the first item: what's the probability...
Could you script in Matlab : Script Name General Algorithm AddNewEmployee Display all of the ID...
Could you script in Matlab : Script Name General Algorithm AddNewEmployee Display all of the ID numbers currently in use Read in a new ID number If the ID number read in is already in use Report this fact End this script Else (this is a new ID number) Read in: Years with the company Salary Vacation days Sick days Add this new employee to the database (by appending a new row to the end of the EmployeeData matrix) Note...
Directions: Include all steps of hypothesis testing in your solutions to the following problems. Show all...
Directions: Include all steps of hypothesis testing in your solutions to the following problems. Show all your work, including calculations of the test-statistic and the p-value. Check your answers using PHStat. Round sample percentages to two (2) decimal places; round other numbers to four (4) decimal places. Only give the Type I or Type II error if the problem asks for these errors. 1. A fitness center offers a spin class three days a week. They want to know if...
Python programming Write a program that prompts the user to input the three coefficients a, b,...
Python programming Write a program that prompts the user to input the three coefficients a, b, and c of a quadratic equationax2+bx+c= 0.The program should display the solutions of this equation, in the following manner: 1. If the equation has one solution, display ONE SOLUTION:, followed by the solution, displayed with4 digits printed out after the decimal place. 2. If the equation has two real solutions, display TWO REAL SOLUTIONS:, followed by the two solutions, each displayed with4 digits printed...
**C++** a. Write a program containing the following expressions. x should be an int variable. Add...
**C++** a. Write a program containing the following expressions. x should be an int variable. Add a statement after each one to print out the current value of x in the following format (of course, you will print the value of x, not the constant 4!): Problem 1. x has the value 4 1. x = 4; 2. x = 2; 3. x = 24 – 6 * 2; (show each computation) 4. x = -15 * 2 + 3;...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
The chapter 4 form worksheet is to be used to create your own worksheet version of...
The chapter 4 form worksheet is to be used to create your own worksheet version of exhibit 4-5 and exhibit 4-8 in the text. Change all of the numbers in the data area of your worksheet so that it looks like this: A B 1.Chapter 4: Applying Excel 2. 3. Data 4. Beginning work in process inventory    5. Units in process 600 6. Completion with respect to materials 90% 7. Completion with respect to conversion 45% 8. Costs in...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT