In the Employee class, add an implementation for the calculatePension() method d
ID: 3877355 • Letter: I
Question
In the Employee class, add an implementation for the calculatePension() method
declared in the interface. Calculate the pension to date as the equivalent of a monthly
salary for every year in service.
4) Print the calculated pension for all employees.
************** I added the interface and I implemented the interface with Employee class but, I don't know how can i call the method in main class ************
import java.io.*;
public class ManagerTest
{ public static void main(String[] args)
{
InputStreamReader isr = null; //
BufferedReader br = null;
Employee[] staff = new Employee[3];
staff[0] = new Employee("Harry Hacker", 35000,
new Day(1989,10,1));
staff[1] = new Manager("Carl Cracker", 75000,
new Day(1987,12,15));
staff[2] = new Employee("Tony Tester", 38000,
new Day(1990,3,15));
int i;
for (i = 0; i < 3; i++) staff[i].raiseSalary(5);
for (i = 0; i < 3; i++) staff[i].print();
System.out.println("The PensionScheme for all Employee are: ");
}
}
class Employee implements PensionScheme
{ public Employee(String n, double s, Day d)
{ name = n;
salary = s;
hireDay = d;
}
public void print()
{ System.out.println(name + " " + salary + " "
+ hireYear());
}
public void raiseSalary(double byPercent)
{ salary *= 1 + byPercent / 100; //assume myPrecent=5. salary * 5 / 100
}
public int hireYear()
{ return hireDay.getYear();
}
@Override
public double calculatePension()
{
Day today = new Day();
double Pension = (today.getYear() - hireYear())*salary;
return Pension;
}
private String name;
private double salary;
private Day hireDay;
}
class Manager extends Employee
{ public Manager(String n, double s, Day d)
{ super(n, s, d);
secretaryName = "";
}
public void raiseSalary(double byPercent)
{ // add 1/2% bonus for every year of service
Day today = new Day();
double bonus = 0.5 * (today.getYear() - hireYear());
super.raiseSalary(byPercent + bonus);
}
public void setSecretaryName(String n)
{ secretaryName = n;
}
public String getSecretaryName()
{ return secretaryName;
}
private String secretaryName;
}
Explanation / Answer
import java.io.*;
interface PensionScheme
{
public double calculatePension();
}
class ManagerTest
{ public static void main(String[] args)
{
InputStreamReader isr = null; //
BufferedReader br = null;
Employee[] staff = new Employee[3];
staff[0] = new Employee("Harry Hacker", 35000,
new Day(1989,10,1));
staff[1] = new Manager("Carl Cracker", 75000,
new Day(1987,12,15));
staff[2] = new Employee("Tony Tester", 38000,
new Day(1990,3,15));
int i;
for (i = 0; i < 3; i++) staff[i].raiseSalary(5);
for (i = 0; i < 3; i++) staff[i].print();
System.out.println("The monthly PensionScheme for all Employee are: ");
for (i = 0; i < 3; i++) System.out.println(staff[i].calculatePension());
}
}
class Day
{
private int day;
private int month;
private int year;
public Day()
{
day = 0;
month = 0;
year = 0;
}
public Day(int year,int month,int day)
{
this.day = day;
this.month = month;
this.year = year;
}
public int getDay()
{
return day;
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
}
class Employee implements PensionScheme
{
public Employee(String n, double s, Day d)
{ name = n;
salary = s;
hireDay = d;
}
public void print()
{ System.out.println(name + " " + salary + " "
+ hireYear());
}
public void raiseSalary(double byPercent)
{ salary *= 1 + byPercent / 100; //assume myPrecent=5. salary * 5 / 100
}
public int hireYear()
{ return hireDay.getYear();
}
@Override
public double calculatePension()
{
Day today = new Day();
double Pension = (2018 - hireYear())*salary;// difference of hiring year and 2018
return Pension/12;
}
private String name;
private double salary;
private Day hireDay;
}
class Manager extends Employee
{ public Manager(String n, double s, Day d)
{ super(n, s, d);
secretaryName = "";
}
public void raiseSalary(double byPercent)
{ // add 1/2% bonus for every year of service
Day today = new Day();
double bonus = 0.5 * (2018 - hireYear());
super.raiseSalary(byPercent + bonus);
}
public void setSecretaryName(String n)
{ secretaryName = n;
}
public String getSecretaryName()
{ return secretaryName;
}
private String secretaryName;
}
output:
Harry Hacker 36750.0 1989
Carl Cracker 90375.0 1987
Tony Tester 39900.0 1990
The monthly PensionScheme for all Employee are:
88812.5
233468.75
93100.0