0% found this document useful (0 votes)
5 views3 pages

2-A Modified MiniQuiz Class

The document describes modifications to the MiniQuiz class, which includes creating an askQuestion method to streamline the process of asking and grading quiz questions. It provides guidelines for implementing this method, such as making it static and private, and moving the Scanner object outside of the main method. Additionally, it includes the code for the Question and Complexity classes that are used in the MiniQuiz class.

Uploaded by

vangelgable5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

2-A Modified MiniQuiz Class

The document describes modifications to the MiniQuiz class, which includes creating an askQuestion method to streamline the process of asking and grading quiz questions. It provides guidelines for implementing this method, such as making it static and private, and moving the Scanner object outside of the main method. Additionally, it includes the code for the Question and Complexity classes that are used in the MiniQuiz class.

Uploaded by

vangelgable5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

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 Author: Lewis/Loftus
//
// 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()

106 Chapter 7: Object-Oriented Design


{
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 + "\n" + answer;
}
}

//*****************************************************************
// Complexity.java Author: Lewis/Loftus
//
// Represents the interface for an object that can be assigned an
// explicit complexity.
//*****************************************************************

public interface Complexity


{
public void setComplexity (int complexity);
public int getComplexity();
}

Chapter 7: Object-Oriented Design 107


//*****************************************************************
// MiniQuiz.java Author: Lewis/Loftus
//
// 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 ());
}
}

108 Chapter 7: Object-Oriented Design

You might also like