I\'m having trouble with this one. I\'ve tried to make it work but I just can\'t
ID: 3809971 • Letter: I
Question
I'm having trouble with this one. I've tried to make it work but I just can't seem to get it right.
Write a program that consists of two class files. One class is called Employee.java and one is called Company.java.
The Employee has the following properties: Name, salary, and if the Employee receives a bonus or not. Figure out what data types to use.
The Employee class has a constructor that takes the name of the employee and if the Employee receives a bonus as parameters. The salary is set to 40,000 by the constructor and not passed as a parameter.
There needs to be a setter for the salary and getters for the name and the salary.
The getter for the salary will return the salary. If the employee however receives a bonus, then the getter will return the employees salary plus a $3,000 bonus.
The second class is called company and it contains the main method. Inside the company class, create two employee objects:
Helen Fry, who receives no bonus.
Kent Waite, who receives a bonus.
By calling the correct getters and setters of the employee objects, first print out the name and salary of both employees on the screen.
Now change Helen Fry’s salary to 55,000 and Kent Waite’s salary to 50,000. Then print out each Employee’s name and salary on the screen again.
The output should look like this:
These are the employees of our company and how much they earn:
Helen Fry $40000
Kent Waite $43000
Now we give them a big pay raise:
Helen Fry $55000
Kent Waite $53000
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
############# Company.java #########
public class Company {
// instance variables
private String name;
private double salary;
private boolean isBonus;
// constructor
public Company(String name, boolean isBonus) {
this.name = name;
this.isBonus = isBonus;
salary = 40000;
}
public double getSalary() {
if(isBonus)
return (salary+3000);
else
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getName(){
return name;
}
}
#########################################
public class CompanyTest {
public static void main(String[] args) {
// creating company objects
Company c1 = new Company("Helen Fry", false);
Company c2 = new Company("Kent Waite", true);
System.out.println("These are the employees of our company and how much they earn:");
System.out.println(c1.getName()+" $"+c1.getSalary());
System.out.println(c2.getName()+" $"+c2.getSalary());
c1.setSalary(55000);
c2.setSalary(50000);
System.out.println("Now we give them a big pay raise:");
System.out.println(c1.getName()+" $"+c1.getSalary());
System.out.println(c2.getName()+" $"+c2.getSalary());
}
}
/*
Sample run:
These are the employees of our company and how much they earn:
Helen Fry $40000.0
Kent Waite $43000.0
Now we give them a big pay raise:
Helen Fry $55000.0
Kent Waite $53000.0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.