By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

Write a program to calculate Payroll of employee

P: 1
The below are the question given; Please help.


(Payroll) Write a program that reads the following information and prints payroll statements in show message dialog box.
These are the inputs:

Employees Full Name (e.g. Mohammed Al Zakwani)
Numbers of Hours worked in a week (e.g. 40 hours)
Hourly Pay Rate (e.g. 6.75)
Federal Tax withholding rate (e.g. 20%)
State Tax withholding rate (e.g. 9%)

You should calculate:

Gross Pay = hourly pay rate * numbers of hours worked in a week.
Federal Tax withholding = Federal Tax withholding rate * gross pay
State Tax withholding = State Tax withholding rate * gross pay
Total Deductions = Federal Tax withholding + State Tax withholding
Net Pay = Gross Pay + Total Deductions

Output should be

Employees Full Name: Mohammed Al Zakwani
Numbers of Hours worked in a week: 40 hours
Hourly Pay Rate: $ 6.75
Gross Pay: $270
Deductions:
Federal Tax withholding (20%): $54
State Tax withholding rate (9%) : $24.3
Total Deductions: $78.3
Net Pay: $191.7

I tried to do as below but i'm getting alot of error;
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. class payroll {
  4. {
  5.    public static void main(String[] args)
  6.    {
  7.       Scanner input = new Scanner(System.in);
  8.  
  9.       System.out.println("Enter employee's name:");
  10.       String employee = input.next();
  11.  
  12.       System.out.println("Enter number of hours worked:");
  13.       double  hours = input.nextDouble();
  14.  
  15.       System.out.println("Enter hourly pay rate:");
  16.       double  pay = input.nextDouble();
  17.  
  18.       double  gross_pay = pay * hours;
  19.  
  20.       System.out.println("Enter federal tax withholding rate:");
  21.       double  fedtax = input.nextDouble();
  22.       double fedtaxr = fedtax * 0.20;
  23.  
  24.       System.out.println("Enter state tax withholding rate:");
  25.       double  statetax = input.nextDouble();
  26.  
  27.       double statetaxr = statetax * 0.20;
  28.  
  29.       double deductions = fedtaxr + statetaxr;
  30.  
  31.       double total_pay = gross_pay - deductions;
  32.  
  33.       System.out.println("Employee name: " + employee);
  34.  
  35.       System.out.println("Hours worked: " + hours);
  36.  
  37.       System.out.println(" Enter payrate: " + pay);
  38.  
  39.       System.out.println(" Enter gross pay: " + gross_pay);
  40.  
  41.       System.out.println(" Deductions: ");
  42.       System.out.println("\t Federdal Withholding (20.0%): " + fedtaxr);
  43.       System.out.println("\t State Withholding (9.0%)" + statetaxr);
  44.       System.out.println("\t Total deductions:" + deductions);
  45.       System.out.println("Total pay: " + total_pay);
  46.    }
  47. }
Apr 17 '10 #1
Share this Question
Share on Google+
7 Replies


Dheeraj Joshi
Expert 100+
P: 1,123
What errors you are getting?

Regards
Dheeraj Joshi
Apr 19 '10 #2

P: 2
Here's the program in Java.....

Expand|Select|Wrap|Line Numbers
  1. // @Copyright: Swaran Bindra
  2.  
  3.  
  4. public class PayrollCalc 
  5. {
  6.     private String name;
  7.     private double hoursWorked;
  8.     private double hourlyPayRate;
  9.     private static final double FEDERAL_TAX_WITHHOLDING_RATE = 0.20;
  10.     private static final double STATE_TAX_WITHHOLDING_RATE = 0.09;
  11.  
  12.  
  13.     public PayrollCalc()
  14.     {
  15.  
  16.     }
  17.  
  18.     public PayrollCalc(String n, double hw, double hpr)
  19.     {
  20.         this.name = n;
  21.         this.hoursWorked = hw;
  22.         this.hourlyPayRate = hpr;
  23.     }
  24.  
  25.     public double grossPay(double hourlyRate, double hrsWorked)
  26.     {
  27.         double grossPay = (hourlyRate * hrsWorked);
  28.         return grossPay;
  29.     }
  30.  
  31.     public double federalTax(double gross)
  32.     {
  33.         return FEDERAL_TAX_WITHHOLDING_RATE * gross;
  34.     }
  35.  
  36.     public double stateTax(double gross)
  37.     {
  38.         return STATE_TAX_WITHHOLDING_RATE * gross;
  39.     }
  40.  
  41.  
  42.     public String getName() {
  43.         return name;
  44.     }
  45.  
  46.  
  47.     public void setName(String name) {
  48.         this.name = name;
  49.     }
  50.  
  51.  
  52.     public double getHoursWorked() {
  53.         return hoursWorked;
  54.     }
  55.  
  56.  
  57.     public void setHoursWorked(double hoursWorked) {
  58.         this.hoursWorked = hoursWorked;
  59.     }
  60.  
  61.  
  62.     public double getHourlyPayRate() {
  63.         return hourlyPayRate;
  64.     }
  65.  
  66.  
  67.     public void setHourlyPayRate(double hourlyPayRate) {
  68.         this.hourlyPayRate = hourlyPayRate;
  69.     }
  70.  
  71. }
  72.  
  73. // @Copyright: Swaran Bindra
  74.  
  75. import java.util.*;
  76.  
  77. public class PayrollDriver 
  78. {
  79.     public static void main(String args[])
  80.     {
  81.         Scanner input =  new Scanner(System.in);
  82.         PayrollCalc data = new PayrollCalc();
  83.  
  84.         System.out.println("Enter your name: ");
  85.         data.setName(input.next());
  86.  
  87.         System.out.println("Number of Hours Worked: ");
  88.         data.setHoursWorked(Double.parseDouble(input.next()));
  89.  
  90.         System.out.println("Hourly Pay Rate: ");
  91.         data.setHourlyPayRate(Double.parseDouble(input.next()));
  92.  
  93.         double grossAmount = data.grossPay(data.getHourlyPayRate(), data.getHoursWorked());
  94.  
  95.         System.out.println();
  96.         System.out.println("------------------------------------");
  97.         System.out.println("Name: " + data.getName());
  98.         System.out.println("Hours Worked: " + data.getHoursWorked() + "hrs");
  99.         System.out.println("Pay Rate: $" + data.getHourlyPayRate());
  100.         System.out.println("Gross Pay: $" + grossAmount + '\n');
  101.         System.out.println("DEDUCTIONS");
  102.         System.out.println("Federal Tax Withholding (20%): $" + data.federalTax(grossAmount));
  103.         System.out.println("State Tax Withholding (9%): $" + data.stateTax(grossAmount));
  104.         System.out.println("Total Deductions: $" + (data.federalTax(grossAmount) + data.stateTax(grossAmount)));
  105.         System.out.println("Net Pay: $" + (grossAmount - (data.federalTax(grossAmount) + data.stateTax(grossAmount))));
  106.  
  107.  
  108.         input.close();
  109.     }
  110.  
  111. }
  112.  
Output:
Name: swaran
Hours Worked: 40.0 hrs
Pay Rate: $6.75
Gross Pay: $270.0

DEDUCTIONS
Federal Tax Withholding (20%): $54.0
State Tax Withholding (9%): $24.3
Total Deductions: $78.3
Net Pay: $191.7



Regards
Swaran
Aug 11 '15 #3

Expert 100+
P: 785
Hey Swaran, can you please tell us what errors you are getting?
Just copy the messages and list them here.
Aug 12 '15 #4

P: 2
Sorry, this program is in response to a previous query by habsy
Aug 12 '15 #5

Expert 100+
P: 785
Swaran, I think there is a misunderstanding.
Listing the program is fine, but we also need the exact error message. You may think, we can run the program of our own on our system and see the error message? Nope. Your system can behave completely different dependent on hardware and software.
Also it is some piece of work for us to run your program. On the other side it should cost you only a few seconds to copy the message that you see on your screen into this forum post. Please do not put an unnecessary burden on us like running the program in order to help you. Keep in mind that we are not getting paid by helping you. It's just a favor we do for you.
Aug 13 '15 #6

Rabbit
Expert Mod 10K+
P: 12,365
@chaarmann, I believe their post is supposed to be the answer to the thread, not a question.

@swaran, Please use code tags when posting code or formatted data.
Aug 13 '15 #7

Expert 100+
P: 785
Hi Swaran.
if that is true what Rabbit believes (why didn't you tell me ?), then I like to say "Thank you" to you for posting the solution, so that others with the same problems can benefit.
Aug 14 '15 #8

Post your reply

Sign in to post your reply or Sign up for a free account.