Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a class Name that stores a person\'s first, middle, and last names and

ID: 3630348 • Letter: 1

Question

1. Write a class Name that stores a person's first, middle, and last names and provides the following methods:
• public Name(String first, String middle, String last)—constructor. The name should be stored in the case given; don't convert to all upper or lower case.
• public String getFirst()—returns the first name
• public String getMiddle()—returns the middle name
• public String getLast()—returns the last name
• public String firstMiddleLast()—returns a string containing the person's full name in order, e.g., "Mary Jane Smith".
• public String lastFirstMiddle()—returns a string containing the person's full name with the last name first followed by a comma, e.g., "Smith, Mary Jane".
• public boolean equals(Name otherName)—returns true if this name is the same as otherName. Comparisons should not be case sensitive. (Hint: There is a String method equalsIgnoreCase that is just like the String method equals except it does not consider case in doing its comparison.)
• public String initials()—returns the person's initials (a 3-character string). The initials should be all in upper case, regardless of what case the name was entered in. (Hint: Instead of using charAt, use the substring method of String to get a string containing only the first letter—then you can upcase this one-letter string.
• public int length()—returns the total number of characters in the full name, not including spaces.

2. Now write a program TestNames.java that prompts for and reads in two names from the user (you'll need first, middle, and last for each), creates a Name object for each, and uses the methods of the Name class to do the following:
a. For each name, print
• first-middle-last version
• last-first-middle version
• initials
• length
b. Tell whether or not the names are the same . Run the program twice. Type in two names that are the same, except for case, in the first run. Type in two names that are different for the second run.

Explanation / Answer

class Person

{

String first;

String middle;

String last;

public Person(String fs,String ml,String lm)

{

first=fs;

middle=ml;

last=lm;

}

public String getFirst()

{

return first;

}

public String getMiddle()

{

return middle;

}

public String getLast()

{

return last;

}

public String firstMiddleLast()

{

return first+" "+middle+" "+last;

}

public String lastFirstMiddle()

{

return last+", "+middle+" "+last;

}

public boolean equals(Person obj)

{

if (first.equals(obj.getFirst())&&middle.equals(obj.getMiddle())&&last.equals(obj.getLast()))

return true;

else

return false;

}

public String Inititals()

{

String cp,st1,st2,st3;

st1=first.substring(0,1);

st2=middle.substring(0,1);

st3=last.substring(0,1);

st1.toUpperCase();

st2.toUpperCase();

st3.toUpperCase();

return st1+st2+st3;

}

public int Length()

{

return first.length()+middle.length()+last.length()+3;

}

}

import java.io.*;

import java.util.Scanner;

public class TestNames

{

public static void main(String[] args)

{

// TODO Auto-generated method stub

Person name1=new Person("Mary","Jane","Smith");

Scanner input=new Scanner(System.in);

System.out.println("Enter first name:");

String f,m,l;

f=input.nextLine();

System.out.println("Enter middle name:");

m=input.nextLine();

System.out.println("Enter last name:");

l=input.nextLine();

Person name2=new Person(f,m,l);

System.out.println("firstMiddleLast Method "+name1.firstMiddleLast());

System.out.println("firstMiddleLast Method "+name2.firstMiddleLast());

System.out.println("lastFirstMiddle Method "+name1.lastFirstMiddle());

System.out.println("lastFirstMiddle Method "+name2.lastFirstMiddle());

if(name1.equals(name2))

System.out.println("Two name equal");

System.out.println(name2.Inititals());

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote