Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY^309


Responsibility Algorithms for Addressand Entry:This application is not using the
nonparameterized constructors of the classes Addressand Entry. Instead, we leave their
implementations as a Case Study Follow-Up exercise. The remaining responsibilities
are knowledge responsibilities that need no further refinement. However, we do need
to decide what the file that Entryis writing should look like. We have two choices: (1)
We can make it look like an address by printing the name on the first line, the address
on two lines, and the phone number as a string with hyphens or (2) we can print each
string or number on a line by itself. The address format is easy to create—so why would
we even consider the one string per line format? The address format is easy to write,
but it is not easy for another application to read. If we write the name all on one line,
we must read the string back in and break the name apart. The same is true of the
address and the phone number. Because the intent of this application is to create the
beginnings of an address book that will be processed on the computer, we know that
the values will be read in at a later time. Here we write them out one string per line for
ease of subsequent input. In the next chapter, we introduce an even easier way to read
the values into an object.


Implementation:Now we are ready to code these classes. The only question is whether
to code “Process entries” as one method or to break up the subalgorithms into helper
methods. It would be better style to make use of helper methods. However, this special-
purpose driver probably will not be used again, so let’s simply code it in main.
Here is the code for Address,Entry, and AddressDr.Nameand Phonewere defined
previously. A copy of the input screen and the output file follow the code.


//**
// This class provides a basic address object. The constructor
// takes the state variables as strings. Four knowledge
// methods return the state values.
//**


packageaddressBook;
// Includes Name, Address, Phone, and Entry
public classAddress
{
// Instance variables
String street;
String city;
String state;
String zipCode;


publicAddress(String newStreet, String newCity, String newState,
String zip)
{
Free download pdf