Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java application that simulates a test. Each question should be a multip

ID: 3756228 • Letter: W

Question

Write a Java application that simulates a test. 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: - create a method to simulate the questions – simulateQuestion

- create a method to check the answer – checkAnswer

-create a method to display a random message for the user – generateMessage - create a method to interact with the user - inputAnswer Display the questions using methods of JOptionPane class. Use a loop to show all the questions.

For each question: - If the user finds the right answer, display a random congratulatory message (“Excellent!”,”Good!”,”Keep up the good work!”, or “Nice work!”). - If the user responds incorrectly, display an appropriate message and the correct answer (“No. Please try again”, “Wrong. Try once more”, “Don't give up!”, “No. Keep trying..”). - Use random-number generation to choose a number from 1 to 4 that will be used to select an appropriate response to each answer.

- Use a switch statement to issue the responses, as in the following code: 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.

Explanation / Answer

CODE TO COPY:

import java.util.Random;

import javax.swing.JOptionPane;

public class Test {

private int correctAnswer;

private int inCorrectAnswer;

private int numQuestion;

public Test() {

correctAnswer = 0;

inCorrectAnswer = 0;

numQuestion = 5;

}

private void simulateQuestion(int choice) {

String question = "";

String[] answers = new String[4];

int answer = 5;

switch (choice + 1) {

case 1:

question = "1.What is the range of byte data type in Java??";

answers[0] = "a.-128 to 127";

answers[1] = "b.-32768 to 32767";

answers[2] = "c.-2147483648 to 2147483647";

answers[3] = "d.None of these";

answer = 0;

break;

case 2:

question = "2.Which one is a valid declaration of a boolean?";

answers[0] = "a.boolean b1 = 1;";

answers[1] = "b.boolean b2 = ‘false’;";

answers[2] = "c.boolean b3 = false;";

answers[3] = "d.boolean b4 = ‘true’";

answer = 2;

break;

case 3:

question = "3.What is Truncation is Java?";

answers[0] = "a.Floating-point value assigned to an integer type";

answers[1] = "b.Integer value assigned to floating type";

answers[2] = "c.Floating-point value assigned to an Floating type";

answers[3] = "d.Integer value assigned to floating type";

answer = 0;

break;

case 4:

question = "4.Which of these is an incorrect array declaration?";

answers[0] = "a.int arr[] = new int[5]";

answers[1] = "b.int [] arr = new int[5]";

answers[2] = "c.int arr[] = new int[5]";

answers[3] = "d.int arr[] = int [5] new";

answer = 3;

break;

case 5:

question = "5.Which of these can not be used for a variable name in Java?";

answers[0] = "a.identifier";

answers[1] = "b.keyword";

answers[2] = "c.identifier & keyword";

answers[3] = "d.none of the mentioned";

answer = 1;

break;

}

int ans = JOptionPane.showOptionDialog(null, question, "Select Answer", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, answers, answer);

checkAnswer(ans, answer);

}

public static void main(String args[]) {

Test test = new Test();

test.inputAnswer();

}

private void checkAnswer(int a1, int a2) {

Random rand = new Random();

if (a1 == a2) {

correctAnswer++;

JOptionPane.showMessageDialog(null, generateMessage(1));

} else {

inCorrectAnswer++;

JOptionPane.showMessageDialog(null, generateMessage(2));

}

}

private void inputAnswer() {

for (int i = 0; i < numQuestion; i++) {

simulateQuestion(i);

}

generateReport();

}

private void generateReport() {

String msg = "";

msg = "No. of correct answers :" + correctAnswer;

msg += " No. of incorrect answers :" + inCorrectAnswer;

msg += " Percentage of correct answers: " + ((double) correctAnswer / numQuestion) * 100 + "%";

JOptionPane.showMessageDialog(null, msg);

}

private String generateMessage(int option) {

Random rand = new Random();

if (option == 1) {

switch (rand.nextInt(4) + 1) {

case 1:

return "Excellent!!";

case 2:

return "Good";

case 3:

return "Keep up the good work!";

case 4:

return "Nice Work!";

}

} else {

switch (rand.nextInt(4) + 1) {

case 1:

return "No!!Please try again.";

case 2:

return "Wrong!Try once more";

case 3:

return "Don't give up";

case 4:

return "No!Keep trying";

}

}

return "";

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote