//An instance of this class provides methods to analyze a full name // The name may have any number (1 or more) of name parts. // The analyzer processes only the first, second and last. //PUBLIC INTERFACE // Constructor // NameAnalyzer(String name) // @parameter name: 1 or more name parts separated by 1 or more spaces // Exception if parameter name has no printable chars // Accessor // String getName() // Other Methods // String firstName() : 1st name part // String secondName() : 2nd name part, null if 2 or fewer parts // String lastName() : last name part, null if 1 part only // String briefName() : in the format First S. Last // String listName() : in the format Last, First S. // String initials() : in the format FSL, or FL, or F based on # of name parts // String fullName() : all name parts, capitalized, separated by 1 space // import javax.swing.*; public class NameAnalyzer { //Instance Variable private String name; //Constructor public NameAnalyzer(String name) { this.name = name.trim(); if ( this.name.length() < 1 ) throw new RuntimeException("No-Name"); } //Accessor public String getName() { return this.name; } //Returns the first name part of the instance variable public String firstName() { String firstPart = name; int spaceIndex = this.name.indexOf(' '); if (spaceIndex != -1) firstPart = this.name.substring(0, spaceIndex); return capitalized(firstPart); } //Returns the last name part of the instance variable // or null if the instance variable has only 1 part public String lastName() { int spaceIndex = this.name.lastIndexOf(' '); if (spaceIndex == -1) return null; return capitalized(this.name.substring(spaceIndex + 1)); } //Returns the second name part of this instance variable // or null if the instance variable has fewer than 3 parts public String secondName() { int spaceIndex = this.name.indexOf(' '); if (spaceIndex == -1) return null; String remainder = this.name.substring(spaceIndex).trim(); spaceIndex = remainder.indexOf(' '); if (spaceIndex == -1) return null; return capitalized(remainder.substring(0, spaceIndex)); } //Returns the usual abbreviated format of the instance variable // First S. Last 3 or more name parts (S is the 2nd name initial) // First Last exactly 2 name parts // First only 1 name part public String briefName() { String name = this.firstName(); if (this.secondName() != null) name = name + " " + this.secondName().charAt(0) + "."; if (this.lastName() != null) name = name + " " + this.lastName(); return name; } //Returns the listing format of the instance variable // Last, First S. 3 or more name parts (S is the 2nd name initial) // Last, First exactly 2 name parts // First only 1 name part public String listName() { if (this.lastName() == null) return this.firstName(); String name = this.lastName() + ", " + this.firstName(); if (this.secondName() != null) name = name + ' ' + this.secondName().charAt(0) + '.'; return name; } //Returns the initials of the first, second and last names // FSL 3 or more name parts (S is the 2nd name initial) // FL exactly 2 name parts // F only 1 name part public String initials() { return "" + this.firstName().charAt(0) + (this.secondName() == null ? "" : this.secondName().charAt(0)) + (this.lastName() == null ? "" : this.lastName().charAt(0)); } public String fullName() { return standardForm(this.name); } //Returns a full name (all name parts) in standard form: // (a) capitalized name parts // (b) exactly 1 space between consectutive name parts // (c) no leading or trailing white-space private static String standardForm(String name) { int index = name.indexOf(' '); if (index == -1) return capitalized(name); return capitalized( name.substring(0, index) ) + ' ' + standardForm(name.substring(index).trim() ); } //Returns a simple name with // (a) first letterin uppercase // (b) all other letters lowercase private static String capitalized (String name) { return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase(); } }