Question

In: Computer Science

The class Person, uploaded on Blackboard with Lab 5, only has methods to set and print...

The class Person, uploaded on Blackboard with Lab 5, only has methods to set and print the name of a person. Redefine the class Person to include the following operations: Set the last name only Set the first name only Set the middle name Check whether a given last name is the same as the last name of this person Check whether a give first name is the same as the first name of this person Check whether a given middle name is the same as the middle name of this person Add the method equals that returns true if two objects contain the same first and last names. Add the method makeCopy that copies the instance of variables of one Person object into another Person object. Add the method getCopy that creates and returns the address of the object, it is a copy of another Person Object. Write the definitions of the methods for the class Person to implement the operations. Write a program to test various operations of the class Person. Hint: You can use the class type Person inside your Person class definition.

public class Person
{
    private String firstName; //store the first name
    private String lastName;  //store the last name

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

    }

        //Constructor with parameters
        //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 firstName.
        //Postcondition: The value of firstName is returned.
    public String getFirstName()
    {
        return firstName;
    }

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

Solutions

Expert Solution

//The person class

public class Person {


    private String firstName;
    private String middleName;
    private String lastName;

    //Default constructor
    public Person()
    {
        firstName = "";
        lastName = "";
        middleName ="";
    }

    //Constructor with parameters
    public Person(String first,String middle, String last)
    {
        setName(first,middle,last);
    }

    //     Method to set the last name:
    //Method to set the last name of a person

    public void setLastName(String last)
    {
        this.lastName=last;
    }

    //     Method to set the first name:
    //Method to set the first name of a person
    public void setFirstName(String first)
    {
        this.firstName=first;
    }

    //     Method to set the middle name:
    //Method to set the middle name of a person
    public void setMiddleName(String middle)
    {
        this.middleName=middle;
    }

    //Method definition of copy constructor that takes //person object variable and copy the instance values //to the class person object
    //Copy Constructor
    public Person(Person obj)
    {
        firstName=obj.firstName;
        middleName=obj.middleName;
        lastName=obj.lastName;
    }

    // method that sets the name
    public void setName(String first,String middle,String last){
        firstName=first;
        middleName=middle;
        lastName=last;
    }

    // method sets the last name
    // method checks for previous name if it is equal
    public void setLast(String last){
        if(last.equals(getLastName()))
            System.out.println("Equal Previous name");
        else
            System.out.println("Not Equal");
        lastName=last;
    }


    // method sets the first name
    // method checks for previous name if it is equal

    public void setFirst(String first){
        if(first.equals(getFirstName()))
            System.out.println("Equal Previous name");
        else
            System.out.println("Not Equal");
            firstName=first;
    }

    // method sets the middle name
    // method checks for previous name if it is equal
    public void setMiddle(String middle){
        if(middle.equals(getMiddleName())){
            System.out.println("Equal Previous name");}
        else{
            System.out.println("Not Equal");
            middleName=middle; }
    }
    // returns the first name
    public String getFirstName()
    {
        return firstName;
    }
    // returns the middle name
    public String getMiddleName()
    {
        return middleName;
    }

    public String getLastName(){
        return lastName;
    }

    //The method equals that takes person object as
    // its argument that returns true if two objects
    // contain the same first ,last and middle name
    // checks for the objects having same names
    public boolean equals(Person obj) {
        return(firstName==obj.firstName && middleName==obj.middleName&& lastName==obj.lastName);
    }

    //Method makeCopy that takes person object as
    //variables into the another person class //copies one object name to another
    public void makeCopy(Person obj) {
        firstName=obj.firstName;
        middleName=obj.middleName;
        lastName=obj.lastName; }

    public Person getCopy()
    {
        Person obj=new Person();
        obj.firstName=firstName;
        return obj;
    }


    // prints the object details
    public String toString()
    {
        return (firstName +" "+middleName+ " " + lastName); }

}


//The Test Class

public class Test
{
    public static void main(String[] args)
    {

        Person person1 = new Person();
        Person person2 = new Person("Tom","Dick","Harry");
        Person person3 =new Person("Ashley","Robert", "Junior");
        Person person = new Person(person2);
        System.out.println("String Person :"+person);
        System.out.println("String Person2 :"+person2);
        System.out.println("String Person3 :"+person3);

        System.out.println("The objects are equal: "+person1.equals(person2));
        person1=person2.getCopy();
        System.out.println("After get copy person1:"+person1);
        person2.makeCopy(person3);
        System.out.println("After make Copy P2 :"+person2);
    }
}

