In: Computer Science
PLEASE USE THE ECLIPSE.
I want to make a program that shows whether the correct number is correct or incorrect.
Example: how much is 1+1?
option 1: 2
option 2: 3
option 3: 4
option 4: 5
option 5: 6
The answer is 1. because the answer is 2 and the option number is 1.
Write a Java application that simulates a test. The test contains at least five questions about first three lectures of this course. Each question should be a multiple-choice question with 4 options.
Design a Test class. Use programmer-defined methods to implement your solution. For example:
For each question:
switch ( randomObject.nextInt( 4 ) )
{
case 0:
return( "Very good!" );
……
}
At the end of the test display the number of correct and incorrect answers, and the percentage of the correct answers.
Your main class will simply create a Test object and start the test by calling inputAnswer method.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. Let me know for any help with any other questions. Thank You! =========================================================================== public class Test { private String question; private String optionA; private String optionB; private String optionC; private String optionD; private String correctAnswer; private String userAnswer; private static Scanner scanner = new Scanner(System.in); private static Random random = new Random(); public Test(String question, String optionA, String optionB, String optionC, String optionD, String correctAnswer) { this.question = question; this.optionA = optionA; this.optionB = optionB; this.optionC = optionC; this.optionD = optionD; this.correctAnswer = correctAnswer; } public void simulateQuestion() { System.out.println(question); System.out.println("Option 1: " + optionA); System.out.println("Option 2: " + optionB); System.out.println("Option 3: " + optionC); System.out.println("Option 4: " + optionD); } public void inputAnswer() { System.out.print("Enter the correct option [1-4] > "); userAnswer = scanner.nextLine(); } public boolean checkAnswer() { return (userAnswer.equalsIgnoreCase(correctAnswer)); } public void generateMessage() { if (checkAnswer()) { switch (random.nextInt(4)) { case 0: System.out.println("Excellent"); break; case 1: System.out.println("Good!"); break; case 2: System.out.println("Keep up the good work!"); break; case 3: System.out.println("Nice work!"); break; } } else { switch (random.nextInt(4)) { case 0: System.out.println("No, Please try again"); break; case 1: System.out.println("Wrong! Try once more."); break; case 2: System.out.println("Don't give up!"); break; case 3: System.out.println("No keep trying!"); break; } } } }
===================================================================
public class Tester { public static void main(String[] args) { Test[] tests = new Test[5]; // chnage the question set here based on the three lectures of this course tests[0] = new Test("How much is 2+2?", "2", "3", "4", "5", "3"); tests[1] = new Test("How much is 12/2?", "2", "6", "10", "12", "2"); tests[2] = new Test("How much is 20-2?", "18", "22", "10", "0", "1"); tests[3] = new Test("How much is 22+2?", "44", "24", "42", "20", "2"); tests[4] = new Test("How much is 2+222?", "222", "220", "226", "224", "4"); int corrects = 0, wrongs = 0; for (int i = 0; i < tests.length; i++) { System.out.println("Question #" + (i + 1) + "\n"); tests[i].simulateQuestion(); tests[i].inputAnswer(); tests[i].generateMessage(); if (tests[i].checkAnswer()) corrects += 1; else wrongs += 1; System.out.println(); } System.out.printf("\nTotal Correct Answers : %2d (%5.2f)%%\n", corrects, (corrects * 100.0 / tests.length)); System.out.printf("Total Wrong Answers : %2d (%5.2f)%%\n", wrongs, (wrongs * 100.0 / tests.length)); } }
===================================================================