Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Define a class named Employee whose objects are records for employees. Derive

ID: 3891889 • Letter: 1

Question

1. Define a class named Employee whose objects are records for employees. Derive this class from the class Person given in Lesson. An employee record inherits an employee's name from the class Person. In addition, an employee record contains an annual salary represented as a single value of type double, a hire date that gives the year hired as a single value of type int, and an identification number that is a value of type String. Give your class a reasonable complement of constructors, accessor methods, and mutator methods. Write a program to fully test your class definition. 2. Define a class named Doctor whose objects are records for a clinic's doctors. Derive this class from the class Person given in Lesson. A Doctor record has the doctor's name-defined in the class Person-a specialty as a string (for example Pediatrician, Obstetrician, General Practitioner, and so on), and an office-visit fee (use the type double). Give your class a reasonable complement of constructors and accessor methods, Write a driver program to test all your methods. 3. Create a base class called Vehicle that has the manufacturer's name (type String), number of cylinders in the engine (type int), and owner (type Person given in Lesson). Then create a class called Truck that is derived from Vehicle and has additional properties: the load capacity in tons (type double, since it may contain a fractional part) and towing capacity in tons (type double). Give your classes a reasonable complement of constructors and accessor methods. Write a driver program that tests all your methods.

Explanation / Answer

1.

class Person
{
private String name;

public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}

class Employee extends Person
{
private double annualSalary;
private int yearHired;
private String id;

public Employee(String name,double annualSalary,int yearHired,String id)
{
super(name);
this.annualSalary = annualSalary;
this.yearHired = yearHired;
this.id = id;
}
public double getAnnualSalary()
{
return annualSalary;
}
public int getYearHired()
{
return yearHired;
}
public String getID()
{
return id;
}

public void setId(String id)
{
this.id = id;
}
public void setAnnualSalary()
{
this.annualSalary = annualSalary;
}

public void setYearHired(int yearHired)
{
this.yearHired = yearHired;
}

public String toString()
{
return "Employee ID "+id+" Name "+getName()+" Annual salary :$"+annualSalary+
" Year Hired : "+yearHired;
}
}
class Test
{
public static void main (String[] args)
{
  Employee emp = new Employee("Catherine Thomas",12000.89,2003,"E1007");
  System.out.println(emp);
}
}

Output:

Employee ID E1007 Name Catherine Thomas Annual salary :$12000.89 Year Hired : 2003

2.

class Person
{
private String name;

public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}

class Doctor extends Person
{
private double officeVisitFee;
private String speciality;

public Doctor(String name,double officeVisitFee,String speciality)
{
super(name);
this.officeVisitFee = officeVisitFee;
this.speciality = speciality;
}

//accessor methods
public double getOfficeVisitFee()
{
return officeVisitFee;
}

public String getSpeciality()
{
return speciality;
}

public String toString()
{
return "Doctor Name "+getName()+" Office Visit Fee :$"+officeVisitFee+
" Speciality : "+speciality;
}
}
class Test
{
public static void main (String[] args)
{
  Doctor doc = new Doctor("Edison Thomas",120.89,"Pediatrician");
  System.out.println(doc);
}
}

Output:

Doctor Name Edison Thomas Office Visit Fee :$120.89 Speciality : Pediatrician

3.

class Person
{
private String firstName;
private String lastName;
  
public Person(String firstName,String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String toString()
{
return firstName+" "+lastName;
}
}
class Vehicle
{
private double horsepower;
private String manufacturerName;
private int numberCylinders;
private Person owner;
  
  
public boolean equals(Vehicle v)
{
if(this.horsepower == v.horsepower && this.manufacturerName == v.manufacturerName && this.numberCylinders == v.numberCylinders && this.owner == v.owner)
return true;
else
return false;
}
//set and get methods
public double getHorsepower()
{
return horsepower;
  
}
public int getNumberCylinder()
{
return numberCylinders;
}
public String getManufacturerName()
{
return manufacturerName;
}
public Person getOwner()
{
return owner;
}
public void setHorsepower(double horsepower)
{
this.horsepower = horsepower;
}
public void setManufacturerName(String manufacturerName)
{
this.manufacturerName = manufacturerName;
}
public void setOwner(Person owner)
{
this.owner = owner;
}
public String toString()
{
return "Horsepower : "+horsepower+" Manufacturer name : "+manufacturerName+" Number of cylinders : "+numberCylinders+ " Owner : "+owner;
}
//parameterized constructor
public Vehicle(String manufacturerName, int numberCylinders, double horsepower, Person owner)
{
this.manufacturerName = manufacturerName;
this.numberCylinders = numberCylinders;
this.horsepower = horsepower;
this.owner = owner;
}
//copy constructor
public Vehicle(Vehicle v)
{
this.manufacturerName = v.manufacturerName;
this.numberCylinders = v.numberCylinders;
this.horsepower = v.horsepower;
this.owner = v.owner;
}
  
}
class Truck extends Vehicle
{
private double loadCapacity;
private double towingCapacity;
  

//set and get methods
public double getLoadCapacity()
{
return loadCapacity;
}
public double getTowingCapacity()
{
return towingCapacity;
}
public void setLoadCapacity(double loadCapacity)
{
this.loadCapacity = loadCapacity;
}
public void setTowingCapacity(double towingCapacity)
{
this.towingCapacity = towingCapacity;
}
public String toString()
{
return super.toString() + "Load Capacity : "+loadCapacity+" Towing Capacity : "+towingCapacity;
}
//parameterized constructor
public Truck(String manufacturerName, int numberCylinder, double horsepower, Person owner, double loadCapacity, double towingCapacity)
{
super(manufacturerName,numberCylinder,horsepower,owner);
this.loadCapacity = loadCapacity;
this.towingCapacity = towingCapacity;
}
//copy constructor
public Truck(Truck t)
{
super(t.getManufacturerName(),t.getNumberCylinder(),t.getHorsepower(),t.getOwner());
this.loadCapacity = t.loadCapacity;
this.towingCapacity = t.towingCapacity;
}
  
}
class Test
{
   public static void main (String[] args)
   {
   Person p = new Person("James","Gosling");
       Truck T = new Truck("Ford",6,567.34,p,400.5,345.54);
         
       System.out.println(T);
       Truck T1 = new Truck(T);
         

         
   }
}

Output:

Horsepower : 567.34 Manufacturer name : Ford Number of cylinders : 6 Owner : James GoslingLoad Capacity : 400.5 Towing Capacity : 345.54

Do ask if any doubt. Please upvote.