Upload
juniper-peters
View
213
Download
0
Embed Size (px)
Citation preview
Quizard
step-by-step
Start with the questions
• The Question class is abstract– a Superclass
• You’ll never create something that’s just a question.
• It’ll always be a specific type of question
Top-Down Design:refinement
Questions —
Classes that represent different types of questions.
«abstract»Question
MCQuestion TFQuestion
FRQuestion
TestQuestion
Create a test case for testing in isolation
Question class
• What does every question have?• These are the kinds of questions:
1. multiple choice2. true/false3. and free response
• They all have:– the words describing the question (including a
description of answer choices if appropriate)– correct answer– point value
constructor
• Always 3 parameters given:– question, answer and number of points are all
initialized when the question is created.
• never changed.
• don’t need mutators
• only accessors
«abstract»Question
String questionString answer
int points
MCQuestion TFQuestion
FRQuestion
What methods do all questions have in common?
Accessors:
getQuestion()
getAnswer()
getPoints()
A way to display the question
toString()
/** * Compare this question's correct answer with the entered answer * @param userAns the entered answer (in upper case) * @return 1 if userAns matches this question's answer, 0 if there is no match, * and -1 if userAns is invalid */ public abstract int compareAns(String ans);
Each subclass must define compareAns for that particular type of question.
NOT defined in Question
Fill in the code for the Question class
Accessors:
getQuestion()
getAnswer()
getPoints()
A way to display the question
toString()
abstract method
public abstract int compareAns(String ans);
«abstract»Question
String questionTextString answerText
int numPoints
«abstract»Question
String questionTextString answerText
int numPoints
MCQuestion
ArrayList ansOpts
TFQuestion
FRQuestion
How are the question subclasses unique?
Answer Options:
MC = ArrayList of choices
TF = always T/F, part of the question text
FR = no answer option given, user must enter
Some way to display the question
toString(), same for TF and MC, different for MC
/** * Compare this question's correct answer with the entered answer*/ public int compareAns(String ans)MC = user enters a letter, must be A-F (ignore case) compare to correct letter answerTF = user enters a letter, must be T or F, compare to correct letter answerFR= user enters words, to keep it simple, only an exact match counts (ignoring case)
Examples TF
From the text file:
TF 3Q: True or false: The chicken crossed the road to get to the other sideA: T
Question tester = new TFQuestion(“True or false: The chicken crossed the road to get to the other side”, “T”,3);
tester.getPoints(); // = 3 inherited from Question
tester.compareAnswer(“F”); // = 0, doesn’t match, TF method
tester.getAnswer(); // = “T” inherited from Question
«abstract»Question
String questionTextString answerText
int numPoints
TFQuestion
only code needed for TF• constructor: call the superclass, assume you are given question,
answer and points as parameters to the constructor.
• public int compareAns(String userAns)• {• if they didn’t answer T or F• return -1; // Invalid answer• else if the user answer = the correct answer• return 1; // correct• else• return 0; // incorrect• }
make TFQuestion
«abstract»Question
String questionTextString answerText
int numPoints
TFQuestion
Examples FR
From the text file:
FR 3Q: Name a random integer between 2.5 and 3.5A: 3
Question tester = new FRQuestion(“Name a random integer between 2.5 and 3.5”, “3”,3);
tester.getPoints(); // = 3
tester.compareAnswer(“8”); // = 0, doesn’t match
tester.getAnswer(); // = “3”
only code needed for FR
• constructor: call the superclass, assume you are given question, answer and points as parameters to the constructor.
• public int compareAns(String userAns)• {• if the user answer = the correct answer
(ignoring any differences in case)• return 1; // correct• else• return 0; // incorrect• }
make FRQuestion
«abstract»Question
String questionTextString answerText
int numPoints
FRQuestion
Examples MCFrom the text file:
MC 8Q: What is encapsulation? A. Being abducted by a UFO B. A 20-second news report on MSNBC C. Calling String's toUpperCase() D. Making all data members of a class privateA: D
When constructing a MC question public MCQuestion(String qtn, ArrayList options, String ans, int pts) { call the superclass constructor to make a question setting qtn, ans and pts instantiate the arraylist for this object copy all the items in the options arraylist into the instance variable arraylist }
MCQuestion
ArrayList ansOpts
write the constructor for a MCQuestion
getQuestion for MCoverrides getQuestion in Question
MCQuestion
ArrayList ansOpts
public String getQuestion() {String str; start by calling getQuestion() from superclass and adding an end-of-line character to it to create the string. for (a counter goes from 0 to the size of the ansOpts arraylist) get the next ansOpt, add it to str with an end-of-line mark at the end of each. return str; }
write the getQuestion method for a MCQuestion
• public int compareAns(String userAns)• {• if (the user answer is more than one letter long)• return -1; // invalid
• if (the user answer is not a letter from A to E)• return -1; // invalid
• if (the user answer is the same as the question answer)• return 1; // correct• else• return 0; // incorrect• }
Write the compareAns method for MCQuestion
Test the questionsimport java.util.ArrayList;
public class TestQuestion{ public static void main(String[] args) { System.out.println("Testing Questions...\n");
/* Free-response question */
Question fr = new FRQuestion("What kind of question is this?", "FR", 7); System.out.println(fr); System.out.println("Compare answers (should be 0 1): " + fr.compareAns("XYZ") + " " + fr.compareAns("FR")); System.out.println();
/* True or false question */
Question tf = new TFQuestion("This is a true/false Question", "T", 5);
System.out.println(tf); System.out.println("Compare answers (should be -1 0 1): " + tf.compareAns("X") + " " + tf.compareAns("F") + " " + tf.compareAns("T")); System.out.println();
/* Multiple-choice question */
ArrayList options = new ArrayList(); options.add(" A. Free response"); options.add(" B. Multiple choice"); options.add(" C. True/false"); MCQuestion mc = new MCQuestion("What kind of question is this?", options, "B", 10); System.out.println(mc); System.out.println("Compare answers (should be -1 0 1): " + mc.compareAns("X") + " " + mc.compareAns("C") + " " + mc.compareAns("B")); System.out.println(); }
Test ReaderWhy test it? To take the time to look at the code and
methods used in reading the input file to understand them.
• public class TestReader• {• public static void main(String[] args)• {• QuizReader reader = new QuizReader("test.dat");
• Question qtn;• while ((qtn = reader.nextQuestion()) != null)• System.out.println(qtn);• }• }