Wednesday, October 5, 2016

// // Leave a Comment

Java Payroll Application

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%)
/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 2.25 - Financial application: payroll
 *
 */ 

import java.util.Scanner;

public class Ex02_25 {

    public static void main(String[] args) {
        
        Scanner input = new Scanner (System.in);
        
        //collect employee data
        System.out.print("Enter employee's name: ");
        String name = input.nextLine();
        System.out.print("Enter number of hours worked in a week: ");
        int hours = input.nextInt();
        System.out.print("Enter hourly pay rate: ");
        double rate = input.nextDouble();
        System.out.print("Enter federal tax withholding rate e.g 20 for 20%: ");
        int fedRate = input.nextInt();
        System.out.print("Enter state tax withholding rate e.g 10 for 10%: ");
        int stateRate = input.nextInt();
        
        //calculate employee gross pay and net pay
        double grossPay = hours * rate;
        double fedWithHolding = ((fedRate/100.0) * grossPay);
        double stateWithHolding = ((stateRate/100.0) * grossPay);
        double totalDeduction = fedWithHolding + stateWithHolding;
        double netPay = grossPay - totalDeduction;
        
        //format netPay to two decimal places
        netPay = (int)(netPay * 100)/100.0;
        
        //print payroll
        System.out.println("\n\nEmployee Name: " + name);
        System.out.println("Hours Worked: " + hours);
        System.out.println("Pay Rate: $" + rate);
        System.out.println("Gross Pay: $" + grossPay);
        System.out.println("Deductions: ");
        System.out.println("  Federal Withholding (" + (double)fedRate + "%): $" + fedWithHolding);
        System.out.println("  State Withholding (" + (double)stateRate + "%): $" + stateWithHolding);
        System.out.println("  Total Deduction: $" + totalDeduction);
        System.out.println("Net Pay: $" + netPay);
    }
}

Exercise 2.25 - Java Payroll Application
Program Output
Click here to see other solutions to Introduction to Java Programming.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.