Question

Hello i am working on a project for my programming course and i am a little...

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);
}
}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its variables (firstName and lastName). It should contain an abstract method called payPrint. Below is the source code for the Employee9C superclass: public class Employee9C {    //declaring instance variables private String firstName; private String lastName; //declaring & initializing static int variable to keep running total of the number of paychecks calculated static int counter = 0;    //constructor to set instance variables public Employee9C(String...
How do I get this portion of my List Iterator code to work? This is a...
How do I get this portion of my List Iterator code to work? This is a portion of the code in the AddressBookApp that I need to work. Currently it stops at Person not found and if it makes it to the else it gives me this Exception in thread "main" java.lang.NullPointerException    at AddressBookApp.main(AddressBookApp.java:36) iter.reset(); Person findPerson = iter.findLastname("Duck"); if (findPerson == null) System.out.println("Person not found."); else findPerson.displayEntry(); findPerson = iter.findLastname("Duck"); if (findPerson == null) { System.out.println("Person not found.");...
Hello, I need a output for the following code as well as screenshots from the eclipse...
Hello, I need a output for the following code as well as screenshots from the eclipse page showing the output. Thank you public class COMP1050 { public static void main(String[] args) { System.out.printf("before"); new CS(); System.out.printf(".after"); } } public class CS extends WIT { public CS() { System.out.printf(".cs"); } } public class WIT { public WIT() { System.out.printf(".wit"); } }
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
Hello. I have an assignment that is completed minus one thing, I can't get the resize...
Hello. I have an assignment that is completed minus one thing, I can't get the resize method in Circle class to actually resize the circle in my TestCircle claass. Can someone look and fix it? I would appreciate it! If you dont mind leaving acomment either in the answer or just // in the code on what I'm doing wrong to cause it to not work. That's all I've got. Just a simple revision and edit to make the resize...
I entered in this code for my programming fundamentals class for this question To encourage good...
I entered in this code for my programming fundamentals class for this question To encourage good grades, Hermosa High School has decided to award each student a bookstore credit that is 10 times the student’s grade point average. In other words, a student with a 3.2 grade point average receives a $32.0 credit. Create an application that prompts a student for a name and grade point average, and then passes the values to a method (computeDiscount) that displays a descriptive...
Whenever I try to run this program a window appears with Class not Found in Main...
Whenever I try to run this program a window appears with Class not Found in Main project. Thanks in Advance. * * 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 Assignment10; /** * * @author goodf */ public class Assignment10{ public class SimpleGeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;    /**...
This is a java assignment on repl.it my code works but I keep failing the tests....
This is a java assignment on repl.it my code works but I keep failing the tests. Can anyone help me Write the body of the fileAverage() method. Have it open the file specified by the parameter, read in all of the floating point numbers in the file and return their average (rounded to 1 decimal place) For the testing system to work, don't change the class name nor the method name. Furthermore, you cannot add "throws IOException" to the fileAverage()...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a class named Triangle that extends GeometricObject. The class contains: Three double fields named side1, side2, and side3 with default values = 1.0. These denote the three sides of the triangle A no-arg constructor that creates a default triangle A constructor that creates a triangle with parameters specified for side1, side2, and side3. An accessor method for each side. (No mutator methods for the sides)...
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...