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

For this project, you will: -create a quiz of up to 25 questions and answers, -g

ID: 3712665 • Letter: F

Question

For this project, you will:

-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 at bottom)

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.

Question.java

//********************************************************************
// Question.java Author: Lewis/Loftus
//
// Represents a question (and its answer).
//********************************************************************

public class Question implements Complexity
{
private String question, answer;
private int complexityLevel;

//-----------------------------------------------------------------
// Constructor: Sets up the question with a default complexity.
//-----------------------------------------------------------------
public Question(String query, String result)
{
question = query;
answer = result;
complexityLevel = 1;
}

//-----------------------------------------------------------------
// Sets the complexity level for this question.
//-----------------------------------------------------------------
public void setComplexity(int level)
{
complexityLevel = level;
}

//-----------------------------------------------------------------
// Returns the complexity level for this question.
//-----------------------------------------------------------------
public int getComplexity()
{
return complexityLevel;
}

//-----------------------------------------------------------------
// Returns the question.
//-----------------------------------------------------------------
public String getQuestion()
{
return question;
}

//-----------------------------------------------------------------
// Returns the answer to this question.
//-----------------------------------------------------------------
public String getAnswer()
{
return answer;
}

//-----------------------------------------------------------------
// Returns true if the candidate answer matches the answer.
//-----------------------------------------------------------------
public boolean answerCorrect(String candidateAnswer)
{
return answer.equals(candidateAnswer);
}

//-----------------------------------------------------------------
// Returns this question (and its answer) as a string.
//-----------------------------------------------------------------
public String toString()
{
return question + " " + answer;
}
}

Please inlcude documentation so I can learn and undrstand the code better, thank you!

Explanation / Answer

The required code is provided below. Completed Quiz and QuizTime classes and conducted a quiz. For a demonstration, I have used simple arithmetic questions for quiz, you can use your own questions if you want. Everything is explained in comments. Thanks.

There is no change made in Question.java, so it is not included in the answer to save space

// Quiz.java

import java.util.Scanner;

public class Quiz {

                // required attributes

                private Question[] questions;

                private int correctAnswers;

                private int incorrectAnswers;

                private int count;

                /**

                * constructor which initializes the questions array with size 25

                */

                public Quiz() {

                                questions = new Question[25];

                                correctAnswers = 0;

                                incorrectAnswers = 0;

                                count = 0;

                }

                /**

                * method to create a question and add to the array

                *

                * @param quest

                *            - question in string format

                * @param ans

                *            - answer in string format

                */

                public void add(String quest, String ans) {

                                /**

                                * Creating a question (using default complexity)

                                */

                                Question question = new Question(quest, ans);

                                /**

                                * adding to the array if the array is not full

                                */

                                if (count < questions.length) {

                                                questions[count] = question;

                                                count++;

                                }

                }

                /**

                * method to loop through each questions and get user input, display all

                * stats

                */

                public void giveQuiz() {

                                /**

                                * initializing a scanner

                                */

                                Scanner scanner = new Scanner(System.in);

                                String answer;

                                /**

                                * loop through each questions

                                */

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

                                                /**

                                                * displaying question

                                                */

                                                System.out.printf("Question (%d): %s ", (i + 1),

                                                                                questions[i].getQuestion());

                                                // getting answer

                                                answer = scanner.nextLine();

                                                // checking if answer is right

                                                if (questions[i].answerCorrect(answer)) {

                                                                System.out.println("Right answer!");

                                                                correctAnswers++;

                                                } else {

                                                                System.out.println("Incorrect answer!");

                                                                incorrectAnswers++;

                                                }

                                }

                                /**

                                * displaying the stats

                                */

                                System.out.println("Quiz completed!");

                                System.out.println("Total questions: " + count);

                                System.out.println("Correct answers: " + correctAnswers);

                                System.out.println("Incorrect answers: " + incorrectAnswers);

                }

                /**

                *

                * @return the number of right answers

                */

                public int getNumCorrect() {

                                return correctAnswers;

                }

                /**

                *

                * @return the number of wrong answers

                */

                public int getNumIncorrect() {

                                return incorrectAnswers;

                }

}

// QuizTime.java

public class QuizTime {

                public static void main(String[] args) {

                                /**

                                * Defining a Quiz

                                */

                                Quiz quiz = new Quiz();

                                /**

                                * Adding a few basic questions, you can add more yourself in the same

                                * format

                                */

                                quiz.add("What is 1+2?", "3");

                                quiz.add("What is 11*2?", "22");

                                quiz.add("What is 9+18?", "27");

                                quiz.add("What is 56*8?", "448");

                                quiz.add("What is 33-8?", "25");

                                quiz.add("What is 70-90?", "-20");

                                quiz.add("What is 3090+10?", "3100");

                                quiz.add("What is 55*2?", "110");

                                quiz.add("What is 20*9?", "180");

                                quiz.add("What is 13+25?", "38");

                                /**

                                * giving the quiz to the user

                                */

                                quiz.giveQuiz();

                }

}

/*OUTPUT*/

Question (1): What is 1+2?

3

Right answer!

Question (2): What is 11*2?

8

Incorrect answer!

Question (3): What is 9+18?

27

Right answer!

Question (4): What is 56*8?

498

Incorrect answer!

Question (5): What is 33-8?

25

Right answer!

Question (6): What is 70-90?

-20

Right answer!

Question (7): What is 3090+10?

3100

Right answer!

Question (8): What is 55*2?

110

Right answer!

Question (9): What is 20*9?

180

Right answer!

Question (10): What is 13+25?

38

Right answer!

Quiz completed!

Total questions: 10

Correct answers: 8

Incorrect answers: 2

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