Programming and Problem Solving with Java

(やまだぃちぅ) #1
3.9 Applications with Multiple Class Files | 133

//**

// This class provides a basic name object. The default constructor
// requests that a name be entered from System.in. A second constructor
// allows creation of a name from strings. Methods return the name
// in various formats.
//**
importjava.io.*; // Package for stream readers
className
{
String first; // Person's first name
String last; // Person's last name
String middle; // Person's middle name


// Gets a name from System.in
publicName() throwsIOException
{
BufferedReader in; // Input stream for strings
// Instantiates in using System.in
in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter first name: "); // Prompt for first name
first = in.readLine(); // Get first name
System.out.print("Enter last name: "); // Prompt for last name
last = in.readLine(); // Get last name
System.out.print("Enter middle name: "); // Prompt for middle name
middle = in.readLine(); // Get middle name
}

// Builds a name from string parameters
publicName(String firstName, String lastName, String middleName)
{
first = firstName; // Assign parameters to fields
last = lastName;
middle = middleName;
}

// Returns name in first last format
publicString firstLast()
{
returnfirst + " " + last;
}

// Returns full name in usual format
publicString full()
{
returnfirst + " " + middle + " " + last;
}
Free download pdf