Write a java eclipse program that will implement an Employee class. The class sh
ID: 3668260 • 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
Payroll.java
//header file
import java.util.Scanner;
// create class
public class Payroll
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
//declare variables
String cleanInputBuffer;
String employeeName;
double hourlyRate;
double hoursWorked;
double sum;
boolean end = false;
System.out.println("Welcome to the Payroll Program! ");
while (end == false)
{
hourlyRate = -1;
hoursWorked = -1;
// getting name from user
System.out.println("Enter Name of Employee(enter 'stop' to exit program): ");
employeeName = input.nextLine();
if ("stop".equals(employeeName))
end = true;
else
// getting rate from user
System.out.println("Enter a positive hourly rate:");
hourlyRate = input.nextDouble();
while (hoursWorked < 0)
{
// getting hours from user
System.out.println("Enter a positive number of hours worked:");
hoursWorked = input.nextDouble();
}
sum = hourlyRate * hoursWorked;
// display output
System.out.printf("The employee %s was paid $ %.2f this week ", employeeName, sum);
cleanInputBuffer = input.nextLine();
}
}
}
// create class Employee
class Employee
{
// declare variable
private String name;
private double hourlyRate;
private double hoursWorked;
public Employee ()
{
//implicit call to object constructor occurs here
name = "";
hourlyRate = 0.0;
hoursWorked = 0.0;
}
// Three argument initialization constructor
public Employee(String employeenameIn, double hourlyRateInDollarsIn, double hoursWorkedInWeekIn) {
//implicit call to object constructor occurs here
name = employeenameIn;
hourlyRate = hourlyRateInDollarsIn;
hoursWorked = hoursWorkedInWeekIn;
} // end three argument intialization constructor
public void setName (String name) {
this.name = name;
}
public String getName () {
return name;
}
public void setpayRate (double payRate) {
this.hourlyRate = payRate;
}
public double getpayRate () {
return hourlyRate;
}
public void sethoursWorked (double hours) {
this.hoursWorked = hours;
}
public double gethoursWorked () {
return hoursWorked;
}
public double getearnedPay () {
return hourlyRate * hoursWorked;
}
} // end class Employee
sample output
Welcome to the Payroll Program!
Enter Name of Employee(enter 'stop' to exit program):
Ravi
Enter a positive hourly rate:
450
Enter a positive number of hours worked:
7
The employee Ravi was paid $ 3150.00 this week
Enter Name of Employee(enter 'stop' to exit program):
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.