Use java Program: Your Employee class will have the employees name and hourly ra
ID: 3824088 • Letter: U
Question
Use java Program:
Your Employee class will have the employees name and hourly rate. There will be a method in the class called getPay that will receive the hours worked and return the pay. The name and hourly rate will be private. You will use get and set methods to assign values to the name and hourly rate. You will also have a displayEmployee method that prints out the employee name and hourly rate. The rate should show 2 decimal places: Example: Robin Banks earns $9.50 Calculate Pay method: If an employee works 40 hours or less the pay is simple the rate times the hours worked. If the employee works more than 40 hours he gets paid at the regular rate for the first 40 hours, and is paid at one and a half for each overtime hour. The main program should declare 3 different employees. One should work 30 hours, one 40, and one 42. Display the employee information and the weeks pay. A class should not interact with the user. If an invalid value is received it simple does not accept it. It does not display a message, it does not exit. It is up to the calling program to check if the value was accepted and display an appropriate message.
Explanation / Answer
public class Employee
{
private String name;
private double hourlyRate;
public void setName(String name)
{
this.name=name;
}
public void setHourlyRate(double rate)
{
hourlyRate=rate;
}
public String getName()
{
return name;
}
public double getHourlyRate()
{
return hourlyRate;
}
public double getPay(int hours)
{
double pay=0;
if(hours<=40)
{
pay=hours*hourlyRate;
}
else
{
pay=40*hourlyRate+(hours-40)*1.5*hourlyRate;
}
return pay;
}
public void displayEmployee()
{
System.out.println(String.format("%s earns %.2f", name,hourlyRate));
}
public static void main(String arg[])
{
Employee a=new Employee();
a.name="a";
a.hourlyRate=10;
Employee b=new Employee();
b.name="b";
b.hourlyRate=11;
Employee c=new Employee();
c.name="c";
c.hourlyRate=12;
System.out.println(a.getPay(30));
System.out.println(a.getPay(40));
System.out.println(a.getPay(42));
a.displayEmployee();
b.displayEmployee();
c.displayEmployee();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.