Question

In Java, lead the class Person to store the name a person we discuss in the...

  1. In Java, lead the class Person to store the name a person we discuss in the class (source is Person.java attached in the assignment). As we can see that the methods we included merely set the name and print the name of a person. Redefine the class person so that, in addition to what existing Class does, you can
  • 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.

  1. Add the method equals that returns true if two objects contains the same first and last name.

  1. Add the method makeCopy that copies the instance of variables of a Person object into another Person object.

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

  1. Write the definition of the methods of the class Person to implement the operation of this Class

  1. Write a program that tests various operation of the Class Person.

Homework Answers

Answer #1

import java.io.*;
import java.util.*;
public class Main
{
   public static void main(String[] args) {
       Person myself = new Person("Ram", "Prasad", "Bismil");
      
       myself.setFirstName("Shyam");
       myself.setMiddleName("Prasad"); /// testing by passing same middle name;
       myself.setLastName("Sunder");

       Person you = myself.getCopy(); //// test for makeCopy() as well as getCopy()

       System.out.println(myself.equals(you));
   }
}

class Person
{
   String firstName;
   String middleName;
   String lastName;

   ///// default constructor ////
   public Person()
   {
      
   }


   //// parameterized constructor //////
   public Person(String firstName, String middleName, String lastName)
   {
       this.firstName = firstName;
       this.lastName = lastName;
       this.middleName = middleName;
   }

   ///// getter for full name /////
   public String getName()
   {
       return firstName + " " + middleName + " " + lastName;
   }

   //// setter for first name /////
   public void setFirstName(String firstName)
   {
       ////// if given first name is same as already stored first name, this block will execute
       if(firstName.equals(this.firstName))
       {
           System.out.println("given first name is same as the already stored firstName");
           return;
       }
       this.firstName = firstName;
   }  


   //// setter for middle name /////
   public void setMiddleName(String middleName)
   {
       ////// if given middle name is same as already stored middle name, this block will execute
       if(middleName.equals(this.middleName))
       {
           System.out.println("given middleName name is same as the already stored middleName");
           return;
       }
       this.middleName = middleName;
   }  

   //// setter for last name /////
   public void setLastName(String lastName)
   {
       ////// if given last name is same as already stored last name, this block will execute
       if(lastName.equals(this.lastName))
       {
           System.out.println("given last name is same as the already stored last name");
           return;
       }
       this.lastName = lastName;
   }


   /////// equals method to comapre two Person objects /////////
   public boolean equals(Person obj)
   {
       if(this.firstName.equals(obj.firstName) && this.lastName.equals(obj.lastName))
           return true;
       else
           return false;
   }  

   ////// makeCopy function to create a copy of current object
   public void makeCopy(Person obj)
   {
       obj.firstName = this.firstName;
       obj.middleName = this.middleName;
       obj.lastName = this.lastName;
   }


   ////// method to create a copy of current object and return to the caller
   public Person getCopy()
   {
       Person obj = new Person();
       this.makeCopy(obj);
       return obj;
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
in JAVA Write a class Store which includes the attributes: store name and sales tax rate....
in JAVA Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Yarn Store, which inherits from Store. A Yarn Store has the following additional attributes: how many skeins of yarn are sold every year and the average price per skein. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Yarn Store; In the Yarn Store class, also code a method returning the...
Consider the following incomplete declaration of a Name class for 2a-2c. public class Name something missing...
Consider the following incomplete declaration of a Name class for 2a-2c. public class Name something missing {    private String first;    private String last;    public Name(String firstName, String lastName)    { first = firstName; last = lastName;    }    //additional methods } 2a) In the first two parts, you will modify the Name class so that it implements the Comparable interface. Show here what should go in place of something missing in the first line of the...
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...
Write a program that utilizes a Professor class. A professor has a first name, last name,...
Write a program that utilizes a Professor class. A professor has a first name, last name, affiliation, easiness grade, and a helpfulness grade. Grades range from 1 (lowest) to 5 (highest). The Professor class has two constructors. The first constructor initiates the object with just the first name, the last name, and the affiliation. The default value for easiness and helpfulness grade is 3. The second constructor initiates the object using the first name, the last name, the affiliation, and...
Create java class with name Dog. Instructions for Dog class:              This class is modeled after...
Create java class with name Dog. Instructions for Dog class:              This class is modeled after a Dog. You should have instance variables as follows: The Dog’s name The number of tricks known by the Dog. The color*of the Dog’s coat (fur). *: For the Dog’s fur color you should use the Color class from the java.awt package. You should choose proper types and meaningful identifiers for each of these instance variables. Your class should contain two constructor methods. A...
JAVA Write a Student class with a String name and double gpa class members. Write a...
JAVA Write a Student class with a String name and double gpa class members. Write a constructor, get and set methods, and a toString method.
Write an object oriented programming (JAVA) code. Create a class called Circle. The class has an...
Write an object oriented programming (JAVA) code. Create a class called Circle. The class has an attribute radius, which default to 1 by the first no-arg constructor. The class also has another constructor that instantiates the radius to a given value. Provide methods that calculate the circumference and the area of the circle (use Math.PI and Math.pow). Provide set and get methods. The set method should verify that radius is greater than or equal to 0.0 and less than 20.0,...
Write a class Store which includes the attributes: store name and sales tax rate. Write another...
Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Book Store, which inherits from Store. A Book Store has the following additional attributes: how many books are sold every year and the average price per book. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Book Store; In the Book Store class, also code a method returning the average taxes per year....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT