// File: Paymaster2.java // Version 2 of the PayCheck class shows the two-alternative "if" statement. import javax.swing.JOptionPane; /** * Computes gross pay for an hourly worker, given hours worked, and rate of pay. */ class PayCheck2 { //instance var's private String name; // employee name private double hoursWorked; // total hours worked for the week private double payRate; // hourly rate of pay private double regHours; // hours worked up to MAX_REGULAR private double overHours; // overtime hours worked private double regPay; // pay for "regular" hours worked private double overPay; // pay for overtime hours worked private double grossPay; // computed total pay private static final int MAX_REGULAR = 40; // hours worked in excess of this are overtime /** * Create a PayCheck object. * @param name the employee name * @param hoursWorked the number of hours worked * @param payRate the hourly rate of pay */ public PayCheck2(String name, double hoursWorked, double payRate) { this.name = name; this.payRate = payRate; this.hoursWorked = hoursWorked; // call method to compute gross pay this.computeGrossPay(); } /** * Computes the gross pay */ public void computeGrossPay() { if (hoursWorked <= MAX_REGULAR) // no overtime... { regHours = hoursWorked; // ...all hours are regular regPay = regHours * payRate; } else // there was overtime... { regHours = MAX_REGULAR; // ...the maximum regular hours // overtime hours are all hours in excess of the threshold overHours = hoursWorked - MAX_REGULAR; regPay = regHours * payRate; // pay "time and a half" the rate for the overtime hours worked overPay = overHours * (payRate * 1.5); } // in either case, gross pay is sum of regular + overtime pay grossPay = regPay + overPay; } /** * Returns the gross pay * @return the gross pay */ public double getGross() { return grossPay; } /** * Returns a String containing all the employee data. * @return all data for an employee */ public String getData() { String out = "Employee: " + name + "\nTotal hours worked: " + hoursWorked + String.format("%nHourly rate: $%.2f",payRate) + "\n\nRegular hours worked: " + regHours + String.format("%nRegular pay: $%.2f",regPay) + "\n\nOvertime hours worked: " + overHours + String.format("%nOvertime pay: $%.2f",overPay) ; return out ; } } public class PayMaster2 { public static void main(String[] args) { // get input data from user String name = JOptionPane.showInputDialog("Enter employee name:") ; String input = JOptionPane.showInputDialog("Enter hours worked:") ; double hours = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter hourly rate of pay:") ; double rate = Double.parseDouble(input) ; // create object using data entered PayCheck2 johnny = new PayCheck2(name, hours, rate) ; // print pay check info System.out.println(johnny.getData()); // print gross pay System.out.printf("Gross pay: $%.2f%n",johnny.getGross()) ; System.exit(0) ; } } /* sample output from 3 runs (no overtime, overtime, and "boundary value") Employee: Carmine Notyourz Total hours worked: 30.0 Hourly rate: $20.00 Regular hours worked: 30.0 Regular pay: $600.00 Overtime hours worked: 0.0 Overtime pay: $0.00 Gross pay: $600.00 Employee: Marge Inoverra Total hours worked: 50.0 Hourly rate: $20.00 Regular hours worked: 40.0 Regular pay: $800.00 Overtime hours worked: 10.0 Overtime pay: $300.00 Gross pay: $1100.00 Employee: Paddy O'Furniture Total hours worked: 40.0 Hourly rate: $15.00 Regular hours worked: 40.0 Regular pay: $600.00 Overtime hours worked: 0.0 Overtime pay: $0.00 Gross pay: $600.00 */