Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions

Question

Absence of disparate impact

Answered: 1 week ago

Question

Performance appraisal criteria based on job analysis

Answered: 1 week ago

Question

Focus on clients needs (efforts to fulfi ll clients requirements)

Answered: 1 week ago