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

A Modified MiniQuiz Class Files Question.java, Complexity.java, and MiniQuiz.jav

ID: 651692 • Letter: A

Question

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 user

Explanation / Answer

Program code:

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

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

}

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

// 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;

     }

}

import java.util.*;

public class MiniQuiz

{

     //static Scanner object

     static Scanner scan = new Scanner(System.in);

     //main method

     public static void main(String args[])

     {

          //Objects for the Question class

          Question q1, q2;       

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

          //call the method by passing the Question object

          //call by passing the first question

          askQuestion(q1);

          //call by passing the second question

          askQuestion(q2);       

     }

     //definition of askQuestion which is a private static method

     //that takes Question object as parameter.

     private static void askQuestion(Question q)

     {

          String possible;

          System.out.print(q.getQuestion());

          System.out.println(" (Level: " + q.getComplexity() + ")");

          possible = scan.nextLine();

          if (q.answerCorrect(possible))

          {

              System.out.println("Correct");

          } else

          {

              System.out.println("No, the answer is " + q.getAnswer());

          }

     }

}

Sample Output:

What is the capital of Jamaica? (Level: 4)

Kingston

Correct

Which is worse, ignorance or apathy? (Level: 10)

ignorance

No, the answer is I don't know and I don't care

Note:

The required modified code is highlighted in bold letters. The askQuestion method is added in the MiniQuiz class as a private static method that is called twice from the main method by passing different question objects.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote