Question
1. Write a class Name that stores a persons first,middle, and last names and provides the following methods: public Name(String first, String middle, Stringlast)constructor. The
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, Stringlast)—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 theperson’s full name in order, e.g., “Mary Jane Smith”.
public String lastFirstMiddle()—returns a string containing theperson’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 nameis the same as otherName. Comparisons should not be case sensitive.(Hint: There is a String method equalsIgnoreCase that isjust like the String method equals except it does notconsider case in doing its comparison.)
public String initials()—returns the person’s initials (a3-character string). The initials should be all in upper case,regardless of what case the name was entered in. (Hint: Instead ofusing charAt, use the substring method of Stringto get a string containing only the first letter—then you canupcase this one-letter string. See Figure 3.1 in the text for adescription of the substring method.)
public int length()—returns the total number of characters inthe full name, not including spaces.
2. Now write a program TestNames.java that prompts for and readsin two names from the user (you’ll need first, middle, and last foreach), creates a Name object for each, and uses the methods of theName 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 arethe same.
Here is my code. I keep gettinga method error with getFullName in the Name.java file. Pleasehelp me re-write the code to fix this issue.
//Name.java
public class Name
{
private String firstName, middleName, lastName,fullName;
public Name(String first, String middle, Stringlast)
{
firstName = first;
middleName = middle;
lastName = last;
String fullName = firstName '+'middleName '+' lastName;
}
public String getFirst()
{
return firstName;
}
public String getMiddle()
{
return middleName;
}
public String getLast()
{
return lastName;
}
public String firstMiddleLast()
{
return firstName + ' ' + middleName + '' + lastName;
}
public String lastFirstMiddle()
{
return lastName + ", " + firstName + '' + middleName;
}
public boolean equals(Name otherName)
{
returnfullName.equalsIgnoreCase(otherName.getFullName());
}
public String initials()
{
returnfirstName.toUpperCase().substring(0,1)
+middleName.toUpperCase().substring(0,1)
+lastName.toUpperCase().substring(0,1);
}
public int length()
{
return firstName.length() +middleName.length() + lastName.length();
}
}
//NameTester.java
import java.util.Scanner;
public class NameTester
{
public static void main(String[] args)
{
Scanner input = newScanner(System.in);
String firstName1 = new String();
String middleName1 = newString();
String lastName1 = new String();
String firstName2 = new String();
String middleName2 = newString();
String lastName2 = new String();
System.out.print("Please enter afirst name: ");
firstName1 = input.nextLine();
System.out.print("Please enter a middlename: ");
middleName1 = input.nextLine();
System.out.print("Please enter a lastname: ");
lastName1 = input.nextLine();
Name name1 = new Name(firstName1,middleName1, lastName1);
System.out.print("Please enteranother first name: ");
firstName2 = input.nextLine();
System.out.print("Please enter anothermiddle name: ");
middleName2 = input.nextLine();
System.out.print("Please enter anotherlast name: ");
lastName2 = input.nextLine();
Name name2 = new Name(firstName2,middleName2, lastName2);
System.out.println();
System.out.println(name1.firstMiddleLast());
System.out.println(name2.firstMiddleLast());
System.out.println(name1.lastFirstMiddle());
System.out.println(name2.lastFirstMiddle());
System.out.println(name1.initials());
System.out.println(name2.initials());
System.out.println(name1.length());
System.out.println(name2.length());
if (name1.equals(name2))
System.out.println("Thenames are the same.");
else
System.out.println("Thenames are not the same.");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started