create a quiz of up to 25 questions and answers, give the quiz, and output how m
ID: 3718416 • Letter: C
Question
create a quiz of up to 25 questions and answers, give the quiz, and output how many correct and incorrect answers were given. You will use an array of 25 Question objects created from the class Question of the Chapter 7 source files. The program should utilize: class Question (provided) Question.java is in the Chapter 7 source files. Class Question contains: constructor for class Question that accepts 2 strings for the question and the answer and an integer which is not used by this program (complexity not used) getQuestion method that returns the question part of the Question object answerCorrect method that accepts a string for the potential answer and returns true if the string matches the correct answer in the Question object. class Quiz Class Quiz contains: declarations of an array to hold Questions (Question objects from Class Question), an int to count correct answers, an int to count incorrect answers constructor that instantiates an initially empty array for 25 Questions add method that accepts 2 strings (question and answer) passed as parameters, instantiates a new Question object and inserts the Question object into the next element of the array giveQuiz method that loops through the array and outputs the question, uses scanner to read an answer from the user, and invokes answerCorrect to determine whether the answer was correct and increments the appropriate count. getNumCorrect method that returns the number answered correctly getNumIncorrect method that returns the number answered incorrectly class QuizTime - contains the main method that: instantiates a Quiz object populates the quiz by invoking the add method 25 times, passing a question and answer string each time. invokes the giveQuiz method outputs the number correct and the number incorrect using methods getNumCorrect and getNumIncorrect Save the files as QuizTime_XXX.java and Quiz_XXX.java, substituting your initials for XXX. No need to change the Question class so no need to upload Question.java.
Explanation / Answer
Solution:
code:
public class Question {
String question;
String answer;
int complexity;
public Question(String question,String answer){
this.question = question;
this.answer = answer;
}
// getQuestion method to get question part
public String getQuestion() {
return question;
}
// getAnswer method to get answer part
public String getAnswer() {
return answer;
}
public boolean answerCorrect(String ans){
if(this.getAnswer().equals(ans))
return true;
return false;
}
}
Quiz.java:
import java.util.Scanner;
public class Quiz {
Question questions[];
int correctAns;
int inCorrectAns;
int size;
public Quiz(){
size = 0; // Initializing size of array. It is used to add questions to Question array
questions = new Question[25]; // Initializing Question array to 25
}
public void add(String ques,String ans){
Question question = new Question(ques,ans); // Creating a question object
questions[size++] = question; // Adding question object to array
}
public void giveQuiz(){
Scanner scan = new Scanner(System.in);
for(int i=0;i<size;i++){
System.out.println(questions[i].getQuestion());
System.out.print("Ans: ");
String answer = scan.nextLine();
// Checking of entered answer is equal to actual answer
if(questions[i].answerCorrect(answer)){
correctAns++;
}
else{
inCorrectAns++;
}
System.out.println();
}
scan.close();
}
public int getNumCorrect(){
return correctAns;
}
public int getNumIncorrect(){
return inCorrectAns;
}
public static void main(String[] args) {
Quiz quiz = new Quiz();
// Adding question and answers
quiz.add("1+1=", "2");
quiz.add("1+2=", "3");
quiz.add("1+3=", "4");
quiz.add("1+4=", "5");
quiz.add("1+5=", "6");
quiz.add("1+6=", "7");
quiz.add("1+7=", "8");
quiz.add("1+8=", "9");
quiz.add("1+10=", "11");
quiz.add("1-1=", "0");
quiz.add("2+1=", "3");
quiz.add("2+2=", "4");
quiz.add("2+3=", "5");
quiz.add("2+4=", "6");
quiz.add("2+5=", "7");
quiz.add("2+6=", "8");
quiz.add("2+7=", "9");
quiz.add("2+8=", "10");
quiz.add("2+9=", "11");
quiz.add("2+10=", "12");
quiz.add("3+1=", "4");
quiz.add("3+2=", "5");
quiz.add("3+3=", "6");
quiz.add("3+4=", "7");
quiz.add("3+5=", "8");
quiz.giveQuiz();
System.out.println("Number of correct answers: "+quiz.getNumCorrect());
System.out.println("Number of incorrect answers: "+quiz.getNumIncorrect());
}
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.