Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A Modified MiniQuiz Class Files Question.java, Complexity.java, and MiniQuiz.java contain the classes. These classes demonstrate the use of the Complexity interface; class Question implements the

A Modified MiniQuiz Class

Files Question.java, Complexity.java, and MiniQuiz.java contain the classes. 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 users 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:

private static void askQuestion(Question question)

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

//

// Represents a question (and its answer).

//****************************************************************

public class Question implements Complexity

{

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.

//--------------------------------------------------------------

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;

}

}

//*****************************************************************

// Complexity.java

//

// Represents the interface for an object that can be assigned an

// explicit complexity.

//*****************************************************************

public interface Complexity

{

public void setComplexity (int complexity);

public int getComplexity();

}

//*****************************************************************

// MiniQuiz.java

//

// Demonstrates the use of a class that implements an interface.

//*****************************************************************

import java.util.Scanner;

public class MiniQuiz

{

//--------------------------------------------------------------

// Presents a short quiz.

//--------------------------------------------------------------

public static void main (String[] args)

{

Question q1, q2;

String possible;

Scanner scan = new Scanner(System.in);

q1 = new Question ("What is the capital of Jamaica?",

"Kingston");

q1.setComplexity (4);

q2 = new Question ("Which is worse, ignorance or apathy?",

"I don't know and I don't care");

q2.setComplexity (10);

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 ());

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions