1 Start with the solution to the assignment where you implem
1) Start with the solution to the assignment where you implemented the Contact class. Change the name of your Contact class to Person. Create a class Clerk derived from (a sub class of) Person. The new class should have instance variables that record the additional information of a salary and an employment grade. Eg, a clerk’s employment grade might be (CR-1, CR-2, etc.) Add the accessors and mutators and a toString() method. The toString() method will call the toString() method of the super class.
The Clerk class will have an equals() method. It must override the equals method of the parent class. Change 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 define its own equals method. So, what does it mean that one clerk equals another clerk? We can define this however we please. Let\'s say that 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 your definition. The equals method MUST accept an Object as its parameter.
Here is my Contact class:
public class Contact {
private static int contactsCreated=0;
private String name;
private Address address;
private Telephone telephoneNumber;
private String emailAddress;
public static int getContactsCreated(){
return contactsCreated;
}
@Override
public String toString() {
return \"NAME : \"+name+\" | ADDRESS : \"+address+\" | PHONE : \"+telephoneNumber+\" | EMAIL : \"+emailAddress+\" \";
}
public Contact(String name, Address address, Telephone telephoneNumber, String emailAddress) {
super();
this.name = name;
this.address = address;
this.telephoneNumber = telephoneNumber;
this.emailAddress = emailAddress;
contactsCreated++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Telephone getTelephoneNumber() {
return telephoneNumber;
}
public void setTelephoneNumber(Telephone telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public static void setContactsCreated(int contactsCreated) {
Contact.contactsCreated = contactsCreated;
}
}
Solution
I have written the code for the clerk class. Some of the variables in the code has a user defined type like address which is not known.
The class clerk is derived from the Person class and I have added the accessor and the mutators in the code. The code is compling and working fine on ideone provided you hack the user defined data types.
working code:
/* package whatever; // don\'t place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class Person {
private static int contactsCreated=0;
private String name;
private Address address;
private Telephone telephoneNumber;
private String emailAddress;
public static int getContactsCreated(){
return contactsCreated;
}
@Override
public String toString() {
return \"NAME : \"+name+\" | ADDRESS : \"+address+\" | PHONE : \"+telephoneNumber+\" | EMAIL : \"+emailAddress+\" \";
}
public Contact(String name, Address address, Telephone telephoneNumber, String emailAddress) {
super();
this.name = name;
this.address = address;
this.telephoneNumber = telephoneNumber;
this.emailAddress = emailAddress;
contactsCreated++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Telephone getTelephoneNumber() {
return telephoneNumber;
}
public void setTelephoneNumber(Telephone telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public static void setContactsCreated(int contactsCreated) {
Contact.contactsCreated = contactsCreated;
}
}
class Clerk extends Person
{
int salary; // variable for salary
String grade; //variable for grade
public int getsalary() {
return salary;
}
public void setSalary(int sal) {
this.salary = sal;
}
public int getgrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public boolean equals(Object object) {
if(object instanceof Clerk && ((Clerk)object).getValue() == this.t) {
return true;
} else {
return false;
}
}
}
/* Name of the class has to be \"Main\" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
}
}



