Respuesta :
Answer:
Kindly note that, you're to replace "at" with shift 2 as the brainly text editor can't take the symbol
Explanation:
1) Question interface:
package chegg;
public interface Question {
public String getTheQuestionText();
public boolean isCorrectAnswer(String answer);
public String getCorrectAnswer();
}
2) MultipleChoiceQuestion class:
package chegg;
public class MultipleChoiceQuestion implements Question {
private String que;
private String ans;
public MultipleChoiceQuestion(String que, String ans) {
this.que = que;
this.ans = ans;
}
"at"Override
public String getTheQuestionText() {
return que;
}
"at"Override
public boolean isCorrectAnswer(String answer) {
if (ans.equals(answer)) return true;
else return false;
}
"at"Override
public String getCorrectAnswer() {
return ans;
}
}
3) TrueFalseQuestion class:
package chegg;
public class TrueFalseQuestion implements Question {
private String que;
private String ans;
public TrueFalseQuestion(String que, String ans) {
this.que = que;
this.ans = ans;
}
"at"Override
public String getTheQuestionText() {
return que;
}
"at"Override
public boolean isCorrectAnswer(String answer) {
if (ans.equals(answer)) return true;
else return false;
}
"at"Override
public String getCorrectAnswer() {
return ans;
}
}
4) ShortAnswerQuestion class:
package chegg;
public class ShortAnswerQuestion implements Question {
private String que;
private String ans;
public ShortAnswerQuestion(String que, String ans) {
this.que = que;
this.ans = ans;
}
"at"Override
public String getTheQuestionText() {
return que;
}
"at"Override
public boolean isCorrectAnswer(String answer) {
if (ans.equals(answer)) return true;
else return false;
}
"at"Override
public String getCorrectAnswer() {
return ans;
}
}
5) Quiz class:
package chegg;
import java.util.ArrayList;
import java.util.Scanner;
public class Quiz {
private ArrayList<Question> questions;
private ArrayList<Question> wrong;
public Quiz(){
questions = new ArrayList<>();
}
public void addQuestion(Question q){
questions.add(q);
}
public double giveQuiz(){
Scanner sc = new Scanner(System.in);
int total = questions.size();
int marks = 0;
wrong = new ArrayList<>();
for(int i=0; i<total; i++){
Question q = questions.get(i);
System.out.println("Question no. "+i+": "+q.getTheQuestionText());
System.out.println("your answer: ");
String ans = sc.next();
if(q.isCorrectAnswer(ans)) marks++;
else wrong.add(q);
}
double score = marks*100/(double)total; // score in percentage
return score;
}
public void showWrongAnswer(){
System.out.println("Solutions to your wrong answers: ");
for (Question q: wrong){
System.out.println("Question: "+q.getTheQuestionText());
System.out.println("Answer: "+q.getCorrectAnswer());
}
}
}
6) QuizTime class:
package chegg;
import java.util.Scanner;
public class QuizTime {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of questions: ");
int n = sc.nextInt();
System.out.println("Please choose type of question (enter 1, 2 or 3) : ");
System.out.println("1. Multiple choice question");
System.out.println("2. True or False type question");
System.out.println("3. Short answer question");
int choice = sc.nextInt();
Quiz quiz = new Quiz();
while (n>0){
System.out.println("Enter question: ");
String q = sc.next();
System.out.println("Enter answer: ");
String ans = sc.next();
Question question;
if (choice == 1) question = new MultipleChoiceQuestion(q, ans);
else if (choice == 2) question = new TrueFalseQuestion(q, ans);
else question = new ShortAnswerQuestion(q, ans);
quiz.addQuestion(question);
n--;
}
System.out.println("Give the following test: ");
if (choice == 1) System.out.println("Note: all questions are Multiple choice question");
else if (choice == 2) System.out.println("Note: all questions are True or False type question");
else System.out.println("Note: all questions are Short answer question");
double score = quiz.giveQuiz();
System.out.println("your final score is: "+score+"%");
quiz.showWrongAnswer();
}
}