import java.util.*; import java.text.*; import java.io.*; public class payroll {
ID: 3625984 • Letter: I
Question
import java.util.*;
import java.text.*;
import java.io.*;
public class payroll
{ public static double dues = 7.85; public static int k = 30;
public static void main(String []args) throws Exception
{ String [] name = new String[30];
Scanner inFile = new Scanner (new File("payIn.txt")); PrintWriter outFile = new PrintWriter("PayOut.txt"); heading(outFile); double [][] data = new double[30][7]; double [] total = new double[5]; int n = input(inFile,name,data); grosspay(data, n); dues(data, n); federaltax(data, n); statetax(data, n); netpay(data, n); Pdetails(outFile,data,name, n); totals(data, total, n); summary(outFile, total, n); outFile.close();
} //********************************************************************** public static int input(Scanner inFile, String [] name, double [][] data) { String line; StringTokenizer st; int i = 0; while (i < name.length && inFile.hasNext()) { line = inFile.nextLine(); st = new StringTokenizer(line); int n = st.countTokens(); name[i] = st.nextToken(); for (int j = 1; j < n -2; j++) name[i] = name[i] + " " + st.nextToken(); data[i][5] = Double.parseDouble(st.nextToken()); data[i][6] = Double.parseDouble(st.nextToken()); i++; } return i;
} //*********************************************************************** public static void grosspay(double[][] data, int n) { double overtime; for(int i = 0; i < n; i++) if(data[i][5] <= 40) data[i][1] = data[i][5] * data[i][6]; else if(data[i][5] <= 50) { overtime = (data[i][5] - 40) * (data[i][6] * 1.5); data[i][1] = (data[i][6] * 40) + overtime; } else { overtime = (data[i][5] - 40) * (data[i][6] * 2); data[i][1] = (data[i][6] * 40) + overtime; } }//************************************************************************
public static void dues(double [][] data, int n) { for(int i = 0; i < n; i++) data[i][4] = dues; } //************************************************************************
public static void federaltax(double [][] data, int n)
{ for(int i = 0; i < n; i++)
data[i][2] = data[i][1] * 0.18;
}//*************************************************************************
public static void statetax(double [][] data, int n)
{ for(int i = 0; i < n; i++)
data[i][3] = data[i][1] * 0.045;
}//*************************************************************************
public static void netpay(double [][] data, int n)
{ for(int i = 0; i < n; i++)
data[i][0] = (data[i][1] - data[i][2] - data[i][3] - data[i][4]);
}//*************************************************************************
public static void totals(double [][]data, double [] total, int n)
{ for(int i = 0; i < n; i++)
for(int j = 0; j < 5; j++)
total[j] += data[i][j];
}//*************************************************************************
public static void heading(PrintWriter outFile)
{ outFile.println(" Natural Pine Furniture Company"); outFile.println(" Payroll Report ");
outFile.print("Name NetPay Gross Pay Federal tax"); outFile.println(" State tax Dues Hours Pay rate"); outFile.println("---------------------------------------------------------------------------------"); }//*************************************************************************
public static void Pdetails(PrintWriter outFile, double [][] data, String [] name, int n)
{ for(int i = 0; i < n; i++) {
outFile.print(rightPad(name[i],17) + leftpad(data[i][0], 10) + leftpad(data[i][1],11));
outFile.print(leftpad(data[i][2], 11) + leftpad(data[i][3],12) + leftpad(data[i][4],10));
outFile.println(leftpad(data[i][5], 8) + leftpad(data[i][6],8)); } }//**************************************************************************
public static void summary(PrintWriter outFile,double [] total, int n)
{ outFile.println("---------------------------------------------------------------------------------"); outFile.print("totals " + leftpad(total[0],20) + leftpad(total[1],11) + leftpad(total[2],11)); outFile.println(leftpad(total[3], 12) + leftpad(total[4], 11)); outFile.println("number of employees processed is: " + n); } //**************************************************************************
public static String rightPad(String name, int width) {
return String.format("%-" + width + "s", name).replace(' ', ' '); }//**************************************************************************
public static String leftpad (double w, int width)
{ String s; // String to be returned int m; // length of s DecimalFormat fmt = new DecimalFormat("0.00"); // convert miles to a String with one decimal place s = fmt.format(w); // determine the length of s m = s.length(); // pad s by spaces on the left so that the resulting length of s is width for (int i = 0; i < width - m; i++) s = " " + s; // one space between the “ “ return s; } //**************************************************************************
public static void sname(int [] A, int k, String [] name, int minI) { String tempName; tempName = name[minI]; name[minI] = name[k]; name[k] = tempName;
}//**************************************************************************
public static void srow(int k, double [][] data, int minI) { double [] tempRow;
tempRow = data[minI]; data[minI] = data[k]; data[k] = tempRow; } }
this is what i have of the output file
Natural Pine Furniture Company Payroll Report Name NetPay Gross Pay Federal tax State tax Dues Hours Pay rate --------------------------------------------------------------------------------- Hancock John 232.40 310.00 55.80 13.95 7.85 40.00 7.75
Light Karen L 209.15 280.00 50.40 12.60 7.85 40.00 7.00
Fagan Bert Todd 217.29 290.50 52.29 13.07 7.85 41.00 7.00
Antrim Forrest N 565.65 740.00 133.20 33.30 7.85 60.00 9.25
Camden Warren 296.24 392.38 70.63 17.66 7.85 36.50 10.75
Mulicka Al B 445.70 585.23 105.34 26.34 7.85 52.33 9.05
Lee Phoebe 479.00 628.19 113.07 28.27 7.85 49.75 11.50
Bright Harry 156.84 212.50 38.25 9.56 7.85 25.00 8.50
Garris Ted 201.21 269.75 48.56 12.14 7.85 41.00 6.50
Benson Martyne 268.05 356.00 64.08 16.02 7.85 43.00 8.00
Lloyd Jeanine D 208.61 279.30 50.27 12.57 7.85 39.90 7.00
Leslie Bennie A 297.69 394.25 70.96 17.74 7.85 41.00 9.50
Brandt Leslie 439.71 577.50 103.95 25.99 7.85 58.50 7.50
Schulman David 233.37 311.25 56.02 14.01 7.85 41.00 7.50
Worthington Dan 279.98 371.39 66.85 16.71 7.85 40.10 9.25
Hall Gus W 804.24 1047.86 188.61 47.15 7.85 80.50 8.66
Prigeon Dale R 400.38 526.75 94.82 23.70 7.85 50.10 8.75
Fitzgibbons Rusty 233.56 311.50 56.07 14.02 7.85 43.00 7.00
Feistner Merle S 340.90 450.00 81.00 20.25 7.85 40.00 11.25
Hallquist Dottie 139.21 189.75 34.16 8.54 7.85 23.00 8.25
Bolton Seth 236.27 315.00 56.70 14.17 7.85 43.33 7.00
Taylor Gregg 268.05 356.00 64.08 16.02 7.85 43.00 8.00
Raskin Rose 319.78 422.75 76.10 19.02 7.85 43.00 9.50
Kenwood Pat 217.74 291.09 52.40 13.10 7.85 40.10 7.25
Slaugher Lew 279.87 371.25 66.82 16.71 7.85 43.33 8.25
--------------------------------------------------------------------------------
totals 7770.88 10280.17 1850.43 462.61 196.25
number of employees processed is: 25
and this is the input file
Hancock John 40.000 7.75
Light Karen L 40.000 7.00
.Fagan Bert Todd 41.000 7.00
Antrim Forrest N 60.000 9.25
Camden Warren 36.500 10.75
Mulicka Al B 52.333 9.05
Lee Phoebe 49.750 11.50
Bright Harry 25.000 8.50
Garris Ted 41.000 6.50
Benson Martyne 43.000 8.00
Lloyd Jeanine D 39.900 7.00
Leslie Bennie A 41.000 9.50
Brandt Leslie 58.500 7.50
Schulman David 41.000 7.50
Worthington Dan 40.100 9.25
Hall Gus W 80.500 8.66
Prigeon Dale R 50.100 8.75
Fitzgibbons Rusty 43.000 7.00
Feistner Merle S 40.000 11.25
Hallquist Dottie 23.000 8.25
Bolton Seth 43.333 7.00
Taylor Gregg 43.000 8.00
Raskin Rose 43.000 9.50
Kenwood Pat 40.100 7.25
Slaugher Lew 43.333 8.25
Explanation / Answer
import java.util.*;
import java.text.*;
import java.io.*;
public class payroll
{
public static double dues = 7.85;
public static int k = 30;
public static void main(String []args) throws Exception
{
String [] name = new String[30];
Scanner inFile = new Scanner (new File("payIn.txt")); PrintWriter outFile = new PrintWriter("PayOut.txt");
heading(outFile); double [][] data = new double[30][7]; double [] total = new double[5]; int n = input(inFile,name,data); grosspay(data, n);
dues(data, n); federaltax(data, n); statetax(data, n); netpaydata,n)); (outFile,data,name, n)
totals(data, total, n);
summary.File(total, n);
outFile.close();
}
public static int input(Scanner inFile, String [] name, double [][] data) { String line;
StringTokenizer st;
int i = 0;
while (i < name.length && inFile.hasNext()) { line = inFile.nextLine(); st = new StringTokenizer(line); int n = st.countTokens(); name[i] = st.nextToken(); for (int j = 1; j < n -2; j++) name[i] = name[i] + " " + st.nextToken();
data[i][5] = Double.parseDouble(st.nextToken()); data[i][6] = Double.parseDouble(st.nextToken()); i++; } return i;
}
public static void grosspay(double[][] data, int n) { double overtime; for(int i = 0; i < n; i++)
if(data[i][5] <= 40) data[i][1] = data[i][5] * data[i][6]; else if(data[i][5] <= 50) { overtime = (data[i][5] - 40) * (data[i][6] * 1.5); data[i][1] = (data[i][6] * 40) + overtime; } else { overtime = (data[i][5] - 40) * (data[i][6] * 2); data[i][1] = (data[i][6] * 40) + overtime; } }
public static void dues(double [][] data, int n) { for(int i = 0; i < n; i++) data[i][4] = dues; }
public static void federaltax(double [][] data, int n)
{ for(int i = 0; i < n; i++)
data[i][2] = data[i][1] * 0.18;
}
public static void statetax(double [][] data, int n)
{ for(int i = 0; i < n; i++)
data[i][3] = data[i][1] * 0.045;
}
public static void netpay(double [][] data, int n)
{ for(int i = 0; i < n; i++)
data[i][0] = (data[i][1] - data[i][2] - data[i][3] - data[i][4]);
}
public static void totals(double [][]data, double [] total, int n)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < 5; j++)
total[j]+= data[i][j];
}
public static void heading(PrintWriter outFile)
{
outFile.println(" Natural Pine Furniture Company"); outFile.println(" Payroll Report ");
outFile.print("Name NetPay Gross Pay Federal tax"); outFile.println(" State tax Dues Hours Pay rate"); outFile.println("---------------------------------------------------------------------------------"); }//*************************************************************************
public static void Pdetails(PrintWriter outFile, double [][] data, String [] name, int n)
{
for(int i = 0; i < n; i++)
{
outFile.print(rightPad(name[i],17) + leftpad(data[i][0], 10) + leftpad(data[i][1],11));
outFile.print(leftpad(data[i][2], 11) + leftpad(data[i][3],12) + leftpad(data[i][4],10));
outFile.println(leftpad(data[i][5], 8) + leftpad(data[i][6],8)); }
public static void summary(PrintWriter outFile,double [] total, int n)
{ outFile.println("--------------------------------------------------------------------------------"); outFile.print("totals " + leftpad(total[0],20) + leftpad(total[1],11) + leftpad(total[2],11)); outFile.println(leftpad(total[3], 12) + leftpad(total[4], 11)); outFile.println("number of employees processed is: " + n); }
public static String rightPad(String name, int width) {
return String.format("%-" + width + "s", name).replace(' ', ' ');
public static String leftpad (double w, int width)
{ String s; // String to be returned int m; // length of s DecimalFormat fmt = new DecimalFormat("0.00"); // convert miles to a String with one decimal place s = fmt.format(w); // determine the length of s m = s.length(); // pad s by spaces on the left so that the resulting length of s is width for (int i = 0; i < width - m; i++) s = " " + s; // one space between the “ “ return s; }
public static void sname(int [] A, int k, String [] name, int minI) { String tempName; tempName = name[minI]; name[minI] = name[k]; name[k] = tempName;
}
public static void srow(int k, double [][] data, int minI) { double [] tempRow;
tempRow = data[minI]; data[minI] = data[k]; data[k] = tempRow; }
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.