You will create a Name class to manage different name changes of a person. Varia
ID: 3634581 • Letter: Y
Question
You will create a Name class to manage different namechanges of a person.
Variables:
Full Name (e.g. “Hillary Rodham Clinton”)
First Name (e.g. “Hillary”)
Middle Name (e.g. “Diane”)
Last Name (e.g. “Clinton”)
Maiden Name (e.g. “Rodham”)
Number of Names (Hint: static variable)
Constructors:
Name(): Initializes full and first names to “Anonymous” and middle, last
and maiden names to empty string.
Name(String fullName): Initializes full name to fullName and initializes
first, middle and last names appropriately. If only 2 names are in the
fullName, middle name is set to empty string. For all cases, maiden name
is set to empty string.
Method
getFullName(), getFirstName(), getMiddleName(), getLastName(),
getMaidenName(): Return appropriate name. They are all public.
getMiddleInitial(): Returns middle initial.
changeFirstName(String newFirst), changeMiddleName(String
newMiddle): Changes appropriate name and full name. They are all
public.
changeLastName(String newLast): Changes last name. Ask the user if
maiden name should be changed and accept “Yes”, “yes”, “No” and “no”
as answers. If “Yes” or “yes”, then call private changeMaidenName()
method and current last name becomes maiden name and newLast
become new last name.
changeMaidenName(): This method is private. Changes maiden name.
displayNames(): Displays names in the appropriate format.
Explanation / Answer
Dear
// Name.java
public class Name {
// names
private String fullName;
private String firstName;
private String middleName;
private String lastName;
private String maidenName;
// number of names
private static int numberOfNames = 0;
// no argument constructor
public Name() {
fullName = firstName = "Anonymous";
middleName = lastName = maidenName = "";
}
// 1-argument constructor
public Name(String fullName) {
this.fullName = fullName;
String[] names = fullName.split(" ");
if (names.length > 2) {
firstName = names[0];
middleName = names[1];
lastName = names[2];
} else if (names.length == 2) {
firstName = names[0];
middleName = names[1];
} else if (names.length == 1)
firstName = names[0];
maidenName = "";
}
// returns full name
public String getFullName() {
return fullName;
}
// returns first name
public String getFirstName() {
return firstName;
}
// returns middle name
public String getMiddleName() {
return middleName;
}
// returns last name
public String getLastName() {
return lastName;
}
// returns maiden name
public String getMaidenName() {
return maidenName;
}
// returns initial of middle name
public char getMiddleInitial() {
if (middleName != "")
return Character.toUpperCase(middleName.charAt(0));
else
return ' ';
}
// changes first name and updates it in full name
public void changeFirstName(String newFirst) {
firstName = newFirst;
fullName = firstName + middleName + maidenName + lastName;
}
//changes middle name and updates it in full name
public void changeMiddleName(String newMiddle) {
middleName = newMiddle;
fullName = firstName + middleName + maidenName + lastName;
}
//changes last name and updates it in full name
//asks for user whether the maiden name can modify or not
public void changeLastName(String newLast) {
String choise;
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Do you want to change the maiden name (yes or no)? ");
choise = input.nextLine();
if (choise.equalsIgnoreCase("yes"))
changeMaidenName(this.lastName);
this.lastName = newLast;
fullName = firstName + middleName + maidenName + lastName;
}
// private method to change maiden name
private void changeMaidenName(String newMaiden) {
maidenName = newMaiden;
}
// display the names
public void displayNames() {
System.out.println("Full Name: " + fullName);
System.out.println("First Name: " + firstName);
System.out.println("Middle Name: " + middleName);
System.out.println("Last Name: " + lastName);
System.out.println("Maiden Name: " + maidenName);
System.out.println("Full Name: " + fullName);
}
}
I hope this would helps you...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.