Define a class named Employee that has four data fields:
name: String
hoursWorked: double
hourlyPayrate: double
bonusRate: double
The class has two constructors:
1. constructor without arguments: initialize name to empty string,
and all other data fields to 0.0
2. constructor with a String type arguments and three double type
arguments. Use them to initialize the data fields properly
The class also has:
1. Getter method for each data field to return the value of the data field
2. Setter method for each data field to set a new value of that data field
3. A method called calculateRegularPay to calculate and return the regular salary using the following formula:
regular salary of an employee = hoursWorked * hourlyPayRate
4. A method called calculateBonus to calculate and return the amount of bonus using the following formula:
bonus amount of an employee = regular salary of an employee * bonus rate
where the regular salary of an employee is the return value of the method in 3
5. A method called calculateGrossPay to calculate and return the gross pay using the following formula:
gross pay of an employee = regular salary of an employee + bonus amount of the employee
where the regular salary of an employee is the return value of the method in 3 and the bonus amount is the return value of the method in 4
Then write a separate demo program to:
create an empty employee. Call the setters methods of the object to set values for all data fields. Call the three calculate methods to calculate regular pay, bonus amount, and gross pay for the employee. Display results.
If you have any doubts, please give me comment...
class Employee{
private String name;
private double hoursWorked;
private double hourlyPayrate;
private double bonusRate;
public Employee(){
name = "";
hoursWorked = 0.0;
hourlyPayrate = 0.0;
bonusRate = 0.0;
}
public Employee(String name, double hoursWorked, double hourlyPayrate, double bonusRate){
this.name = name;
this.hoursWorked = hoursWorked;
this.hourlyPayrate = hourlyPayrate;
this.bonusRate = bonusRate;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param hoursWorked the hoursWorked to set
*/
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
/**
* @param hourlyPayrate the hourlyPayrate to set
*/
public void setHourlyPayrate(double hourlyPayrate) {
this.hourlyPayrate = hourlyPayrate;
}
/**
* @param bonusRate the bonusRate to set
*/
public void setBonusRate(double bonusRate) {
this.bonusRate = bonusRate;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the hoursWorked
*/
public double getHoursWorked() {
return hoursWorked;
}
/**
* @return the hourlyPayrate
*/
public double getHourlyPayrate() {
return hourlyPayrate;
}
/**
* @return the bonusRate
*/
public double getBonusRate() {
return bonusRate;
}
public double calculateRegularPay(){
return hoursWorked * hourlyPayrate;
}
public double calculateBonus(){
return calculateRegularPay() * (bonusRate/100);
}
public double calculateGrossPay(){
return calculateRegularPay()+calculateBonus();
}
}
public class EmployeeDemo{
public static void main(String[] args) {
Employee employee = new Employee();
employee.setName("ANUNAGA");
employee.setHoursWorked(30);
employee.setHourlyPayrate(25);
employee.setBonusRate(5);
double regularPay = employee.calculateRegularPay();
double bonusPay = employee.calculateBonus();
double grossPay = employee.calculateGrossPay();
System.out.println("Regular Pay: "+regularPay);
System.out.println("Bonus Pay: "+bonusPay);
System.out.println("Gross Pay: "+grossPay);
}
}
Get Answers For Free
Most questions answered within 1 hours.