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

in java •Create a new class called Question. This class should contain instance

ID: 3718679 • Letter: I

Question

in java

•Create a new class called Question. This class should contain instance variables to store the question, its correct answer, and the complexity level of the question.

•Create a method for this class that receives an answer as its input and verifies if it matches the correct answer.

•Use the Question class to define a Quiz class. A quiz can be composed of up to 20 questions.

•Define a method of the Quiz class to add a question to a quiz.

•Define the giveQuiz method of this class to present each question in turn to the user, accept an answer for each one, and keep track of the results (the results should be returned).

•Write a driver program (QuizTime) that populates a quiz, presents it, and prints the final results.

Explanation / Answer

Question Class-------------------------------------------------

package quiztime;


public class Question {
public String ques;
public String ans;
public int complexity;
public boolean isCorrect(String answer){
if (this.ans.equalsIgnoreCase(answer)) {
return true;
}else{
return false;
}
}
  
}

Quiz Class-------------------------------------------------------------

package quiztime;
import java.util.Scanner;

public class Quiz extends Question{
public Question[] ques= new Question[20];
public int ind=0;
public int res=0;
public int qInd=0;
public void addQues(String qu,String ans){
Question q = new Question();
q.ques=qu;
q.ans=ans;
q.complexity=1;
ques[ind]=q;
ind++;
}
public void giveQuiz(){
Scanner sc=new Scanner(System.in);
System.out.println(ques[qInd].ques);
String ans=sc.nextLine();
if (ques[qInd].isCorrect(ans)==true) {
res++;
}
qInd++;
}
  
}

QuizTime Class ----------------------------------

package quiztime;


public class QuizTime {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Quiz quiz = new Quiz();
// add questions here
quiz.addQues("Capital of USA?", "DC");
quiz.addQues("Color of sky?", "blue");
quiz.addQues("Days in a week?", "7");
for (int i = 0; i < 3; i++) {
quiz.giveQuiz();   
}
System.out.println("YOur score" );
System.out.println( quiz.res);
}
  
}