Answer to Question #272696 in Java | JSP | JSF for User 12345

Question #272696

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 

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


1
Expert's answer
2021-11-28T05:27:04-0500
public class Main
{
	public static void main(String[] args) {
	    Worker w=new Worker(112,"John",4050.50);
	    System.out.println(w.toString());
	    w.addHours(12);
	    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();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS