Hey! I just need help with this program, I have been working 7 days a week with
ID: 3842411 • Letter: H
Question
Hey! I just need help with this program, I have been working 7 days a week with overtime and have not had the time to even look at this until now and it is due tonight! I appreciate the help! Oh, and it is a Java program by the way!
Create a class Person and Clerk derived from (a sub class of) Person.The Person class should have instance variables and Clerk class should have the additional information of a salary and an employment grade.Eg, a clerk’s employment grade might be (CR-1, CR-2, etc.)Both class should have the accessors, mutators and a toString() method.The toString() method in Clerk class will include a call the toString() method of the super class(Person).
A Person Has A Telephone, Address, Name, Email
The Clerk class will have an equals() method. It must override the equals method of the parent class(Person). The equals method in the Person class to override the equals method in the Object class. We considered the difference between using == and equals() method with String objects.
Any class can, and should, define its own equals method. So, what does it mean that one clerk equals another clerk? We can define this such as two Clerk objects are equal, if they have the same salary and grade. So, clerk1.equals(clerk2) will return true if their salary and grade are "equal" by the definition. You might notice that when comparing String objects, you must use the equals method defined in the String class, as opposed to the relational == operator.
The equals method MUST accept an Object as its parameter.
Create a Week02_yourName_Assignment.java and use it to demonstrate following minimum:
Your clerk Class is functioning, and the methods are correct.Create several clerks, compare them to each other using the equals method, and output clearly when one object is equal to another.Print out the objects as well
You can output the address of a clerk object
You can change the zip code of a clerk object and print zip code before and after change.
Deliverables:
Submit Week02_yourName_Assignment.zip contains following items;
Person.java
Clerk.java
Week02_yourName_Assignment.java
^^My name is Ben by the way!
Explanation / Answer
Person.java
public class Person {
//Declaring instance variables
private String telephone;
private String address;
private String name;
private String email;
//Parameterized constructor
public Person(String telephone, String address, String name, String email) {
super();
this.telephone = telephone;
this.address = address;
this.name = name;
this.email = email;
}
//getters and setters
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Name=" + name +" Address=" + address
+" Telephone=" + telephone +" Email=" + email;
}
//equals() method compare two clerk object contents
@Override
public boolean equals(Object obj) {
Person other = (Person) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (telephone == null) {
if (other.telephone != null)
return false;
} else if (!telephone.equals(other.telephone))
return false;
return true;
}
}
__________________________
Clerk.java
public class Clerk extends Person {
//Declaring instance variables
private double salary;
private String employment_grade;
//Parameterized constructor
public Clerk(String telephone, String address, String name, String email,
double salary, String employment_grade) {
super(telephone, address, name, email);
this.salary = salary;
this.employment_grade = employment_grade;
}
//getters and setters
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getEmployment_grade() {
return employment_grade;
}
public void setEmployment_grade(String employment_grade) {
this.employment_grade = employment_grade;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString()+" Salary=" + salary + " Employment grade=" + employment_grade;
}
//equals() method compare two Person object contents
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Clerk other = (Clerk) obj;
if (employment_grade == null) {
if (other.employment_grade != null)
return false;
} else if (!employment_grade.equals(other.employment_grade))
return false;
if (Double.doubleToLongBits(salary) != Double
.doubleToLongBits(other.salary))
return false;
return true;
}
}
_________________________
Week02_yourName_Assignment.java
public class Week02_yourName_Assignment {
public static void main(String[] args) {
Clerk c1,c2,c3,c4;
c1=new Clerk("9848425878","101, lake Street 560068","Kane Williams","williams@xyzmail.com",45000.00,"CR-1");
c2=new Clerk("9848425878","101, lake Street 560068","Kane Williams","williams@xyzmail.com",45000.00,"CR-1");
c3=new Clerk("9834567898","201, Church Street 560032","Johnson","john@xyzmail.com",65000.00,"CR-4");
c4=new Clerk("9832434456","301, Lake View Street 560012","Rob","rob@xyzmail.com",95000.00,"CR-2");
System.out.println(c1.toString());
System.out.println(c2.toString());
boolean bool1=c1.equals(c2);
if(bool1)
System.out.println("Clerk1 is equal to Clerk2");
else
System.out.println("Clerk1 is not equal to Clerk2");
System.out.println(c2.toString());
System.out.println(c3.toString());
boolean bool2=c2.equals(c3);
if(bool2)
System.out.println("Clerk2 is equal to Clerk3");
else
System.out.println("Clerk2 is not equal to Clerk3");
System.out.println("______ Displaying the Address and Pin code of Clerk4 object before change______");
System.out.println("The Address of clerk4 is :"+c4.getAddress());
//changing the pincode of clerk4 object
c4.setAddress("301, Lake View Street 560049");
System.out.println("______ Displaying the Address and Pin code of Clerk4 after before change______");
System.out.println("The Address of clerk4 is :"+c4.getAddress());
}
}
_______________________
Output:
Name=Kane Williams Address=101, lake Street 560068 Telephone=9848425878 Email=williams@xyzmail.com Salary=45000.0 Employment grade=CR-1
Name=Kane Williams Address=101, lake Street 560068 Telephone=9848425878 Email=williams@xyzmail.com Salary=45000.0 Employment grade=CR-1
Clerk1 is equal to Clerk2
Name=Kane Williams Address=101, lake Street 560068 Telephone=9848425878 Email=williams@xyzmail.com Salary=45000.0 Employment grade=CR-1
Name=Johnson Address=201, Church Street 560032 Telephone=9834567898 Email=john@xyzmail.com Salary=65000.0 Employment grade=CR-4
Clerk2 is not equal to Clerk3
______ Displaying the Address and Pin code of Clerk4 object before change______
The Address of clerk4 is :301, Lake View Street 560012
______ Displaying the Address and Pin code of Clerk4 after before change______
The Address of clerk4 is :301, Lake View Street 560049
______________Could You rate me well .Plz Thank You