First, we define the Employee class
Source Code..
class Employee {
double salary;
double workHours;
// Constructor to
initialize salary and workHours
public
Employee(double salary, double workHours) {
this.salary =
salary;
this.workHours
= workHours;
}
// Method to set
salary and workHours
public void
getInfo(double salary, double workHours) {
this.salary =
salary;
this.workHours
= workHours;
}
// Method to add
bonus if salary < $600
public void
addBonus() {
if (salary
< 600) {
salary +=
15;
}
}
// Method to add
extra pay if workHours > 8
public void
addWork() {
if (workHours
> 8) {
salary +=
10;
}
}
// Method to get
final salary
public double
getSalary() {
return salary;
}
}
The Employee class has two instance variables salary and
workHours, which represent the salary and number of hours worked by an
employee, respectively. The class also has a constructor that takes in two
arguments salary and workHours and initializes the instance variables. There
are also three methods:
getInfo(): This method takes in two arguments salary and
workHours and sets the instance variables salary and workHours to their
respective values.
addBonus(): This method adds $15 to the salary of the
employee if their salary is less than $600.
addWork(): This method adds $10 to the salary of the
employee if they worked more than 8 hours per day.
getSalary(): This method returns the final salary of the
employee.
Next, we define the Main class and the main() method
public class Main {
public static void
main(String[] args) {
// Create an
Employee object with initial salary and work hours
Employee emp =
new Employee(500, 9);
// Add bonus
and extra pay
emp.addBonus();
emp.addWork();
// Print final
salary
System.out.println("Final salary of employee: $" +
emp.getSalary());
}
}
n the main() method, we create an Employee object emp with
an initial salary of $500 and work hours of 9. We then call the addBonus() and
addWork() methods on this object to add any applicable bonuses or extra pay to
the employee's salary. Finally, we print the final salary of the employee using
the getSalary() method of the Employee class.
When we run this Java program, we get the output:
Final salary of employee: $510.0
No comments:
Post a Comment