//output image


Related Solutions

In Lab 4, you made a class with static methods. The static methods converted an integer...
In Lab 4, you made a class with static methods. The static methods converted an integer in binary, Hex, and Octal. To do this, you made a class of static methods. The class did not have any fields. The static methods received the integer value as a parameter. For this lab, make all of the static methods, non-static methods. Lab 5 Requirements Create a class named “Lab5_IntConv_Class Have one private field name intValue. Add two constructors: One is a default...
How to print these methods in a separate main class? need the output to look like...
How to print these methods in a separate main class? need the output to look like this: Enter three integers whose GCD is to be found -> Enter an integer n to find the nth Fibonacci number -> Enter the base and exponent , an integer, of a power -> Enter two positive integers 1 and j where i < j -> gcd() = fib() = (a number ^ a number) = a number There are ___ palindromic numbers between...
In math class, a student has written down a sequence of 16 numbers on the blackboard....
In math class, a student has written down a sequence of 16 numbers on the blackboard. Below each number, a second student writes down how many times that number occurs in the se‐ quence. This results in a second sequence of 16 numbers. Below each number of the second se‐ quence, a third student writes down how many times that number occurs in the second se‐ quence. This results in a third sequence of numbers. In the same way, a...
Modify StudentLinkedList class by adding the following methods: printStudentList: print by calling and printing “toString” of...
Modify StudentLinkedList class by adding the following methods: printStudentList: print by calling and printing “toString” of every object in the linkedList. Every student object to be printed in a separate line.  deleteStudentByID(long id): delete student object from the list whose ID is matching with the passed parameter.  sortListByID(): sort the linkedlist according to students IDs.  findMarksAverage(): find the average of all marks for all students in the list.  findMinMark(int markIndex): find the student with the minimum...
Problem 3: Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and...
Problem 3: Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and printing “toString” of every object in the linkedList. Every student object to be printed in a separate line.  deleteStudentByID(long id): delete student object from the list whose ID is matching with the passed parameter.  sortListByID(): sort the linkedlist according to students IDs.  findMarksAverage(): find the average of all marks for all students in the list.  findMinMark(int markIndex): find the student...
In this lab you will learn how to use methods from the Math class in order...
In this lab you will learn how to use methods from the Math class in order to calculate the area or the volume of several different shapes. If you are confused about the Methods you can access from the Math class and would like to see some examples click here. Hint: Most of these methods can be done in one line. Step 1 - circleArea In this method you will calculate the area of a circle. Before you can calculate...
In C++, implement a class called setOper that provides several simple set operations. The class only...
In C++, implement a class called setOper that provides several simple set operations. The class only needs to deal with sets that are closed intervals specified by two real numbers; for example, the pair (2.5, 4.5) represent the interval [2.5, 4.5]. The following operations should be supported: - Check if the value x belongs to the given interval. - Check if the value x belongs to the intersection of two intervals. - Check if the value x belongs to the...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
Previous Lab Code files: PersonType.h: #include <string> using namespace std; class personType { public: void print()...
Previous Lab Code files: PersonType.h: #include <string> using namespace std; class personType { public: void print() const; void setName(string first, string middle, string last);    void setLastName(string last);    void setFirstName(string first);    void setMiddleName(string middle);    bool isLastName(string last) const;    bool isFirstName(string first) const; string getFirstName() const;    string getMiddleName() const;    string getLastName() const; personType(string first = "", string middle = "", string last = ""); private: string firstName; string middleName; string lastName; }; PersonTypeImp: #include <iostream>...
Using SQL Developer ONLY! Lab 5 1. Create a lab report file (MS Word compatible) and...
Using SQL Developer ONLY! Lab 5 1. Create a lab report file (MS Word compatible) and name it as “IT4153_Lab 5_ Your D2L ID”. a. Create two tables: Employee: empID (PK), empFname, empLname, deptID(FK) and Department: deptID(PK), deptName, chairID chairID is empID from Employee table b. Insert at least 3 rows in the Department table and at least 6 rows in the Employee table. c. Create trigger on update of chairID that enforces the following business rules • One employee...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT