In: Computer Science
Java programming language
Write a Java application that simulates a test. The test
contains at least five questions. Each question should be a
multiple-choice question with 4 options.
Design a QuestionBank 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
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 QuestionBank object ( in the
driver class – QuestionBankTest.java) and start the test by calling
inputAnswer method.
//----- QuestionBank.java -----------
import java.util.Random;
import java.util.Scanner;
class QuestionBank
{
//array of questions.
private final String[] questions = {
"What is the Capital of
England?",
"What is the Sum of first 10
numbers?",
"Which continent is called as Dark
Continent?",
"What is the highest peak in the
world?",
"What is the result of 2 + 3 * 10 /
2 = ?"
};
//answers for above questions
private final String[] answers={
"London",
"55",
"Africa",
"Mount Everest",
"17"
};
//options for each of the above questions.
private final String[][] options = {
{"London","New
York","Rome","Washington"},
{"10","55","54","52"},
{"America","Europe","Asia","Africa"},
{"Mount Cameroon","Mount
Everest","Chomo Lonzo","Vinson Massif"},
{"16","50","17","25"}
};
//random object
Random randomObject;
//scanner
Scanner in;
QuestionBank()
{
randomObject = new Random();
in = new Scanner(System.in);
}
//function that simulates the questions
//by printing the question and its options for given
question index.
int simulateQuestion(int qInd)
{
System.out.println("\n"+(qInd+1)+".
"+questions[qInd]);
int opLen =
options[qInd].length;
for(int i = 0 ; i < opLen ;
i++)
{
System.out.println("\n"+(char)('a'+i)+". "+options[qInd][i]);
}
return qInd;
}
//function that takes the option user
entered.(a,b,c,d,)
//and gets the option answer (a -> london) selected
and
//checks the option answer with the
answers[qInd]
boolean checkAnswer(String answer,int qInd)
{
if(answer.length() != 1)
{
return
false;
}
int opLen =
options[qInd].length;
String choosenAns="";
boolean found = false;
String i_thOp;
for(int i =0; i<opLen;i++)
{
i_thOp =
""+(char)('a'+i);
if(answer.equals(i_thOp))
{
choosenAns = options[qInd][i];
found = true;
break;
}
}
if(!found)
{
return
false;
}
else
{
return
answers[qInd].equals(choosenAns);
}
}
//function that simulates the process.
//by printing the question , options and checking the
answer
//iterating thorugh all questions.
void inputAnswer()
{
int posScore = 0;
int negScore = 0;
for(int i =
0;i<questions.length;i++)
{
int qInd =
simulateQuestion(i);
System.out.print("\nEnter your option: ");
String ans =
in.nextLine();
System.out.println("");
if(checkAnswer(ans,qInd))
{
System.out.println(generateMessage(true));
posScore++;
}
else
{
System.out.println(generateMessage(false));
negScore++;
}
}
System.out.println("\nTotal Right
Answer: "+posScore);
System.out.println("Total Wrong
Answer: "+negScore);
float percent = (float)posScore
/(posScore + negScore);
System.out.println(String.format("Percentage of Right Answers:
%.2f",percent));
}
//function that returns the message
//for given type.
//if boolean positive set to true then it returns the
random
//positive messages stored in posMessages array.
//or else it returns the negative messages in
negMessages array.
String generateMessage(boolean positive)
{
//if want to print to positive
messsages.
if(positive)
{
//positive
messages.
switch(randomObject.nextInt(4))
{
case 0:
return "Excellent!";
case 1:
return "Good!";
case 2:
return "Keep up the good
work!";
case 3:
return "Nice work!";
}
}
else
{
//negative
messages
switch(randomObject.nextInt(4))
{
case 0:
return "No. Please try
again";
case 1:
return "Wrong. Try once more"
;
case 2:
return "Don't give up!"
;
case 3:
return "No. Keep
trying..";
}
}
return"";
}
}
//---------- QuestionBankTest.java ------------
class QuestionBankTest
{
public static void main(String[] args)
{
QuestionBank driver = new
QuestionBank();
driver.inputAnswer();
}
}