In: Computer Science
JAVA
(1) Create two files to submit:
Build the Payroll class with the following specifications:
(2) In main(), create two Payroll objects: one use the info
inputted by the user, and the other use the default constructor,
and print the info for each employee (2 pts)
Ex:
Enter the name, ID, pay rate, and hours worked of an employee: Linda Smith 1234 20.5 35 Employee Records: Name ID Pay Rate Hrs Worked Gross Pay Linda Smith 1234 $20.50/hr 35.0 $717.50 John Doe 9999 $15.00/hr 40.0 $600.00
Payroll.java and PayrollClient.java are created in Payroll Package inside the Payroll Package. I have attached the Structure of this project below:
JAVA CODE:
Payroll.java
package Payroll;
// Payroll Class
public class Payroll {
// private field name of type String
private String name;
// private field ID of type int
private int ID;
// private field payRate of type double
private double payRate;
// private field hrWorked of type double
private double hrWorked;
// Default constructor
public Payroll() {
// String name - Initialized in default constructor to "John Doe"
this.name = "John Doe";
// int ID - Initialized in default constructor to 9999
this.ID = 9999;
// double payRate - Initialized in default constructor to 15.0
this.payRate = 15.0;
// double hrWorked - Initialized in default constructor to 40
this.hrWorked = 40;
}
// A constructor that accepts the employee’s name, ID, and pay rate as arguments.
public Payroll(String name, int ID, double payRate, double hrWorked) {
// The values passed in the argument are assigned to the fields
this.name = name;
this.ID = ID;
this.payRate = payRate;
this.hrWorked = hrWorked;
}
// getName accessor method
public String getName() {
return name;
}
// getID accessor method
public int getID() {
return ID;
}
// getPayRate accessor method
public double getPayRate() {
return payRate;
}
// setPayRate mutator method
public void setPayRate(double payRate) {
this.payRate = payRate;
}
// getHrWorked accessor method
public double getHrWorked() {
return hrWorked;
}
// setHrWorked mutator method
public void setHrWorked(double hrWorked) {
this.hrWorked = hrWorked;
}
// grossPay returns employee’s gross pay
public double grossPay() {
return this.hrWorked * this.payRate;
}
}
PayrollClient.java
package Payroll;
import java.text.DecimalFormat;
import java.util.Scanner;
// PayrollClient Class
public class PayrollClient {
// Has main method
public static void main(String[] args) {
// The decimal number in the print output of gross pay should have two numbers after the decimal point
DecimalFormat df = new DecimalFormat("#.00");
// A Scanner object is created
Scanner sc = new Scanner(System.in);
// Displays prompt
System.out.println("Enter the name, ID, pay rate, and hours worked of an employee:");
// Gets name, ID, pay rate and number of hours worked from the user
String name = sc.nextLine();
int ID = Integer.parseInt(sc.nextLine());
double payRate = Double.parseDouble((sc.nextLine()));
double hrWorked = Double.parseDouble((sc.nextLine()));
// Two objects of Payroll class are created
// p1 is created using user input
Payroll p1 = new Payroll(name, ID, payRate, hrWorked);
// p2 object uses default constructor
Payroll p2 = new Payroll();
// Output values are printed
System.out.println("Employee Records:");
System.out.println("Name ID Pay Rate Hrs Worked Gross Pay");
System.out.println(p1.getName() + " " + p1.getID() + " $" + p1.getPayRate() + "/hr " + p1.getHrWorked()
+ " $" + df.format(p1.grossPay()));
System.out.println(p2.getName() + " " + p2.getID() + " $" + p2.getPayRate() + "/hr " + p2.getHrWorked()
+ " $" + df.format(p1.grossPay()));
}
}
OUTPUT: