The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.27.
Question: Rewrite Exercise 2.25 using GUI input and output dialog boxes.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.27 - Financial application: payroll<br /> *<br /> */ <br /><br />import javax.swing.JOptionPane;<br /><br />public class Ex02_27 {<br /><br /> public static void main(String[] args) {<br /> <br /> //collect employee data using GUI input dialog box<br /> String name = JOptionPane.showInputDialog(null, "Enter employee's name: ",<br /> "Input", JOptionPane.QUESTION_MESSAGE);<br /> String sHours = JOptionPane.showInputDialog(null,<br /> "Enter number of hours worked in a week: ", "Input",<br /> JOptionPane.QUESTION_MESSAGE);<br /> int hours = Integer.parseInt(sHours);<br /> String sRate = JOptionPane.showInputDialog(null, "Enter hourly pay rate: ",<br /> "Input", JOptionPane.QUESTION_MESSAGE);<br /> double rate = Double.parseDouble(sRate);<br /> String sFedRate = JOptionPane.showInputDialog(null,<br /> "Enter federal tax withholding rate e.g 20 for 20%: ", "Input",<br /> JOptionPane.QUESTION_MESSAGE);<br /> int fedRate = Integer.parseInt(sFedRate);<br /> String sStateRate = JOptionPane.showInputDialog(null,<br /> "Enter state tax withholding rate e.g 10 for 10%: ", "Input",<br /> JOptionPane.QUESTION_MESSAGE);<br /> int stateRate = Integer.parseInt(sStateRate);<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 using GUI output dialog box<br /> String output = ("nnEmployee Name: " + name + "nHours Worked: " + hours<br /> + "nPay Rate: $" + rate + "nGross Pay: $" + grossPay + "nDeductions: "<br /> + "n Federal Withholding (" + (double) fedRate + "%): $" + fedWithHolding<br /> + "n State Withholding (" + (double) stateRate + "%): $" + stateWithHolding<br /> + "n Total Deduction: $" + totalDeduction + "nNet Pay: $" + netPay);<br /><br /> JOptionPane.showMessageDialog(null, output, "Employee Payroll Info", 1);<br /> }<br />}<br /> |
Program output |
Click here to see other solutions to Introduction to Java Programming.