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

How to average gross/net pay of employees

P: 2
i'm writing a java payroll program for class. I am at the end of the program but I can not figure out how to average the gross/net pay of all employees entered. I know that I must create an array but can not figure out how to get the averages......here is a copy of my code.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Payroll 
  4. {
  5.  
  6.     private static Object employeeName;
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         Scanner input = new Scanner ( System.in );
  11.         double hourlyRate=0;  // Calculated hourly pay rate for the employee
  12.         double hoursWorked =0; // Calculated hours worked for the week
  13.         double grossPay = 0; // Calculated weekly pay before taxes
  14.         double netPay; // Calculated weekly pay after taxes
  15.         double overTime = 0; // Calculated hours over 40 at rate of hourly plus half
  16.         double taxRate = .10; // Calculated tax rate for all employees
  17.         double healthCare = .05;  // Calculated rate for employees with healthcare insurance
  18.         int employeesProcessed = 0; // Number of employees entered by user
  19.         double healthCareOption; // Input from User
  20.         double healthCareCost; // the cost of health care for the employee
  21.         int totalNumberEmployees; // the total number of employees entered by user
  22.         double[] averageGrossPay= new double [employeesProcessed]; // average gross pay for all users entered in the program
  23.         double[] averageNetPay= new double[employeesProcessed]; // average net pay of all users entered in the program
  24.         double[] totalGrossPay= new double[employeesProcessed]; // total gross pay of all employees entered
  25.         double[] totalNetPay= new double [employeesProcessed]; // total net pay of all employees entered
  26.         double sum;
  27.  
  28.         System.out.println ("How many employees will be processed: "); // User inputs # of employees that will be entered
  29.         employeesProcessed = input.nextInt();
  30.  
  31.         for (int count = 1; count <= employeesProcessed; count ++) // loop will continue until the number of employees that the user entered is complete
  32.         {
  33. [**snipped**]            
  34.         grossPay = (hourlyRate * hoursWorked + overTime);
  35.         System.out.printf("%s's total gross pay this week is: $%.2f\n", employeeName, grossPay); // program calculates gross pay
  36.         System.out.println();
  37.  
  38. [**snipped**]
  39.       } // end loop
  40.  
  41.         for (int i=0; i<totalNumberEmployees; i++)
  42.  
  43.         totalNumberEmployees= employeesProcessed;
  44.         System.out.printf("Number of employees processed this week is: $%.2f\n", employeesProcessed); //print out of the number of employees entered in system
  45.         System.out.println();
  46.  
  47.         averageGrossPay= totalGrossPay/totalNumberEmployees;
  48.         System.out.printf ("The average gross pay for all employees entered is: $%.2f\n", averageGrossPay);
  49.         System.out.println();
  50.  
  51.         averageNetPay= totalNetPay/totalNumberEmployees; 
  52.         System.out.println("The average net pay for all employees entered is: " + formatter.format(averageNetPay/totalNumberEmployees));
  53.         System.out.println();

I am stuck after end loop.
Apr 9 '13 #1
Share this Question
Share on Google+
3 Replies


P: 2
I am writing a payroll program for class. It is pretty much done with the exception of averaging the gross/net pay of employees entered. I know an array must be created and I have declared the variables. Just trying to find out how I would average the net/gross pay of employees entered into the program. Number of employees entered depends on user. Here is what I have so far but its not going well

Expand|Select|Wrap|Line Numbers
  1. totalNumberEmployees= employeesProcessed;
  2.         System.out.printf("Number of employees processed this week is: $%.2f\n", employeesProcessed); //print out of the number of employees entered in system
  3.         System.out.println();
  4.  
  5.         averageGrossPay= totalGrossPay/totalNumberEmployees;
  6.         System.out.printf ("The average gross pay for all employees entered is: $%.2f\n", averageGrossPay);
  7.         System.out.println();
  8.  
  9.         averageNetPay= totalNetPay/totalNumberEmployees; 
  10.         System.out.println("The average net pay for all employees entered is: " + formatter.format(averageNetPay/totalNumberEmployees));
  11.         System.out.println();
Apr 9 '13 #2

10K+
P: 13,264
You need a loop running the the array or list of employees adding together their salary at each step.
Apr 9 '13 #3

10K+
P: 13,264
1.) Use code tags when posting code
2.) Do not start multiple threads for the same question.
3.) Revise your notes on classes and use an object oriented approach by creating a class called Employee that holds all the Employee values.
4.) Don't write everything in the main method. Split out the functionality into separate methods that do simple and well defined tasks
Apr 9 '13 #4

Post your reply

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