Sunteți pe pagina 1din 2

CPSC 1181 - Lab 4 [45 marks]

Objectives:
• Create subclasses
• Use visibility modifiers to maintain encapsulation during inheritance
• Override methods from super classes in subclasses
Instructions:
• Complete all of the exercises and submit your zip file prior to the due date.
Submission:
• Zip up your NumericQuestion.java, FillInQuestion.java, Question.java, Quiz.java, and QuizTime.java and
submit them to D2L. Due date set in D2L
• Submissions that are less than 24 hours late receive a 1% per hour late penalty. Submission that
are more than 24 hours late will not be accepted.
• Unzipped submissions or submissions containing .class or other unneeded files will be penalized.

Exercise 1
In this lab you are going to be implementing the inheritance hierarchy shown below. You’ve already done Quiz and
Question.
Modify your Question class to have a String askQuestion() method returns a string containing the question and
difficulty of the current question. Modify Quiz to use this askQuestion method when displaying a question to the user.
You should not modify Question or Quiz in any other way (unless there are errors to fix).

Exercise 2
Create a class called FillInQuestion that inherits from Question. This kind of question is created using a single string
that contains the answer, surrounded by _- -_. A sample question like this would be “The color _-white-_ is the most
common color found on flags.” The question should be displayed to the user as “The color _____ is the most
common color found on flags.”

The first line of the constructor must be a call to Question’s constructor using super. But you need to process the
String before calling super. Do the following…
CPSC 1181 - Lab 4 [45 marks]

• Write two private static methods String parseQ(String) and String parseA(String) These take the constructor’s
parameter and return the computed question/answer as specified above
o So parseQ(“The sky is _-blue-_”) might return “The sky is _________”
• Now your constructor might look like…
public FillInQuestion(String sentence, Diffenum diff) {
super(parseQ(sentence), parseA(sentence),diff) on the first line.

If FillInQuestion’s parameter String does not contain _- and -_ in the correct order, throw an
IllegalArgumentException. (You can throw this in the parseQ method)

Modify one of your quiz objects in QuizTime.java to contain some FillInQuestions.

Write a Junit test class that tests the constructor is working properly. (The other methods are already tested by the
Junit test class for Question)

Exercise 3
Create a class called NumericQuestion that is a child of Question. The constructor takes two double parameters, the
first is the correct answer, the second is the tolerance. (If an attempted answer is within the tolerance value of the
correct answer, it is considered correct). Override the checkAnswer method to properly check an attempted answer.
• You can assume the attempted answer given to checkAnswer will convert to a double without throwing
an exception
Modify one of your quiz objects in QuizTime.java to contain some NumericQuestions.
Write a Junit test class that tests the constructor and new version of checkAnswer are working properly.

• Always use @Override where appropriate

Marking Rubric:
Style, Convention [5 marks]
Documentation [5 marks] FillInQuestion.java and Junit Test [14 marks]
QuizTime.java[6 marks]
Modifications to Question.java [2 marks]
Modifications to Quiz.java [1 marks]

NumericQuestion.java and Junit Test [12 marks]

S-ar putea să vă placă și