Share
Explore BrainMass

Programming in Java II - Two common attributes of a person are the person's first name and last name.

Please see attached file for full problem description.

Example 1-1: Two common attributes of a person are the person's first name and last name. The typical operations on a person's name are to set the name and print the name. The following statements define a class with these properties.

public class Person
{

private String firstName; //store the first name
private String lastName; //store the last name

//Default constructor;
//Initialize firstName and lastName to empty string.
//Postcondition: firstName = ""; lastName = " ";
public Person ()
{
firstName = "";
lastName = "";

}

//Constructor with parameter
//Set firstName and lastName according to the parameters.
//Postcondition: firstName = first; lastName = last;
public Person (String first, String last)
{
setName (first, last);
}

//Method to output the first name and last name
//in the form firstName lastName
public String toString ()
{
return (firstName + " " + lastName);
}

//Method to set firstName and lastName according to
//the parameters
//Postcondition: firstName = first; lastName = last;
public void setName (String first, String last)
{
firstName = first;
lastName = last;
}
//Method to return the firstName
//Postcondition: the value of firstName is returned
public String getFirstName ()
{
return firstName;
}

//Method to return the lastName
//Postcondition: the value of lastName is returned
public String getLastName ()
{
return lastName;
}
}

The following program tests the class Person:

public class TestProgPerson
{
public static void main (String[ ] args)
{
Person name = new Person (); //Line 1

Person emp = new Person ("Donald", "Jackson"); //Line 2

System.out.println ("Line 3: name: " +name); //Line 3

name.setName ("Ashley", "Blair"); //Line 4
System.out.println ("Line 5: name: " +name); //Line 5

System.out.println ("Line 6: emp: " + emp); //Line 6

emp.setName ("Sandy", "Smith"); //Line 7
Sytem.out.println ("Line 8: emp: " + emp); //Line 8
}//end main
}

Question #1: Develop and execute a program for this exercise.

a) Example 1-1 above defined the class Person to store the name of a person. The methods that we included merely set the name and print the name of a person. Redefine the class Person so that, in addition to what the existing class does, you can:

i. Set the last name only.
ii. Set the first name only.
iii. Set the middle namde.
iv. Check whether a given last name is the same as the last name of this person.
v. Check whether a given first name is the same as the first name of this person.
vi. Check whether a given middle name is the same as the middle name of this person.

b) Add the method equals that returns true if two objects contain the same first, middle, and last name.

c) Add the method makeCopy that copies the instance variable of a Person object into another Person object.

d) Add the method getCopy that creates and returns the address of the object, which is a copy of another Person object.

e) Add the copy constructor.

f) Write the definitions of the methods of the class Person to implement the operations for this class.

g) Write a program that tests various operations of the class Person.

Attachments

$2.19