Hello i am working on a project for my programming course and i am a little lost. The assignment is as follows:
Part A
Do all of problem 3.13 (Employee Class), including the main, as written.
Part B
Now, suppose a new company policy says that instead of the monthly salary we will now store the yearly salary. We will need to make this change inside Employee and support the new usage, without breaking existing code (such as the old main) that relies on the old version.
Remove the monthly salary instance variable from Employee and add an instance variable for yearly salary (with accessor and mutator).
Change the existing public accessor and mutator for the monthly salary so that they will still work as originally expected, that is, the existing main method, unchanged, will have the same results it did when you ran it for part A, but inside Employee we are using the new instance variable. We should also be able to write new code using Employee that works with yearly salaries directly.
Think this through. If someone tries to setYearlySalary(50000) what would they expect to happen? If someone tries to setMonthlySalary(1000) what would they expect to happen? Does your code do this?
Add a new class with a main, NewEmployeeHarness, and test the additions to Employee.
Part C
Add a new class Position, which has a String title, a double bonus (must be non-negative), and an Employee worker. Include the usual methods, and a parameterized constructor that takes title and bonus.
In the mutator for worker, if the worker passed in is not null, increase that worker's yearly salary by the bonus.
Whenever the bonus for the position is changed, update the salary for the worker by the same amount (if there is a worker). (That is, if the bonus goes from 100 to 150, we'd only add the 50 to the salary, since they already got the 100 when they became the worker). You can assume bonuses only increase. (You don't have to worry about the worker losing the bonus when replaced by another worker)
Test the class Position later in the same main as for part B.
I have done all of part A, Here is what i have done to this point:
Employee Class
public class Employee {
private String firstName;
private String lastName;
private double monthlySalary;
//constructor to set class variables
public Employee(String firstName, String lastName,
double monthlySalary) {
this.firstName = firstName;
this.lastName = lastName;
this.monthlySalary = monthlySalary;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
public void raiseSalary() {
monthlySalary = monthlySalary + monthlySalary + 0.10;
}
}//end of Employee class
Test
public class EmployeeMain {
public static void main(String[] args) {
// 2 Instances of employees
Employee emp1 = new Employee("John", "Doe", 16000);
Employee emp2 = new Employee("Jane", "Doe", 13000);
int Number_of_Months = 12;
System.out.printf("emp1 yearly salary %5.2f \n",
emp1.getMonthlySalary() * Number_of_Months);
System.out.printf("emp2 yearly salary %5.2f \n",
emp2.getMonthlySalary() * Number_of_Months);
//print salary 10 % on monthly salary
emp1.raiseSalary();
emp2.raiseSalary();
//print new yearly salary
System.out.printf("emp1 new yearly salary %5.2f \n",
emp1.getMonthlySalary() * Number_of_Months);
System.out.printf("emp2 new yearly salary %5.2f \n",
emp2.getMonthlySalary() * Number_of_Months);
}
}
Please find the classes needed for Part A,B and C:
///////////////////////Employee.java//////////////////////////
package test;
//class Employee
public class Employee {
private String firstName;
private String lastName;
//private double monthlySalary;
private double yearlySalary;
//constructor to set class variables
public Employee(String firstName, String lastName,double
monthlySalary) {
this.firstName = firstName;
this.lastName = lastName;
//this.monthlySalary = monthlySalary;
this.setMonthlySalary(monthlySalary);//change for Part
B
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getMonthlySalary(){
//return this.monthlySalary;
return (this.yearlySalary/12);//change for Part
B
}
public void setMonthlySalary(double monthlySalary) {
//this.monthlySalary = monthlySalary;
this.yearlySalary = monthlySalary * 12;//change for
Part B
}
public void raiseSalary() {
//monthlySalary = monthlySalary + monthlySalary *
0.10;
this.yearlySalary = this.yearlySalary +
this.yearlySalary*0.10;//change for part B
}
//getter and setter for yearlySalary
public double getYearlySalary() {
return yearlySalary;
}
public void setYearlySalary(double yearlySalary) {
this.yearlySalary = yearlySalary;
}
}//end of Employee class
//////////////////////////////////Position.java//////////////////////////////
package test;
//Class Position
public class Position {
//private fields
private String title;
private double bonus;
private Employee worker;
//constructor with title and bonus
public Position(String title, double bonus) {
this.title = title;
this.bonus = bonus;
}
//getter for title
public String getTitle() {
return title;
}
//setter for title
public void setTitle(String title) {
this.title = title;
}
//getter for bonus
public double getBonus() {
return bonus;
}
//setter for bonus
public void setBonus(double bonus) {
double existingBonus = this.bonus;
//get the existing bonus
this.bonus = bonus; //set new
bonus
//assuming bonus will always
increase get the increased bonus amount
double increasedBonusAmount =
this.bonus - existingBonus;
if(this.worker!=null){
//adjust
worker's yerly salary with the i ncreased amount
this.worker.setYearlySalary(worker.getYearlySalary()+increasedBonusAmount);
}
}
//getter for worker
public Employee getWorker() {
return worker;
}
//setter for worker
public void setWorker(Employee worker) {
if(worker!=null){
this.worker =
worker;//updateworker instance field
//update
worker's yearly salary with bonus
this.worker.setYearlySalary(worker.getYearlySalary()+bonus);
}
}
}
//////////////////////////////////EmployeeMain.java/////////////////////////////////////
package test;
public class EmployeeMain {
public static void main(String[] args) {
// 2 Instances of employees
Employee emp1 = new Employee("John", "Doe",
16000);
Employee emp2 = new Employee("Jane", "Doe",
13000);
int Number_of_Months = 12;
System.out.printf("emp1 yearly salary %5.2f \n",
emp1.getMonthlySalary() * Number_of_Months);
System.out.printf("emp2 yearly salary %5.2f \n",
emp2.getMonthlySalary() * Number_of_Months);
//print salary 10 % on monthly salary
emp1.raiseSalary();
emp2.raiseSalary();
//print new yearly salary
System.out.printf("emp1 new yearly salary %5.2f
\n",
emp1.getMonthlySalary() * Number_of_Months);
System.out.printf("emp2 new yearly salary %5.2f
\n",
emp2.getMonthlySalary() * Number_of_Months);
//PartB test
Employee NewEmployeeHarness = new
Employee("Harness","Williams",15000);
//calling existing method getMonthlySalary
System.out.printf("\n\n(Through getMonthlySalary
Call)NewEmployeeHarness yearly salary %5.2f \n",
NewEmployeeHarness.getMonthlySalary() * Number_of_Months);
//calling new getter for yearly salary
System.out.printf("(Through getYearlySalary
Call)NewEmployeeHarness yearly salary %5.2f \n",
NewEmployeeHarness.getYearlySalary());
NewEmployeeHarness.setMonthlySalary(16000); //set
monthly salary by existing method
System.out.println("\n\nYearly salary is set by
existing setMonthlySalary method.");
System.out.printf("\nMonthly salary: %5.2f ",
NewEmployeeHarness.getMonthlySalary());
System.out.printf("\nYearly salary: %5.2f ",
NewEmployeeHarness.getYearlySalary());
NewEmployeeHarness.setYearlySalary(204000); //set
yearly salary by new method
System.out.println("\n\nYearly salary is set by new
setYearlySalary method.");
System.out.printf("\nMonthly salary: %5.2f ",
NewEmployeeHarness.getMonthlySalary());
System.out.printf("\nYearly salary: %5.2f ",
NewEmployeeHarness.getYearlySalary());
NewEmployeeHarness.raiseSalary();
System.out.println("\n\nYearly salary is raised by 10%
through raiseSalary method.");
System.out.printf("\nMonthly salary: %5.2f ",
NewEmployeeHarness.getMonthlySalary());
System.out.printf("\nYearly salary: %5.2f ",
NewEmployeeHarness.getYearlySalary());
//Part C
//Create a Developer position with Bonus 500.00
Position pos1 = new
Position("Developer",500.00);
pos1.setWorker(emp1); //set emp1 in this
position
if(pos1.getWorker()!=null){
System.out.printf("\nYearly salary
of %s %s in %s Position (with bonus %5.2f) is %5.2f",
pos1.getWorker().getFirstName(),pos1.getWorker().getLastName(),pos1.getTitle(),
pos1.getBonus(),pos1.getWorker().getYearlySalary());
}
//Create a Tester position with Bonus 400.00
Position pos2 = new Position("Tester",400.00);
pos2.setWorker(NewEmployeeHarness);//set
NewEmployeeHarness for this position
if(pos2.getWorker()!=null){
System.out.printf("\nYearly salary
of %s %s in %s Position (with bonus %5.2f) is %5.2f",
pos2.getWorker().getFirstName(),pos2.getWorker().getLastName(),pos2.getTitle(),
pos2.getBonus(),pos2.getWorker().getYearlySalary());
//update bonus for tester
position
pos2.setBonus(475.00); //bonus for
position 2 has changed from 400 to 475
System.out.printf("\nYearly salary
of %s %s in %s Position (with bonus %5.2f) is %5.2f",
pos2.getWorker().getFirstName(),pos2.getWorker().getLastName(),pos2.getTitle(),
pos2.getBonus(),pos2.getWorker().getYearlySalary());
}
}
}
===============================================
OUTPUT
===============================================
emp1 yearly salary 192000.00
emp2 yearly salary 156000.00
emp1 new yearly salary 211200.00
emp2 new yearly salary 171600.00
(Through getMonthlySalary Call)NewEmployeeHarness yearly salary
180000.00
(Through getYearlySalary Call)NewEmployeeHarness yearly salary
180000.00
Yearly salary is set by existing setMonthlySalary method.
Monthly salary: 16000.00
Yearly salary: 192000.00
Yearly salary is set by new setYearlySalary method.
Monthly salary: 17000.00
Yearly salary: 204000.00
Yearly salary is raised by 10% through raiseSalary method.
Monthly salary: 18700.00
Yearly salary: 224400.00
Yearly salary of John Doe in Developer Position (with bonus 500.00)
is 211700.00
Yearly salary of Harness Williams in Tester Position (with bonus
400.00) is 224800.00
Yearly salary of Harness Williams in Tester Position (with bonus
475.00) is 224875.00
Get Answers For Free
Most questions answered within 1 hours.