Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


84


Our two value-returning methods use straightforward string expressions that are
identical to those in the original problem.

We are now ready to write the code for the class. What about the application? Our
application is really just meant to test the class. Software engineers refer to such a test
application as a driver.

It’s hard to believe that the mainmethod has been reduced to just four steps, but that’s
the beauty of object-oriented programming.
Now we’re ready to write the whole application. We also include comments as
necessary.

//******************************************************************
// NameDriver application
// This application inputs a name and prints it in
// two different formats
//******************************************************************
importjava.io.*; // Package for stream readers
public classNameDriver
{
static className
{
String first; // Person’s first name

Name Driver
Define a Name testName
Instantiate a Name object and assign it to testName
Print First Last Format
Print Last First Middle Format

Last First Middle Format
returnlast + “, “ + first + ”, “ + middle + “.”

First Last Format
returnfirst + “ “ + last

Name Constructor
Define a BufferedReader in
Instantiate a BufferedReader using an InputStreamReader using System.in, and
assign it to in
Prompt for first name on System.out
Get first name from in
Prompt for last name on System.out
Get last name from in
Prompt for middle initial on System.out
Get middle initial from in
Free download pdf