Your program should meet all of the following requirements:
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%) |
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.
Get Answers For Free
Most questions answered within 1 hours.