The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.25.
Question: Write a program that reads the following information and prints a payroll statement:
Employee’s name (e.g., Smith)
Number of hours worked in a week (e.g., 10)
Hourly pay rate (e.g., 6.75)
Federal tax withholding rate (e.g., 20%)
State tax withholding rate (e.g., 9%)
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.25 - Financial application: payroll<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class Ex02_25 {<br /><br /> public static void main(String[] args) {<br /> <br /> Scanner input = new Scanner (System.in);<br /> <br /> //collect employee data<br /> System.out.print("Enter employee's name: ");<br /> String name = input.nextLine();<br /> System.out.print("Enter number of hours worked in a week: ");<br /> int hours = input.nextInt();<br /> System.out.print("Enter hourly pay rate: ");<br /> double rate = input.nextDouble();<br /> System.out.print("Enter federal tax withholding rate e.g 20 for 20%: ");<br /> int fedRate = input.nextInt();<br /> System.out.print("Enter state tax withholding rate e.g 10 for 10%: ");<br /> int stateRate = input.nextInt();<br /> <br /> //calculate employee gross pay and net pay<br /> double grossPay = hours * rate;<br /> double fedWithHolding = ((fedRate/100.0) * grossPay);<br /> double stateWithHolding = ((stateRate/100.0) * grossPay);<br /> double totalDeduction = fedWithHolding + stateWithHolding;<br /> double netPay = grossPay - totalDeduction;<br /> <br /> //format netPay to two decimal places<br /> netPay = (int)(netPay * 100)/100.0;<br /> <br /> //print payroll<br /> System.out.println("nnEmployee Name: " + name);<br /> System.out.println("Hours Worked: " + hours);<br /> System.out.println("Pay Rate: $" + rate);<br /> System.out.println("Gross Pay: $" + grossPay);<br /> System.out.println("Deductions: ");<br /> System.out.println(" Federal Withholding (" + (double)fedRate + "%): $" + fedWithHolding);<br /> System.out.println(" State Withholding (" + (double)stateRate + "%): $" + stateWithHolding);<br /> System.out.println(" Total Deduction: $" + totalDeduction);<br /> System.out.println("Net Pay: $" + netPay);<br /> }<br />}<br /> |
Program Output |
Click here to see other solutions to Introduction to Java Programming.