a. Name.java i. Define a complete Java class named Name that represents a person
ID: 3716997 • Letter: A
Question
a. Name.java
i. Define a complete Java class named Name that represents a person’s name. This class should contain 3 instance variables: one for the person’s first name, one for the last name, and one for the middle initial. The first and last name should be Stringvalues, and the middle initial a char.
ii. Define two constructor methods for this class. The first constructor should take 3 arguments – a person’s first name, middle initial, and last anem. The other constructor should accept just two arguments – a person’s first name and last name (because some people don’t have middle names).
iii. A method public String getNormalOrder()that returns a person’s name in the order with the first name followed by a space, then the middle initial, a period(.) and space, and then the last name. For example, “Stuart Q. Roberts” might be returned. If there is no middle name initial, then the first name would simply by followed by a space and the last name.
iv. A method public String getReversedOrder()that returns a person’s name in reverse order with the last name followed by a comma and space before the first name and initial. (e.g. “Roberts, Stuart Q.” would be returned.
v. A method public String getInitials()that returns a person’s initial in the usual order, all in uppercase. For example, “SQR” might be returned.
vi. This file is Name.java
b. NameTest.java
i. Define a main method that constructs two different Name objects, as below.
Name director = new Name(“Megan”, ‘H’, “Hansen”);
Name supervisor = new Name(“Peter”, “Rabbit”);
Apply the three methods you defined in the Name class (getNormalOrder(), getReversedOrder(), and getInitials()) to the two name objects that were constructed, and print the results. For example,
System.out.println(director.getIntials()); should output the String value MHH.
Explanation / Answer
class Name
{
String fname;
String lname;
char mname=null;
Name(String f,String l, char m)
{
this.fname=f;
this.lname=l;
this.mname=m;
}
Name(String f1,String l1)
{
this.fname=f1;
this.lname=l1;
}
public String getNormalOrder()
{
if(mname!=null)
System.out.println(fname+" " +mname+". " + lname);
else
System.out.println(fname+" "+ lname);
}
public String getReversedOrder()
{
if(mname!=null)
System.out.println(lname+","+fname+"."+mname);
else
System.out.println(lname+","+fname);
}
public String getInitials()
{
char firstinitials;
char lastinitials;
char minitials;
firstinitials=fname.charAt(0);
firstinitials=Character.toUpperCase(firstinitials);
minitials=Character.toUpperCase(mname);
lastinitials=lname.charAt(0);
lastinitials=Character.toUpperCase(lastinitials);
System.out.println(firstinitials+minitials+lastinitials);
}
}
class main
{
public static void main(String a[])
{
Name director = new Name(“Megan”, ‘H’, “Hansen”);
Name supervisor = new Name(“Peter”, “Rabbit”);
director.getNormalOrder();
director.getReversedOrder();
director.getInitials();
supervisor.getNormalOrder();
supervisor.getReversedOrder();
supervisor.getInitials();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.