in Java, Redefine the class Person so that, in addition to what the existing cla
ID: 3661893 • Letter: I
Question
in Java,
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 name.
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 variables 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.
Explanation / Answer
import java.io.*;
import java.util.*;
class Person{
String first_name;
String last_name;
String middle_name;
void set_first(String s){
first_name = s;
}
void set_last(String s){
last_name = s;
}
void set_middle(String s){
middle_name = s;
}
boolean check_last(String last){
if (last_name == last) return true;
return false;
}
boolean check_last(String first){
if (first_name == first) return true;
return false;
}
boolean check_last(String middle){
if (middle_name == middle) return true;
return false;
}
boolean same(Person p){
if (p.last_name == this.last_name && p.first_name == this.first_name && p.middle_name == this.middle_name)
return true;
return false;
}
Person getCopy(){
return (Person) this.clone();
}
Person makeCopy(){
Person temp = new Person();
temp.first_name = this.first_name;
temp.last_name = this.last_name;
temp.middle_name = this.middle_name;
}
}
class main{
public static void main(String[] args){
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.