Objective Define a class SimplePerson which has a nameand age. Next, define a cl
ID: 646397 • Letter: O
Question
Objective
Define a class SimplePerson which has a nameand age.
Next, define a class Doctor which extends SimplePerson and has a specialty (such as Pediatrician, Obstetrician, etc.), and an office visit fee.
Then, define a class called Lawyer which also derives from SimplePerson and has the instance variables type (patent, criminal defense, etc.) and court date.
Finally, define a class Wrestler which has the instance variables finishing move (Stinger Splash, Top Rope Elbow, Super Kick) and is a face (which is a true or false value).
Make sure to give your classes a set of getters, setters (with error checking), constructors, toString, and an equals method.
Write a separate driver class Driver that tests ALL the methods. This doesn't require any input from the user, as it simply goes through and tests all the methods such as the getters and setters.
Example Dialog:
Explanation / Answer
//SimplePerson.java
public class SimplePerson
{
private String name;
private int age;
//default constructor
public SimplePerson()
{
name="No Name Yet";
age=0;
}
//parameterized default constructor
public SimplePerson(String name, int age)
{
this.name=name;
this.age=age;
}
//setter metods
public void setName(String name)
{
this.name=name;
}
public void setAge(int age)
{
this.age=age;
}
//getter metods
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
//Override toString method that returns name and age as string
@Override
public String toString()
{
return "Name : "+name+" "+
"Age : "+age+" ";
}
}
------------------------------------------------
//Doctor.java that extends the super class SimplePerson
public class Doctor extends SimplePerson
{
private String speciality;
private double fee;
//default constructor
public Doctor()
{
super();
speciality="None";
fee=0;
}
//Parameterized default constructor
public Doctor(String name, int age, String speciality, double fee)
{
super(name, age);
this.speciality=speciality;
this.fee=fee;
}
//setter methods
public void setName(String name)
{
super.setName(name);
}
public void setAge(int age)
{
super.setAge(age);
}
//setter methods
public void setSpeciality(String speciality)
{
this.speciality=speciality;
}
public void setFee(double fee)
{
this.fee=fee;
}
//getter methods
public String getSpeciality()
{
return speciality;
}
public double getFee()
{
return fee;
}
//override equals methods that returns true if
//two objects are equals otherwise returns false
@Override
public boolean equals(Object obj)
{
Doctor other=(Doctor)obj;
return super.getName().equals(other.getName()) &&
super.getAge()==other.getAge() &&
speciality.equals(other.getSpeciality())&&
fee==fee;
}
//Override toString method that returns the name, age
//speciality and fee as string representation
@Override
public String toString()
{
return super.toString()+"Speciality : "+speciality+
" Fee : $"+fee;
}
}
-----------------------------------------------
//Lawyer.java that extends the super class SimplePerson
public class Lawyer extends SimplePerson
{
private String type;
private String courtDate;
//default constructor
public Lawyer()
{
//call super class default cosstructor
super();
}
//parameterized default constructor
public Lawyer(String name, int age, String type, String courtDate)
{
//call super class parameterized cosstructor
super(name, age);
this.type=type;
this.courtDate=courtDate;
}
//setter methods
public void setType(String type)
{
this.type=type;
}
public void setDate(String courtDate)
{
this.courtDate=courtDate;
}
//getter methods
public String getType()
{
return type;
}
public String getDate()
{
return courtDate;
}
//override equals methods that returns true if
//two objects are equals otherwise returns false
@Override
public boolean equals(Object obj)
{
Lawyer other=(Lawyer)obj;
return super.getName().equals(other.getName()) &&
super.getAge()==other.getAge() &&
type.equals(other.getType())&&
courtDate.equals(other.getDate());
}
//Override toString method that returns name , age
//type and court date
@Override
public String toString()
{
return super.toString()+"Type : "+type+
" "+"Court Date : "+courtDate;
}
}
------------------------------------------------
public class Wrestler extends SimplePerson
{
private String finishingMove;
private boolean face;
//default constructor
public Wrestler()
{
//calling super class default constror
super();
}
//default constructor
public Wrestler(String name, int age, String finishingMove, boolean face)
{
//calling super class parameter constror
super(name, age);
this.finishingMove=finishingMove;
this.face=face;
}
//setter methods
public void setFinishMove(String finishingMove)
{
this.finishingMove=finishingMove;
}
public void setFace(boolean face)
{
this.face=face;
}
//getter methods
public String getFinishMove()
{
return finishingMove;
}
public boolean getFace()
{
return face;
}
//Checking equality of wresteler
@Override
public boolean equals(Object obj)
{
//Cast obj to Wrestler type object
Wrestler other=(Wrestler)obj;
return super.getName().equals(other.getName()) &&
super.getAge()==other.getAge() &&
finishingMove.equals(other.getFinishMove())&&
face==other.getFace();
}
@Override
public String toString()
{
return super.toString()+"Finishing Move : "+finishingMove+
" "+"Is a face ?"+face;
}
}
--------------------------------------------
//Driver.java
//Test the classes Doctor, Lawyer and Wrestler and print
//their informations
public class Driver
{
public static void main(String[] args)
{
System.out.println("Testing the Doctor Class");
System.out.println("Creatubg Doctor via default Construcor");
System.out.println("Print Doctor Information");
//create a defaul constructor
Doctor doctor=new Doctor();
//print object information
System.out.println(doctor.toString());
System.out.println("Changing default Doctor's name, speciality, and " +
"fee via mutators.");
doctor.setName("Matt Smith");
doctor.setAge(65);
doctor.setSpeciality("Time Lord");
doctor.setFee(4.95);
System.out.println("Printing Doctor's Information");
System.out.println(doctor.toString());
System.out.println("Creating Doctor via parameterized constructor");
System.out.println("Print Doctor's Information");
Doctor otherDoctor=
new Doctor("Albert W. Wiley", 62, "Robotics",200);
//testing if two doctors are equal
System.out.println("Testing if the two doctors are equal. ");
System.out.println(doctor.equals(otherDoctor));
//Create Lawyer instance
Lawyer lawyer=new Lawyer("Saul Goodman", 44, "Criminal Lawyer",
"4/6/2015");
//print lawyer information
System.out.println(lawyer.toString());
//Create a Wrestler instance
Wrestler wrestler=new Wrestler("Daniel Brain",
33, "Yes Lock", true);
//print wrestler information
System.out.println(wrestler.toString());
}
}
-------------------------------------------------
Sample output:
Testing the Doctor Class
Creatubg Doctor via default Construcor
Print Doctor Information
Name : No Name Yet
Age : 0
Speciality : None
Fee : $0.0
Changing default Doctor's name, speciality, and fee via mutators.
Printing Doctor's Information
Name : Matt Smith
Age : 65
Speciality : Time Lord
Fee : $4.95
Creating Doctor via parameterized constructor
Print Doctor's Information
Testing if the two doctors are equal.
false
Name : Saul Goodman
Age : 44
Type : Criminal Lawyer
Court Date : 4/6/2015
Name : Daniel Brain
Age : 33
Finishing Move : Yes Lock
Is a face ?true
Hope this helps you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.