Write a java eclipse program that will implement an Employee class. The class sh
ID: 3668264 • Letter: W
Question
Write a java eclipse program that will implement an Employee class. The class should have three member variables called name, hoursWorked, and hourlyRate.
The class should exhibit “data hiding”.
Write get and set methods for each member variable.
Write a constructor to initialize all member variables.
Write a method called GetPay that returns the pay that the employee should make. An employee’s pay is the hours worked times the hourly rate.
You should declare two instance of the Employee class inside main() and initialize them with different data (use constructor).
Print name and pay for each employee (hint: use printf)
Explanation / Answer
public class Employee
{
private String myName;
private double myHoursWorked;
private double myHourlyRate;
private double myWages;
public Employee(String name, double hoursWorked, double hourlyRate)
{
myName = name;
myHoursWorked = hoursWorked;
myHourlyRate = hourlyRate;
myWages = calculateWages();
}
public String getName()
{
return myName;
}
public double getHoursWorked()
{
return myHoursWorked;
}
public double getHourlyRate()
{
return myHourlyRate;
}
public double getWages()
{
return myWages;
}
private double calculateWages()
{
double wages;
wages = myHoursWorked*myHourlyRate;
if (myHoursWorked > 40.0)
wages = wages + (myHoursWorked - 40.0)*myHourlyRate*0.5;
return wages;
}
public String toString()
{
return "Name: " + myName +" Hours Worked: " + myHoursWorked +" Hourly Rate: " + myHourlyRate +" Wages: " + myWages;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.