Give the definition of a class named Doctor whose objects are records for a clinic’s doctors. This class will be a derived class of the class SalariedEmployee given in Display 7.5. A Doctor record has the doctor’s specialty (such as "Pediatrician", "Obstetrician", "General Practitioner", and so forth; so use the type String) and office visit fee (use type double ). Be sure your class has a reasonable complement of constructors, accessor, and mutator methods, and suitably defined equals and toString methods. Write a program to test all your methods.
public class SalariedEmployee
{
private double salary; //annual
public SalariedEmployee()
{
salary = 0;
}
public SalariedEmployee(double theSalary)
{
salary = theSalary;
}
public SalariedEmployee(SalariedEmployee originalObject)
{
salary = originalObject.salary;
}
public double getSalary()
{
return salary;
}
public double getPay()
{
return salary/12;
}
public void setSalary( double newSalary)
{
if (newSalary >= 0)
salary = newSalary;
}
public String toString()
{
return ( salary + " per year");
}
public boolean equals(SalariedEmployee other)
{
return (salary == other.salary);
}
}
public class Doctor extends SalariedEmployee {
private String special;
private double fee;
Doctor() {
}
Doctor(String special,double fee,double pay){
super(pay);
this.special=special;
this.fee=fee;
}
void setSpecial(String special)
{
this.special=special;
}
void setFee(double fee){
this.fee=fee;
}
String getSpecial()
{
return special;
}
double getFee()
{
return fee;
}
public String toString()
{
return ("\nDoctor Speciality is:"+special+"\nPay of Doctor:"+getSalary()+"\nVisit Fee:"+fee);
}
public boolean equals(Doctor other){
return (getSalary()==other.getSalary() && special.equals(other.special) && fee==other.fee );
}
}
public class TestMethod {
public static void main(String[] args) {
Doctor doc1=new Doctor("Obstetrician",500,2250.3);
Doctor doc2=new Doctor();
doc2.setFee(500);
doc2.setSalary(2250.3);
doc2.setSpecial("Obstetrician");
System.out.println("\nDoctor 1\n"+doc1.toString());
System.out.println("\nDoctor 2\n\nDoctor Speciality is:"+doc2.getSpecial()+"\nPay of Doctor:"+doc2.getSalary()+"\nVisit Fee:"+doc2.getFee());
System.out.println("\nDoctor 1 Equals to Doctor 2?"+doc1.equals(doc2));
}
}
Output
Doctor 1
Doctor Speciality is:Obstetrician
Pay of Doctor:2250.3
Visit Fee:500.0
Doctor 2
Doctor Speciality is:Obstetrician
Pay of Doctor:2250.3
Visit Fee:500.0
Doctor 1 Equals to Doctor 2?true