2-A Modified MiniQuiz Class
2-A Modified MiniQuiz Class
Files Question.java, Complexity.java, and MiniQuiz.java contain the classes in Listings 6.8-6.10of the text. These classes
demonstrate the use of the Complexity interface; class Question implements the interface, and class MiniQuiz creates two
Question objects and uses them to give the user a short quiz.
Save these three files to your directory and study the code in MiniQuiz.java. Notice that after the Question objects are
created, almost exactly the same code appears twice, once to ask and grade the first question, and again to ask and grade the
second question. Another approach is to write a method askQuestion that takes a Question object and does all the work of
asking the user the question, getting the user’s response, and determining whether the response is correct. You could then
simply call this method twice, once for q1 and once for q2. Modify the MiniQuiz class so that it has such an askQuestion
method, and replace the code in main that asks and grades the questions with two calls to askQuestion. Some things to keep
in mind:
The definition of askQuestion should be inside the MiniQuiz class but after the main method.
Since main is a static method, askQuestion must be static too. (A static method cannot call an instance method of the
same class.) Also, askQuestion is for use only by this class, so it should be declared private. So the header for
askQuestion should look like this:
• String possible, which is currently declared in main, will need to be defined in askQuestion instead.
• The Scanner object scan needs to be a static variable and moved outside of main (so it is available to askQuestion).
• You do not need to make any changes to Question.java or Complexity .java.
//****************************************************************
// Question.java Author: Lewis/Loftus
//
// Represents a question (and its answer).
//****************************************************************
//--------------------------------------------------------------
// 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()
//--------------------------------------------------------------
// 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 + "\n" + answer;
}
}
//*****************************************************************
// Complexity.java Author: Lewis/Loftus
//
// Represents the interface for an object that can be assigned an
// explicit complexity.
//*****************************************************************
import java.util.Scanner;
System.out.print (q1.getQuestion());
System.out.println (" (Level: " + q1.getComplexity() + ")");
possible = scan.nextLine();
if (q1.answerCorrect(possible))
System.out.println ("Correct");
else
System.out.println ("No, the answer is " + q1.getAnswer());
System.out.println();
System.out.print (q2.getQuestion());
System.out.println (" (Level: " + q2.getComplexity() + ")");
possible = scan.nextLine();
if (q2.answerCorrect(possible))
System.out.println ("Correct");
else
System.out.println ("No, the answer is " + q2.getAnswer ());
}
}