import java.util.Scanner; /* Java program to compute an employee's gross and net pay for a week. ** A fixed percentage of gross pay is deducted for taxes. ** There is no consideration given to overtime pay. ** Input is provided via the keyboard (as opposed to being hard-coded, ** as it was in the previous versions of this program). ** ** As in the previous version of this program, here we use separate ** methods for computing gross pay, tax, and net pay. Each of these ** methods assigns the computed value to a global variable. This is not ** the preferred way of doing things, but we resort to it because, at ** this point in the course, we have neither parameter passing nor ** functional methods at our disposal. ** ** Author: R. McCloskey ** Date: Sept. 2016 (modified Sept. 2023) */ public class GrossNetPay2 { static final double TAX_RATE = 0.1825; // symbolic constant static double hoursWorked, hourlyWage; // inputs static double grossPay, netPay; // outputs static double tax; // auxiliary variable public static void main(String[] args) { // Create a Scanner object that can read input from the keyboard. Scanner keyboard = new Scanner(System.in); // For each input value, prompt user and read response System.out.print("Enter employee's hours worked:"); hoursWorked = keyboard.nextDouble(); System.out.print("Enter employee's hourly wage:"); hourlyWage = keyboard.nextDouble(); computeGrossPay(); computeTax(); computeNetPay(); reportResults(); } /* Computes gross pay and assigns the result to the proper global variable. ** Assumes ability to access global variables containing values for hours ** worked and hourly wage. */ private static void computeGrossPay() { grossPay = hoursWorked * hourlyWage; } /* Computes tax and assigns the result to the proper global variable. ** Assumes ability to access global variables containing values for gross ** pay and tax rate. */ private static void computeTax() { tax = grossPay * TAX_RATE; } /* Computes net pay and assigns the result to the proper global variable. ** Assumes ability to access global variables containing values for gross ** pay and tax. */ private static void computeNetPay() { netPay = grossPay - tax; } /* Prints a message reporting the employee's hours worked, hourly wage, ** gross pay, and net pay. Assumes ability to access global variables ** containing the values of these quantities. */ private static void reportResults() { System.out.println(hoursWorked + " hours worked at $" + hourlyWage + " per hour"); System.out.println("yields gross pay of $" + grossPay + "\nand net pay of $" + netPay); } }