Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


504


Data Representation:The exam answers are an ordered collection of T’s and F’s. An
array is the obvious choice for holding the T’s and F’s. The index of an individual answer
is the question number minus one. Now we are ready to design the algorithms for the
two classes.

Responsibility Algorithms for AnswerSheet: We have forgotten something. How do we
know how many questions were asked on this exam? We must know this information
to instantiate the objects of class AnswerSheet. The user could enter it from the keyboard
as an event, or the user could enter the number of questions on the file, on the line im-
mediately before the key. Because this application does not need the user to be present
to enter any other data, putting the number of questions on the data file is the better
choice. The responsibility for reading this value should be in the driver.

There is another problem: A student’s answer sheet contains a name that is printed
along with the number correct. The key doesn’t have a name to be read. We can have
two input methods, one that reads a name and the sequence of T’s and F’s and one that
reads just the T’s and F’s. Another alternative is to have the instructor supply “Answer
Key” for the name. This second alternative is simpler because it requires only one input
method. Here, then, is what the input file would look like.
15
Answer Key
TFTFTFTFTTTFFFT
Joe Jones
TFTFTFTFTTTFFTT
Janet Jerome
TFTFTFTFTTTFFFF
Jeff Jubilee
TFTFTFTFTTTFFFT
...

Answer Sheet (int numQuestions)
Create responses as an array of numQuestions characters

Class Name: GradeExams Superclass: Object Subclasses:


Responsibilities Collaborations
Prepare files for input

Prepare files for otput FilerWriter, PrintWriter


FileReader, BufferedReader


Get key AnswerSheet
Process exams
Close files

AnswerSheet
None
Free download pdf