(^134) | Arithmetic Expressions
// Returns name as last, first, m.
publicString lastFirstMI()
{
returnlast + ", "+ first + ", "+ middle.substring(0, 1) + ".";
}
}
To show how we would use this package, we can rewrite the NameDriverapplication. We
simply remove the class from the application itself and include an importdeclaration for
the name package. Let’s call the application NewNameDriver.
//**
// NewNameDriver application
// This application inputs a name and prints it in
// two different formats
//**
importjava.io.*; // Package for stream readers
importName; // Name class
public classNewNameDriver
{
public static voidmain(String[] args) throwsIOException
{
Name testName; // Declare a name
testName = new Name(); // Instantiate a name
System.out.println("Name in first-last format is "+
testName.firstLast()); // First format
System.out.println("Name in last-first-initial format is "+
testName.lastFirstMI()); // Second format
}
}
Now we can really see how object-oriented programming helps simplify application de-
velopment. The NewNameDriverapplication is very short and easy to understand because we’ve
separated the details of how a name is created and accessed into a class. What’s more, we
can now use Nameobjects in other problems merely by importing the Nameclass. In fact, that’s
precisely what we will do in the Case Study.
やまだぃちぅ
(やまだぃちぅ)
#1