Build a class Worker having id(int), name(Sting), hoursWorked(int), salRate (double) a. Provide a constructor Worker(int id, String name, double salRate). The hoursWorked will be initially 0 b. Provide getters for each but setters for only salRate c. Provide a method addHours(int h) that increase the number of hoursWorked by the provided value. d. Provide a method getCurrentSalary() that calculates and returns the current salary i.e. the hoursWorked times the salRate. e. Override the toString to get appropriate output
public class Main
{
public static void main(String[] args) {
Worker w=new Worker(1234,"ABC",6000);
System.out.println(w.toString());
w.addHours(15);
System.out.println(w.toString());
}
}
class Worker{
private int id;
private String name;
private int hoursWorked;
private double salRate;
public Worker(int id, String name, double salRate){
this.id=id;
this.name=name;
this.salRate=salRate;
hoursWorked=0;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public int getHoursWorked(){
return hoursWorked;
}
public double getSalRate(){
return salRate;
}
public void setSalRate(double salRate){
this.salRate=salRate;
}
public void addHours(int h){
hoursWorked=hoursWorked+h;
}
public double getCurrentSalary(){
return (hoursWorked*salRate);
}
public String toString(){
return "Current Salary = "+getCurrentSalary();
}
}
Comments
Leave a comment