# Java Please help me to solve this problem. Many thanks! Question: //**********
ID: 3757366 • Letter: #
Question
# Java
Please help me to solve this problem. Many thanks!
Question:
//********************************************************************
// Question.java
//********************************************************************
public class Question {
private String question, answer;
private int complexityLevel;
//-----------------------------------------------------------------
// 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.
//-----------------------------------------------------------------
3
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;
}
}
2) (40 points) Use the Question class from below5 to define a Quiz class. A quiz can be composed of up to 25 questions. Define the add method of the Quiz class to add a question to a quiz. Define the giveQuiz method of the Quiz class to present each question in turn to the user, accept an answer for each one, and keep track of the results. Additionally, overload thel giveQuiz method so that it accepts two integer parameters that specify the minimum and maximum complexity levels for the quiz questions and only presents questions in that complexity range. Define a class called QuizTime with a main method that populates a quiz, presents it, and prints the final results. Also demonstrate the overloaded giveQuiz method in the main method (allowing users to pick questions within a specific complexity range)Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. If you are satisfied with the solution, please rate the answer. Thanks
// Quiz.java
import java.util.Scanner;
public class Quiz {
// attributes
private Question[] questions;
private int numQuestions; // current number of questions
/**
* default constructor
*/
public Quiz() {
// initializing array so that it can hold upto 25 questions
questions = new Question[25];
numQuestions = 0;
}
/**
* method to add a Question
*
* @param q
* question object
*/
public void addQuestion(Question q) {
// adding to the array if not full
if (numQuestions < questions.length) {
questions[numQuestions] = q;
numQuestions++;
}
}
/**
* method to start quiz of all questions
*/
public void giveQuiz() {
// calling the overloaded method, so that it will include questions of
// all complexities
giveQuiz(0, Integer.MAX_VALUE);
}
/**
* method to conduct a quiz based on given complexity range
*/
public void giveQuiz(int minComplexity, int maxComplexity) {
int num = 0; //question number/count
//number of right and wrong questions
int right = 0, wrong = 0;
//scanner to read answers
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < numQuestions; i++) {
//asking the question if it is under given complexity range
if (questions[i].getComplexity() >= minComplexity
&& questions[i].getComplexity() <= maxComplexity) {
num++;
System.out.print("Q" + num + "): ");
System.out.println(questions[i].getQuestion());
//getting answer
String answer = scanner.nextLine();
//validating answer
if (questions[i].answerCorrect(answer)) {
right++;
System.out.println("Right answer!");
} else {
wrong++;
System.out.println("Wrong answer!");
}
}
}
//displaying the quiz stats
System.out.println("Quiz completed!");
System.out.println("Number of questions: " + num);
System.out.println("Right answers: " + right);
System.out.println("Wrong answers: " + wrong);
double percentage = (double) right / num;
percentage *= 100;
System.out.printf("Win percentage: %.2f%% ", percentage);
}
}
// QuizTime.java
import java.util.Scanner;
public class QuizTime {
/**
* method to create a Quiz, add some questions and return the Quiz object.
*
* @return quiz object created
*/
static Quiz createQuiz() {
/**
* Creating some questions with complexity 1, 2 and 3. Note that, for
* the sake of demonstration I'm only defining simple questions. You can
* create your own questions if you like.
*/
Question q1 = new Question("What is 1+1?", "2");
q1.setComplexity(1);
Question q2 = new Question("What is 7-2?", "5");
q2.setComplexity(1);
Question q3 = new Question("What is 5+3?", "8");
q3.setComplexity(1);
Question q4 = new Question("What is 5*3?", "15");
q4.setComplexity(2);
Question q5 = new Question("What is 6*9?", "54");
q5.setComplexity(2);
Question q6 = new Question("What is 7*8?", "56");
q6.setComplexity(2);
Question q7 = new Question("What is 20/4?", "5");
q7.setComplexity(3);
Question q8 = new Question("What is 100/25", "4");
q8.setComplexity(3);
Question q9 = new Question("What is 144/12?", "12");
q9.setComplexity(3);
Question q10 = new Question("What is 123/3?", "41");
q10.setComplexity(3);
//creating a Quiz object, adding all created questions
Quiz quiz = new Quiz();
quiz.addQuestion(q1);
quiz.addQuestion(q2);
quiz.addQuestion(q3);
quiz.addQuestion(q4);
quiz.addQuestion(q5);
quiz.addQuestion(q6);
quiz.addQuestion(q7);
quiz.addQuestion(q8);
quiz.addQuestion(q9);
quiz.addQuestion(q10);
return quiz;
}
public static void main(String[] args) {
//scanner to read user input
Scanner scanner = new Scanner(System.in);
//creating Quiz
Quiz quiz = createQuiz();
String choice = "";
//looping until user wish to quit (allowing multiple quizzes)
while (!choice.equalsIgnoreCase("q")) {
//displaying options
System.out.println("a. Quiz type 1 (All Questions)");
System.out.println("b. Quiz type 2 (Selected complexity range)");
System.out.println("q. Quit");
System.out.print("Your choice: ");
choice = scanner.nextLine();
if (choice.equalsIgnoreCase("a")) {
//first type, questions in all complexities
quiz.giveQuiz();
} else if (choice.equalsIgnoreCase("b")) {
//getting min and max complexity range
System.out.print("Enter minimum complexity (1-3): ");
int min = Integer.parseInt(scanner.nextLine());
System.out.print("Enter maximum complexity (1-3): ");
int max = Integer.parseInt(scanner.nextLine());
//validating range
if (min < 1 || min > 3 || max < 1 || max > 3 || min > max) {
System.out.println("Invalid complexity!");
} else {
//starting 2nd type of quiz
quiz.giveQuiz(min, max);
}
}
}
}
}
/*OUTPUT*/
a. Quiz type 1 (All Questions)
b. Quiz type 2 (Selected complexity range)
q. Quit
Your choice: a
Q1): What is 1+1?
2
Right answer!
Q2): What is 7-2?
5
Right answer!
Q3): What is 5+3?
8
Right answer!
Q4): What is 5*3?
14
Wrong answer!
Q5): What is 6*9?
54
Right answer!
Q6): What is 7*8?
55
Wrong answer!
Q7): What is 20/4?
5
Right answer!
Q8): What is 100/25
4
Right answer!
Q9): What is 144/12?
12
Right answer!
Q10): What is 123/3?
41
Right answer!
Quiz completed!
Number of questions: 10
Right answers: 8
Wrong answers: 2
Win percentage: 80.00%
a. Quiz type 1 (All Questions)
b. Quiz type 2 (Selected complexity range)
q. Quit
Your choice: b
Enter minimum complexity: (1-3)2
Enter maximum complexity: (1-3)2
Q1): What is 5*3?
15
Right answer!
Q2): What is 6*9?
54
Right answer!
Q3): What is 7*8?
56
Right answer!
Quiz completed!
Number of questions: 3
Right answers: 3
Wrong answers: 0
Win percentage: 100.00%
a. Quiz type 1 (All Questions)
b. Quiz type 2 (Selected complexity range)
q. Quit
Your choice: q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.