106 Chapter 7: Object-Oriented Design
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
//————————————————————–
// Sets up the question with a default complexity.
//————————————————————–
public Question (String query, String result)
{
question = query;
//————————————————————–
// Returns the complexity level for this question.
//————————————————————–
public int getComplexity()