Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


502


GRADING TRUE/FALSE TESTS
Problem Statement:Your history teacher gives true/false exams. Knowing that you are
studying computer science, she asks you to write an application that grades true/false
questions. The answer key to the exam is on the first line of data, followed by a line
with the student’s name and then a line with his or her answers. Your teacher gives you
the following example of the file to use as a guideline.

TFTFTFTFTTTFFFT
Joe Jones
TFTFTFTFTTTFFTT
Janet Jerome
TFTFTFTFTTTFFFF
Jeff Jubilee
TFTFTFTFTTTFFFT
...

As output, she wants the student’s name followed by the number of questions
answered correctly, written on a file with one student’s data per line.

Brainstorming:The relevant objects in the problem statement are students, exam
answers, and the key. The students each consist of a name. The exam answers are a se-
ries of characters (T’s and F’s). The key is another series of characters.

Filtering:Let’s look at the key object first. An exam is made up of questions, the answers
to which are either T or F. Therefore, the key is an ordered collection of T’s and F’s that
represents the correct answers to the questions. The student object contains a name
and is associated with a set of exam answers, which is a sequence of T’s and F’s. The de-
scription of the key and the exam answers are identical. Viewed in this light, a student
really isn’t an important object; it is a student’s answer sheetthat is the object. An
answer sheet is made up of a name and a sequence of T’s and F’s. If we think of the an-
swer key as having the name “Answer Key,” then the answer key and a student’s answer
sheet are instances of the same class: an answer sheet. Finally, we need a driver or ap-
plication class that coordinates the interaction of the other classes.

input filenp f
output fileoupu fi
keyky
exam answerxam answ r
ssudntudent
Free download pdf