-You will create a Name class to manage different name changes of a person. Crea
ID: 3634572 • Letter: #
Question
-You will create a Name class to manage different name
changes of a person.
Create a program to;
1. Have the user enter the number of names (=x).
2. Have the user enter in x full names (e.g. Hillary Diane
Rodham, Barrack Obama).
3. While (The user hasn’t typed in “exit” or “Exit”)
Have the user type in a person’s first name
Ask which name they would like to change (first, middle or last)
Ask the user what they would like to change that name to
• If they want to change last name, ask the user if they would like to
change maiden name also.
• If yes, current last name becomes maiden name, and new last name
becomes last name.
4. Print everybody’s name in the specified format:
Full Name: Hillary D. Clinton
First Name: Hillary
Middle Name: Diane
Last Name: Clinton
Maiden Name: Rodham
Here are the methods that should be used:
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
please rate - thanks
message me if any problems, and I'll fix it tomorrow
import java.util.*;
public class NameDemo
{public static void main(String[] args)
{Scanner in=new Scanner(System.in);
int n,i,j,k;
String input,f,m,l;
boolean done;
System.out.print("How many names do you have? ");
n=in.nextInt();
Name name[]=new Name[n];
in.nextLine();
for(i=0;i<n;i++)
{System.out.print("Enter name "+(i+1)+":");
input=in.nextLine();
j=input.indexOf(' ');
f=input.substring(0,j);
k=input.indexOf(' ',j+1);
if(k==-1)
{m="";
k=j;
}
else
m=input.substring(j+1,k);
l=input.substring(k+1,input.length());
name[i]=new Name(f,m,l);
}
System.out.print("Enter first name, exit to exit: ");
f=in.nextLine();
done=false;
while(f.compareToIgnoreCase("exit")!=0)
{i=0;
do
{if(name[i].getFirstName().compareToIgnoreCase(f)==0)
{System.out.println("Which name to change(first, middle, last)? ");
m=in.nextLine();
System.out.println("Enter new name: ");
l=in.nextLine();
if(m.compareToIgnoreCase("first")==0)
{name[i].changeFirstName(l);
name[i].displayNames();
done=true;
}
else if(m.compareToIgnoreCase("middle")==0)
{name[i].changeMiddleName(l);
name[i].displayNames();
done=true;
}
else
{ name[i].changeLastName(l);
name[i].displayNames();
done=true;
}
}
i++;
}while(i<n&&!done);
System.out.print("Enter first name, exit to exit: ");
f=in.nextLine();
done=false;
}
}
}
--------------------------------------
import java.util.*;
class Name
{private String first;
private String middle;
private String last;
private String maiden;
public Name(String f, String m, String l)
{first=f;
middle=m;
last=l;
maiden="";
}
public String getFullName()
{if(maiden.compareTo("")!=0)
return first+" "+middle+" "+maiden+" "+last;
else
return first+" "+middle+" "+last;
}
public String getFirstName()
{return first;
}
public String getMiddleName()
{return middle;
}
public String getLastName()
{return last;
}
public String getMaidenName()
{return maiden;
}
public void changeFirstName(String newFirst)
{first=newFirst;
}
public void changeMiddleName(String newMiddle)
{middle=newMiddle;
}
public void changeLastName(String newLast)
{String yes;
System.out.print("change maiden name(yes/no): ");
Scanner in=new Scanner(System.in);
yes=in.nextLine();
if(yes.compareToIgnoreCase("yes")==0)
{changeMaidenName();
last=newLast;
}
else if (yes.compareToIgnoreCase("no")==0)
{last=newLast;
}
else
System.out.println("invalid entry-operation aborted");
}
private void changeMaidenName()
{
maiden=last;
}
public void displayNames()
{System.out.println("Full Name: "+first+" "+middle.charAt(0)+". "+" "+last);
System.out.println("First Name: "+first);
System.out.println("Middle Name: "+middle);
System.out.println("Last Name: "+last);
if(maiden.compareTo("")!=0)
System.out.println("Maiden Name: "+maiden);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.