Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^182) | Selection and Encapsulation
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;
}
// Basic observer methods that return the value of each field
publicString knowFirstName()
{
returnfirst;
}
publicString knowMiddleName()
{
returnmiddle;
}
publicString knowLastName()
{
returnlast;
}
// Additional observer methods that return a formatted name
// Returns name in first last format
publicString firstLast()
{
returnfirst + " " + last;
}
// Returns full name in usual format
publicString full()
{
returnfirst + " " + middle + " " + last;
}

Free download pdf