Consider the following problem description and write java co
Consider the following problem description and write java code
A company need to store its employees information. Each employee has an id, name, date of birth, phone number and salary. Using the diagram, write your java classes that shows the inheritance between these classes, and consider the following:
Class employee has a method work() which returns the statement “I am an employee”
Class employee overrides toString() method to return the employee information.Or if you don’t what to override toString() you can write your own method to return the employee info. (both ways are accepted)
Each subclass should overrides the method work() to return its work. You need to call work() of the superclass. For example:
Method work() in executive class should returns:
I am an employee.. I work as an executive.
So in each subclass work() method first call it’s parent work() method by using super. Which will return “I am an employee ..” and then add the child own details. in this example “I work as an executive”
Implement the main class Company which constructs objects from the 3 subclasses, and test all of their methods.
Sample Run will result in:
I am an employee.. I work as an executive.
My ID is: 1234567890
My Name is: John Smith
My Date of Birth is: 1-1-1981
My phone is: 555-555-12345
My Salary is: 12,000
I am an employee.. I work as a Software Engineer.
My ID is: 0987654321
My Name is: Jane Doe
My Date of Birth is: 1-1-1988
My phone is: 555-555-12345
My Salary is: 10,000
I am an employee.. I work as a Software Manager.
My ID is: 9988776655
My Name is: Joe Green
My Date of Birth is: 1-1-1978
My phone is: 555-555-12345
My Salary is: 11,000
Solution
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;
import java.util.Date;
public class Employee{
private int id;
private Date dob;
private double salary;
private String name,phone_number;
public Employee()
{
salary = 0.0;
name = \"\";
id = 0;
phone_number = \"\";
dob = new Date();
}
public Employee(int id,String phone_number,Date dob,double salary,String name)
{
this.id = id;
this.phone_number = phone_number;
this.dob = dob;
this.salary = salary;
this.name = name;
}
public String work()
{
return \"I am an employee\";
}
public String toString()
{
return work()+\"\ My ID is : \"+this.id+\"\ My Name is : \"+this.name+\"\ My Date of Birth is : \"+dob.toString()
+\"\ My phone number is : \"+this.phone_number+\"\ My Salary is : \"+this.salary;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;
import java.util.Date;
public class Executive extends Employee {
public Executive()
{
}
public Executive(int id,String phone_number,Date dob,double salary,String name)
{
super(id,phone_number,dob,salary,name);
}
public String work()
{
return super.work()+\"I work as an executive.\";
}
public String toString()
{
return super.toString();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;
import java.util.Date;
public class Manager extends Employee {
public Manager()
{}
public Manager(int id,String phone_number,Date dob,double salary,String name)
{
super(id,phone_number,dob,salary,name);
}
public String work()
{
return super.work()+\"I am manager too.\";
}
public String toString()
{
return super.toString();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;
import java.util.Date;
public class Company {
public static void main(String[] args)
{
Employee e1 = new Employee(1234567890,\"555-555-12345\",new Date(),12000.0,\"John\");
Manager m1 = new Manager(1234567891,\"556-555-12345\",new Date(),20000.0,\"Wayne\");
Executive ee1 = new Executive(1234567892,\"557-555-12345\",new Date(),32000.0,\"David\");
e1.work();
m1.work();
ee1.work();
System.out.println(e1);
System.out.println(m1);
System.out.println(ee1);
}
}



