Write a second Person class that will have a instance variable of type Name inst
ID: 3625838 • Letter: W
Question
Write a second Person class that will have a instance variable of type Name instead of a String. You will use the Name class that was written in project 1. The methods of the Name class become available to solve the methods of the Person class.
import java.util.Scanner;
class Name{
private String firstName;
private String middleName;
private String lastName;
Scanner keyboard = new Scanner(System.in);
public Name()
{
firstName = "Mark";
middleName = "Adam";
lastName = "Black";
}
public Name(Name otherName)
{
firstName = otherName.firstName;
middleName = otherName.middleName;
lastName = otherName.lastName;
}
public Name(String first, String middle, String last)
{
setName(first, middle, last);
}
public String toString()
{
return (firstName + " " + middleName + " " + lastName);
}
public void setFirstName(String first)
{
firstName = first;
}
public void setMiddleName(String middle)
{
middleName = middle;
}
public void setLastName(String last)
{
lastName = last;
}
public void setName(String first, String middle, String last)
{
firstName = first;
middleName = middle;
lastName = last;
}
public void readName()
{
System.out.println("Please enter first name");
firstName = keyboard.nextLine();
System.out.println("Please enter middle name");
middleName = keyboard.nextLine();
System.out.println("Please enter last name");
lastName = keyboard.nextLine();
}
public String getFirstName()
{
return firstName;
}
public boolean equals(Name n)
{
return (this.firstName.equalsIgnoreCase(n.firstName)&&
this.middleName.equalsIgnoreCase(n.middleName)&&
this.lastName.equalsIgnoreCase(n.lastName));
}
public String getMiddleName()
{
return middleName;
}
public String getLastName()
{
return lastName;
}
}
Here is the Demo class that goes with it:
public class NameDemo
{
public static void main(String[] args)
{ Name brother = new Name("Mark","Dark","Plus" );
Name schoolFriend = new Name( );
Name neighbor = new Name( );
System.out.println("your brother's name is. " + brother);
System.out.println("Enter your brother's name."); brother.readName( );
System.out.println("Enter your school friend's name.");
schoolFriend.readName( );
System.out.println("Enter your neighbor's name.");
neighbor.readName( );
System.out.println(" Your neighbor's middle name is " + neighbor.getMiddleName());
System.out.println(" Your brother's name is " + brother);
System.out.println(" Your neighbor's name is " + neighbor);
if(neighbor.equals(brother))
{
System.out.println(" Your neighbor's name is the same as your brother's name! " );
}
else
{
System.out.println(" Your neighbor's name is not the same as your brother's name! " );
}
}
}
Please help me as soon as possible, I need this by today :(
Explanation / Answer
Not exactly sure what you're looking for, as the question was not all that clear. It seems that you are supposed to take your Person class (hopefully you have that) and then replace the name instance field in that class, with a Name object instead of a String object. public class Person{ Name name; int age; //other data about the person public Person (){ name = new Name(); age = 19; } public Person(String first, String middle, String last){ name = new Name(first, middle, last); age = 19; } Hopefully you get the idea. Once you have instantiated the Name object, you can use all the methods that you defined in the Name class (readName(), getName(), equals(), etc) Hope this helps.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.