Design a Payroll class with the following fields: • name : a String containing t
ID: 3681080 • Letter: D
Question
Design a Payroll class with the following fields: • name : a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods : • Constructor : takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll class • Mutators: let the user assign values to the fields of the Payroll class • grossPay: returns the employee's gross pay, which is calculated as the number of hours worked times the hourly pay rate. Write another program that demonstrates the class by creating a Payroll object , then asking the user to enter the data for an employee in the order: name , ID number, rate, hours. The program should then print out a statement in the following format (for example, if you had an employee named Chris Jacobsen with ID number 11111, who works for 5 hours at $10/hr): Chris Jacobsen, employee number 11111, made $50.00 in gross pay. Using text forming so that the gross pay is rounded to two decimal places.
Explanation / Answer
Hi, Please find the below classes.
Payroll.java
public class Payroll {
private String name;
private int idNumber;
private double rate;
private int hours;
public Payroll(String name, int idNumber){
this.name = name;
this.idNumber = idNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIdNumber() {
return idNumber;
}
public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public String getGross(){
double grossPay;
grossPay = hours * rate;
return String.format("%.2f", grossPay);
}
}
Main.java
public class Main {
public static void main(String[] args) {
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please Enter Employee Name: ");
String empName = in.next();
System.out.println("Please Enter Employee ID: ");
int empID = in.nextInt();
System.out.println("Please Enter Employee Rate: ");
double rate = in.nextDouble();
System.out.println("Please Enter Employee Working hours: ");
int hours = in.nextInt();
Payroll p1 = new Payroll(empName, empID);
p1.setRate(rate);
p1.setHours(hours);
String grossPay = p1.getGross();
System.out.println("Employee Name : "+p1.getName());
System.out.println("Employee ID : "+p1.getIdNumber());
System.out.println("Employee Rate : "+p1.getRate());
System.out.println("Employee Working hours : "+p1.getHours());
System.out.println("The Employee Gross Pay : "+grossPay);
}
}
Output:
Please Enter Employee Name:
Suresh
Please Enter Employee ID:
1111
Please Enter Employee Rate:
50
Please Enter Employee Working hours:
5
Employee Name : Suresh
Employee ID : 1111
Employee Rate : 50.0
Employee Working hours : 5
The Employee Gross Pay : 250.00