in java Consider the following problem description and write
in java. 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
class Employee
{
long id;
String name;
String dob;
String pno;
int sal;
String work()
{
return \"I am an employee\";
}
Employee(long idno, String n, String d, String phno,int salary)
{
id = idno;
name = n;
dob = d ;
pno = phno;
sal = salary;
}
Employee()
{
}
}
class Executive extends Employee
{
Executive(long idno, String n, String d, String phno,int salary)
{
super(idno,n,d,phno,salary);
}
String work()
{
return \"I work as an executive\";
}
void display()
{
System.out.printf(\"My ID is: \"+id);
System.out.println(\"My Name is:\"+name);
System.out.println(\"My Date of Birth is :\"+dob);
System.out.println(\"My Phone no is :\"+pno);
System.out.println(\"My Salary is :\"+sal);
}
}
class SoftwareEngineer extends Employee
{
SoftwareEngineer(long idno, String n, String d, String phno,int salary)
{
super(idno,n,d,phno,salary);
}
String work()
{
return \"I work as an SoftwareEngineer\";
}
void display()
{
System.out.printf(\"My ID is: \"+id);
System.out.println(\"My Name is:\"+name);
System.out.println(\"My Date of Birth is :\"+dob);
System.out.println(\"My Phone no is :\"+pno);
System.out.println(\"My Salary is :\"+sal);
}
}
class SoftwareManager extends SoftwareEngineer
{
SoftwareManager(long idno, String n, String d, String phno,int salary)
{
super(idno,n,d,phno,salary);
}
String work()
{
return \"I work as an Software Manager\";
}
void display()
{
System.out.printf(\"My ID is: \"+id);
System.out.println(\"My Name is:\"+name);
System.out.println(\"My Date of Birth is :\"+dob);
System.out.println(\"My Phone no is :\"+pno);
System.out.println(\"My Salary is :\"+sal);
}
}
class Company
{
public static void main(String args[])
{
Employee e = new Employee();
// SoftwareEngineer se = new SoftwareEngineer();
// SoftwareManager sm= new SoftwareManager();
//Employee e = new Employee(1234567890,\"John Smith\",\"1-1-1981\", \"555-555-12345\",12000);
Executive ex = new Executive(1234567890,\"John Smith\",\"1-1-1981\", \"555-555-12345\",12000);
System.out.println(e.work()+\"\\t\"+ex.work());
ex.display();
SoftwareEngineer se = new SoftwareEngineer(98765432,\"Jane Doe\",\"1-1-1988\", \"555-555-12345\",10000);
System.out.println(e.work()+\"\\t\"+se.work());
se.display();
SoftwareManager sm = new SoftwareManager(998877665,\"Joe Green\",\"1-1-1978\", \"555-555-12345\",11000);
System.out.println(e.work()+\"\\t\"+sm.work());
sm.display();
}
}


