Question

Create a Java Program that calculates payroll for N number of workers in a company Using...

Create a Java Program that calculates payroll for N number of workers in a company Using Arrays and Methods

Pass Elements and Pass arrays

            Array length = size of the array.

  • Input :- First Name, MI, Last Name, Id, Hours Worked, Rate(1st method )
  • Constants :- State Tax, Federal Tax, Union Fees(2nd Method)

Calculate The following using different methods

  • Gross income(3rd Method)
  • State Tax(4th Method)
  • Federal Tax(5th Method)
  • Union Fees(6th Method)
  • Net Income(7th Method)

Out Put (Formatted)(8th Method)

(FirstName[i] + "\t" + MiddleName[i] + "\t" + LastName[i] +"\t" +
    Rate[i] +"\t" + HoursWorked[i] +"\t"+ Overtime[i]+"\t"+ GrossIncome[i]+"\t"+
     StateTax[i]+"\t"+ FedTax[i]+"\t"+ UnionFees[i]+"\t"+ Net[i]);

  • Send to file
  • Send to Screen
0 0
Add a comment Improve this question Transcribed image text
Answer #1


// Java program to input details of employees of a company and calculate and display their payroll details
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class CompanyPayroll {
  
   // variables to store the tax value
   public static double stateTaxPercent, federalTaxPercent, unionFeePercent;

   // method to input the details of the employees
   public static void inputDetails(Scanner scan, String firstName[], String middleName[], String lastName[], String id[], double rate[], double hoursWorked[])
   {
       for(int i=0;i<firstName.length;i++)
       {
           System.out.println("Employee "+(i+1)+": ");
           System.out.print("First name: ");
           firstName[i] = scan.next();
           System.out.print("Middle name: ");
           middleName[i] = scan.next();
           System.out.print("Last name: ");
           lastName[i] = scan.next();
           System.out.print("ID: ");
           id[i] = scan.next();
           System.out.print("Pay rate: ");
           rate[i] = scan.nextDouble();
           System.out.print("Hours worked: ");
           hoursWorked[i] = scan.nextDouble();
       }
   }
  
   // method to input the tax values (eg- 2% = 0.02 as input)
   public static void inputConstants(Scanner scan)
   {
       System.out.print("Enter federal tax percent: ");
       federalTaxPercent = scan.nextDouble();
       System.out.print("Enter state tax percent: ");
       stateTaxPercent = scan.nextDouble();
       System.out.print("Enter union fees percent: ");
       unionFeePercent = scan.nextDouble();
   }

   //method to calculate gross income and overtime for employees
   public static void calGrossIncome(double rate[], double hoursWorked[], double grossIncome[], double overTime[])
   {
       for(int i=0;i<rate.length;i++)
       {
           if(hoursWorked[i] > 40)
           {
               overTime[i] = (hoursWorked[i]-40)*1.5*rate[i];
               grossIncome[i] = 40*rate[i] + overTime[i];
           }else
           {
               overTime[i] = 0;
               grossIncome[i] = hoursWorked[i]*rate[i] + overTime[i];
           }
       }
   }
  
   // method to calculate the state tax for employees
   public static void calStateTax(double grossIncome[], double stateTax[])
   {
       for(int i=0;i<stateTax.length;i++)
           stateTax[i] = grossIncome[i]*stateTaxPercent;
   }
  
   // method to calculate federal tax for employees
   public static void calFederalTax(double grossIncome[], double federalTax[])
   {
       for(int i=0;i<federalTax.length;i++)
       {
           federalTax[i] = grossIncome[i]*federalTaxPercent;
       }
   }
  
   // method to calculate union fees for employees
   public static void calUnionFees(double grossIncome[], double unionFees[])
   {
       for(int i=0;i<unionFees.length;i++)
           unionFees[i] = grossIncome[i]*unionFeePercent;
   }
  
   // method to calculate net income of employees
   public static void calNetIncome(double grossIncome[], double stateTax[], double federalTax[], double unionFees[], double netIncome[])
   {
       for(int i=0;i<netIncome.length;i++)
           netIncome[i] = grossIncome[i] - stateTax[i] - federalTax[i] - unionFees[i];
   }
  
   // method to output the payroll details of employees on screen and in file
   public static void output(String id[],String firstName[], String middleName[], String lastName[], double rate[],
           double hoursWorked[], double overTime[], double grossIncome[], double stateTax[], double federalTax[], double unionFees[], double netIncome[]) throws FileNotFoundException
   {
       PrintWriter writer = new PrintWriter("companyPayroll.txt");
       System.out.printf("%-20s%-20s%-20s%-20s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s","ID","First Name","Middle Name","Last Name","Pay Rate","Hours Worked",
               "Overtime","Gross Income","State tax","Federal Tax","Union Fees","Net Income");
       writer.printf("%-20s%-20s%-20s%-20s%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s","ID","First Name","Middle Name","Last Name","Pay Rate","Hours Worked",
               "Overtime","Gross Income","State tax","Federal Tax","Union Fees","Net Income");
       for(int i=0;i<firstName.length;i++)
       {
           System.out.printf("\n%-20s%-20s%-20s%-20s%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f",id[i],firstName[i],middleName[i],lastName[i],rate[i],hoursWorked[i],
                   overTime[i],grossIncome[i],stateTax[i],federalTax[i],unionFees[i],netIncome[i]);
           writer.printf("\n%-20s%-20s%-20s%-20s%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f",id[i],firstName[i],middleName[i],lastName[i],rate[i],hoursWorked[i],
                   overTime[i],grossIncome[i],stateTax[i],federalTax[i],unionFees[i],netIncome[i]);
       }
       writer.close();
   }
   public static void main(String[] args) throws FileNotFoundException {

       int n;
       String firstName[], middleName[], lastName[], id[];
       double rate[], hoursWorked[], overtime[], grossIncome[], stateTax[], fedTax[], unionFees[], net[];
       Scanner scan = new Scanner(System.in);
       // input of number of employees
       System.out.print("Enter number of employees: ");
       n = scan.nextInt();
       scan.nextLine();
       if(n> 0)
       {
           firstName = new String[n];
           lastName = new String[n];
           middleName = new String[n];
           id = new String[n];
          
           rate = new double[n];
           hoursWorked = new double[n];
           overtime = new double[n];
           grossIncome = new double[n];
           overtime = new double[n];
           stateTax = new double[n];
           fedTax = new double[n];
           unionFees = new double[n];
           net = new double[n];
          
           inputDetails(scan, firstName, middleName, lastName, id, rate,hoursWorked);
           inputConstants(scan);
           calGrossIncome(rate, hoursWorked, grossIncome, overtime);
           calStateTax(grossIncome, stateTax);
           calFederalTax(grossIncome, fedTax);
           calUnionFees(grossIncome , unionFees);
           calNetIncome(grossIncome, stateTax, fedTax, unionFees, net);
          
           output(id,firstName, middleName,lastName,rate, hoursWorked,overtime, grossIncome,stateTax, fedTax, unionFees, net);
       }else
           System.out.println("Number of employees > 0");
       scan.close();

   }

}
//end of program

Output:

Output file:

Add a comment
Know the answer?
Add Answer to:
Create a Java Program that calculates payroll for N number of workers in a company Using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a c++ program to create a payroll based on these assumptions and requirements: A company...

    Write a c++ program to create a payroll based on these assumptions and requirements: A company called Data Housing Corp. employs a number of employees (say 5) all employees are paid on hourly base, any employee who works more than 40 hours is entitled to be paid overtime (1.5 for every hour exceeding 40). Calculate : 1. The Gross income=(Rate*hours worked )+ overtime 2. The overtime= No. of hours exceeding 40 * Rate *1.5 3. State tax= gross*6% 4.Federal tax=gross*12%...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints...

    Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints filename before the words filename. The starter file will be expecting them in that order. Lab#4's main method is completely written. Do not modify main. Just fill in the methods. Main will load a large arrays of int, and then load a large array of Strings. As usual the read loops for each file will be calling a resize method as needed. Once the...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT