CASE STUDY
506
How do we know whether there are more exams? Reading is done within the input
method, so checking for the end of the data must be done there as well. If input of the
line containing the name returns null, then the line with the T’s and F’s should not be
read. How does the inputmethod let the driver know when the end of file has been
reached? We must add an additional Boolean method to the class AnswerSheetthat
returns trueif there is more data, and falseotherwise. Here is the revised algorithm for
the inputmethod and the algorithm for the moreDatamethod.
//**
// This class provides a name object and an array of T’s and F’s
// representing answers on a true/false test.
//**************************************************************
packagegrader;
importjava.io.*;
public classAnswerSheet
{
private char[] responses; // Contains T’s and F’s
private intnumItems; // Number of questions
privateString name;
publicAnswerSheet(intnumQuestions) // Constructor
{
responses = new char[numQuestions];
numItems = numQuestions;
}
public voidinput(BufferedReader inFile) throwsIOException
// Name and sequence of T’s and F’s are read
{
name = inFile.readLine();
if(name != null)
responses = inFile.readLine().toCharArray();
}
Boolean moreData()
returnname != null
input (BufferedReader inFile) Revised
Set name to inFile.readLine()
if(name != null)
Set responses to inFile.readLine().toCharArray()