Question: Purpose Calculate payroll data using classes write complete program under one class Upload Screenshot of output. and write a complete program under one class. Java
Purpose Calculate payroll data using classes write complete program under one class
Upload Screenshot of output. and write a complete program under one class.
Java Topics Classes, arrays of classes, processing partial arrays, loops.
Files Used These files are in Flip_Files.zip and should be in the same folder as your program:
Employee.java Employee data tracked
EmployeeParameters.java Options used in the program
EmployeeParameters.txt The parameters for EmployeeParameters
Specification Except for the Toolkit and constants noted below, do NOT use static (global) variables. Also, all output to the output file should be echoed on the console.
Overview
Read this programs parameters of the maximum number of employees, savings rate, IRA rate, federal tax rate and state tax rate. A class, EmployeeParameters, and a method in that class are provided to read the parameter file named EmployeeParameters.txt.
Access the Employee class to see what data values will be stored.
Create an array of the Employee class of maximum number of employees
Read the input file into the Employee array. The input data is, in this order, the hours worked, the pay rate and the employee name
Calculate the gross pay for each employee based on a table.
Calculate the remaining values in the Employee class. Note that you can add variables to the Employee class if it will help write the program more efficiently and/or more easily.
Now that all the data has been calculated, print a heading and the detail lines per the spec below.
Calculate the totals of all columns, except the pay rate, which you will average.
Sort the employees alphabetically (using a provided method) and print the details the same way you did in step G above
Print the totals as in step H
Sort the employees by increasing gross pay ((using a provided method) and print the details the same way you did in step G above
Print the totals as in step H
Details follow for the main program
1. Read the Employee parameters. Access the EmployeeParameters class with, say
EmployeeParameters params = new EmployeeParameters();
The parameter class is as follows:
public class EmployeeParameters {
public int maxEmployees; // Maximum number of employees
public double savingsRate; // Savings rate as a percent
public double iraRate // IRA investment rate as a percent
public federalWithholdingRate; // Federal withholding tax rate as a percent
public stateWithholding; // State withholding tax rate as a percent
// Constructor and other methods follow. View the available methods in the class.
} // End class
Use the method in this class, getEmployeeParameters, to read the program parameters. Note that the variables in this class are declared public, not private, so you can access them from the main program. For example, after calling getEmployeeParameters, you can assign to a local variable the maximum number of employees with
maxEmployees = params.maxEmployees;
2. Use an array of a class to hold employee data. The array might be partially filled. The separate class is named Employee (in file Employee.java) and has this definition:
public class Employee {
String name; // Name of the employee
public double hoursWorked; // Hours worked in the payroll period
public double payRate; // Hourly pay rate
public double grossPay; // Gross pay based on the number of hours worked
public double adjustedGrossPay; // Gross pay less amount that goes into the IRA
public double netPay; // Gross pay less taxes
public double savingsAmount; // Amount of gross pay that goes to savings
public double iraAmount; // Amount of gross pay that goes into the IRA
public double taxAmount; // Amount of tax based on gross pay and tax rates
} // End class
You can add fields to the Employee class if it helps writing your program. In the main method, you will define an array of the above class as:
Employee[ ] empl = new Employee[maxEmployees];
where maxEmployees is a number read from the Employee Parameter file as described above in step 1. Note that this allocates space for the array but does not create instances for each employee.
Using the index i as an example, an instance of Employee can be created with a statement like:
empl[i] = new Employee();
Using the index i as an example, an instance variable can be accessed in a method with a statement like:
empl[i].netPay = empl[i].grossPay empl[i].taxAmount;
The main program should consist mostly of method calls. Use appropriate parameter passing between methods and no non-local (static) variables.
3. Data is read from a text file named YourName_S_10_Input.txt and writes to a text file named YourName_S_10_Output.txt. The input file has the employee data. Each line of employee data contains the number of hours worked, the pay rate (both doubles), followed by the name. One or more spaces separate the fields.
4. Gross pay is calculated in a separate method as follows:
hours ? 40.0 Paid at the pay rate.
40.0 < hours ? 50.0 hours > 40.0 and less ? 50.0 are paid at one and a half times the rate. The first 40 hours are paid at the pay rate.
50.0 < hours hours > 50.0 are paid at twice the pay rate. The first 40 hours are paid at the pay rate. Hours 40 to 50 are paid at time and a half.
Examples: with a pay rate is $10.00 an hour, gross pay is calculated as follows:
# Hours Gross Pay
30 30 x $10 = $300
40 40 x $10 = $400
46 40 x $10 + ( (46-40) x $10 x 1.5 ) = $400 + (6 x $15) = $490
50 40 x $10 + ( (50-40) x $10 x 1.5 ) = $400 + (10 x $15) = $400 + $150 = $550
58 40 x $10 + ( (50-40) x $10 x 1.5) + ( (58-50) x $10 x 2) = $400 + $150 + $160
= $710
Note how the pay rate ($10 in the example above) can be factored out, which you should do in the program. For example, the reimbursement for 46 hours is
$10 x (40 + (46-40) x 1.5) = $10 x (40 + (6 x 1.5)) = $10 x (40 + 9) = $490
Do you see why?
5. Use a method to calculate the variables for each instance of an entry in the Employee array, empl:
a. IRA amount = gross pay * IRA investment rate / 100
b. Adjusted gross pay = gross pay - IRA amount
c. Taxes = adjusted gross pay * (the sum of the tax rates as decimals)
d. Net pay = adjusted gross pay taxes
e. Savings amount = net pay * savings rate / 100
6. A method to create a report -- this method will be called after all input and all calculations are done and in turn calls three methods as follows:
a. A method to print heading information as shown in the sample output below. The information includes the report title, column headings for the name, gross pay, net pay, wealth accumulation, taxes for that employee, hours worked, and pay rate, in that order. Also shown here are the first few lines of the data in input file order and the respective totals and average that you can use to ensure you are getting the correct results. Heres how the report should look (partial list):
In Input Order
Mobile Apps Galore, Inc. - Payroll Report
Name Gross Pay Net Pay Wealth Taxes Hours Pay Rate
------------------ --------- ------- ------ --------- ----- --------
Hancock John 487.21 347.38 73.71 100.85 41.00 11.74
Light Karen L 583.00 415.68 88.21 120.68 50.00 10.60
Fagan Bert Todd 637.20 454.32 96.41 131.90 52.00 10.80
Antrim Forrest N 966.96 689.44 146.30 200.16 62.00 12.24
Camden Warren 566.02 403.57 85.64 117.17 38.40 14.74
Mulicka Al B 559.80 399.14 84.70 115.88 44.33 12.04
etc.
Totals: 14,729.71 10,502.28 2,228.60 3,049.05 1,103.14
Average: 12.36
This heading method has a parameter that indicates the print order of the data.
b. A method to print the above detail lines for all employees, matching the headings indicated in part a. Single space the detail lines. The wealth column has the sum of the savings amount and the IRA amount.
c. A method that calculates the totals for gross pay, net pay, wealth, taxes and hours, and the average(not sum) pay rate. Print a summary line with those totals and the average. (The summary line should print the totals at the bottom of the appropriate columns.) Also, print a line with a message that contains the number of employees processed which could be less than 30. You can store the totals in an array or in a separate class.
7. After the report has been printed, sort the employees alphabetically and print out the report again with the same print methods: heading, details and summary. The sort used is located in Toolkit and is called selectionSortArrayOfClass which is discussed at the end of this document and will be reviewed in class. Use the print report method described above to print the data in this order.
8. Next, sort the employees in ascending order by gross pay and print out the header, details and summary again. Use the same selectionSortArrayOfClass for the sort. Use the print report method described above to print the data in this order.
9. The Data. The input data file format is the number of hours (double), the pay rate (double) and a name (which may or may not have an initial). One or more spaces separate the fields. The file has been uploaded and is named CS1050_Assignment_10_Input.txt. Remember to trim the name once youve extracted it from the input file. The String method name is trim(). Use this data:
41.00 11.74 Hancock John
50.00 10.60 Light Karen L
52.00 10.80 Fagan Bert Todd
62.00 12.24 Antrim Forrest N
38.40 14.74 Camden Warren
44.33 12.04 Mulicka Al B
41.75 13.40 Lee Phoebe
24.00 11.40 Bright Harry
41.00 10.40 Garris Ted
43.00 12.00 Benson Martyne
31.90 12.40 Lloyd Jeanine D
44.00 13.50 Leslie Bennie A
48.40 14.40 Brandt Leslie
42.00 12.90 Schulman David
50.10 10.84 Worthington Dan
70.40 12.66 Hall Gus W
40.10 12.74 Prigeon Dale R
43.00 12.44 Fitzgibbons Rusty
50.00 12.24 Feistner Merle S
23.00 12.34 Hallquist Dottie
43.33 10.90 Bolton Seth
43.00 12.20 Taylor Gregg
42.00 12.94 Raskin Rose
50.10 12.44 Kenwood Pat
44.33 14.64 Slaughter Lew
__________________________________________________________________________________________________
Employee class and selection sort-
// Employee class - data values for each employee
import java.util.Scanner;
import java.io.*;
public class Employee {
public String name; // Name of the employee
public double hoursWorked; // Hours worked in the payroll period
public double payRate; // Hourly pay rate
public double grossPay; // Gross pay based on the number of hours worked
public double adjustedGrossPay; // Gross pay less amount for the IRA
public double netPay; // Gross pay less taxes
public double savingsAmount; // Amount of gross pay for savings
public double iraAmount; // Amount of gross pay for the IRA
public double taxAmount; // Amount of tax based on gross pay and tax rates
public double wealth; // Savings amount + iraAmount
// *******************************************************************
// *******************************************************************
/**
selectionSortArrayOfClass
Sort an array of the Employee class in ascending order.
The parameters are:
@param empl - an array of the Employee class. If the array is empty,
the method simply returns with no action taken
@param len - the number of possibly partially-filled elements in the
Employee array (i.e., it might be < empl.length)
@param sortType - a string that indicates the type of sort:
"Name" - sort by name
"Gross Pay" - sort by gross pay
@return 0, sort was completed successfully
1, the array to sort has no elements
2, the sortType is invalid
*/
public static int selectionSortArrayOfClass(
Employee[] empl,
int len,
String sortType) {
final String SORT_BY_NAME = "NAME";
final String SORT_BY_GROSS = "GROSS PAY";
Employee tempEmpl = new Employee(); // Holds an entry being swapped
boolean isSortByName; // True if sort by name
boolean isSortByGross; // True if sort by gross pay
int k, i; // Indexes into the array to assist with the sort
int minIndex; // Index of the minimum value in the array
String howSort; // Upper case of the sort type
// Begin execution. If the employee array has no entries, there is nothing
// to sort, so simply return
if (empl.length == 0) {
return 1;
} // End if
// Set the flag for the entire sort based on whether we are sorting by
// a name (sortType = "Name") or we are sorting by the gross pay
// (sortType = "Gross Pay"). If the the sortType is not one of the two
// possibilities, the method returns with no action taken.
howSort = sortType.toUpperCase().trim(); // Deal only with upper case
isSortByName = (howSort.equals(SORT_BY_NAME)); // True means sort by name
isSortByGross = (howSort.equals(SORT_BY_GROSS)); // True means sort by gross
// If neither of the sort types is true, the user used an unrecognized sort
// type, so just return
if (!(isSortByName || isSortByGross)) {
return 2;
} // End if
// At this point, the empl array has at least one entry, and we are
// sorting by name or by gross pay.
// Each pass determines the location, minIndex, of the smallest value
for (k = 0; k < len - 1; k++)
{
// Find the location, minIndex, of the smallest value in row k
minIndex = k; // Assume the minimum value is at location k
// We check once for each pass of control variable k to see whether
// we are sorting by name or gross pay. This approach is in contrast
// to one inner for loop (control variable i) in which we ask each
// time through the loop what type of sort we are doing.
// This approach saves a whole bunch of times we ask what type
// of sort we're doing at the expense of a slightly larger program.
if (isSortByName) {
for (i = k + 1; i < len; i++) {
if (empl[i].name.compareTo(empl[minIndex].name) < 0) minIndex = i;
} // End for (i = k + 1; i < len; i++)
} // End 'if' sorting by name
else { // We are sorting by gross pay
for (i = k + 1; i < len; i++) {
if (empl[i].grossPay < empl[minIndex].grossPay) minIndex = i;
} // End for (i = k + 1; i < len; i++)
} // End 'else' sorting by gross pay
// Swap elements in the minIndex and k positions of the arrays
tempEmpl = empl[minIndex];
empl[minIndex] = empl[k];
empl[k] = tempEmpl;
} // End for (k = 0; k < len - 1; k++)
return 0; // Indicate the sort ended OK
} // End selectionSortArrayOfClass
// **************************************************************************
} // End Employee
___________________________________________________________________
employee paramaters class
// Define the class with the payroll program parameters
import java.util.Scanner; // To read the parameter file
import java.io.*; // To acces File operations
public class EmployeeParameters {
static private String EMPLOYEE_PARAMETERS = "EmployeeParameters.txt";
// Name of the parameter file
static private String NL = " "; // New line for println
static private String PERCENT = "%"; // Phrase to display percent
public int maxEmployees; // Maximum number of employees
public double savingsRate; // Savings rate, %
public double iraRate; // IRA investment rate, %
public double federalWithholdingRate; // Federal withholding tax rate, %
public double stateWithholdingRate; // State withholding tax rate, %
//**************************************************************************
// Default constructor - Set all private variables to 0
public EmployeeParameters() {
maxEmployees = 0;
savingsRate = 0.0;
iraRate = 0.0;
federalWithholdingRate = 0.0;
stateWithholdingRate = 0.0;
} // End default constructor
//**************************************************************************
// getEmployeeParameters - read the parameter file and load the employee
// parameters from the parameter file. The order of the varaiables is the
// same order the numbers appear in the parameter file. See the comments
// in the private declarations above for a description of each variable.
public void getEmployeeParameters() throws IOException {
File parameterFile = new File(EMPLOYEE_PARAMETERS);
Scanner parameter = new Scanner(parameterFile);
maxEmployees = parameter.nextInt();
savingsRate = parameter.nextDouble();
iraRate = parameter.nextDouble();
federalWithholdingRate = parameter.nextDouble();
stateWithholdingRate = parameter.nextDouble();
} // End getEmployeeParameters
//**************************************************************************
// displayEmployeeParameters - display the parameters on the console.
public void displayEmployeeParameters() {
System.out.println(
NL + "Maximum # of employees: " + maxEmployees +
NL + "Savings rate: " + savingsRate + PERCENT +
NL + "IRA investment rate: " + iraRate + PERCENT +
NL + "Federal withholding rate: " + federalWithholdingRate + PERCENT +
NL + "State withholding rate: " + stateWithholdingRate + PERCENT);
} // End displayParameters
} // End class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
