java programming I have modified this program based on what I\'ve gotten from ch
ID: 3796264 • Letter: J
Question
java programming I have modified this program based on what I've gotten from chegg already and I am trying to get it to match the teachers instructions precisely. These are the instructions and this is what I have so far and it keeps giving me errors.
Java programming 5th edition chapter 9 Parallel Arrays
Write a Java program to create 4 parallel arrays - one to hold employee names, one to hold gross pays, one to hold deduction amounts, and one to hold net pays. Read the names into the array from the keyboard, read the gross pay data from a file, and assign the deduction amounts when you create the array. Use the data below.
Perform the following functions using the arrays, each in a separate method.
* Calculate the net pay array amounts by subtracting the deduction amount from the gross pay
* Write one method to calculate the sum of the values in any one array and call it 3 times, once to total the gross pay amounts, once to total the deduction amounts, and once to total the net pay amounts
*Determine the employee with the highest net pay.
* Determine the employee with the lowest net pay.
Print the arrays in column form with table headings and the totals below each column( See below for format). Print the name and net pay for the employees with the highest and lowest net pay. Do all output in main().
Name Gross Pay Deductions Net Pay
______________________________________________
John Brown $2000.00 $525.00 $1475.00
Jim Smith $3750.25 $1025.00 $2725.25
Sue Fuller $1115.50 $300.00 $815.50
Bob Allen $2348.50 $730.25 $1618.25
Leslie Brown $1800.00 $675.75 $1124.25
______________________________________________
Total $11014.25 $3256.00 $7758.25
The employee with the highest net pay is Jim Smith with $2725.25
The employee with the lowest net pay is Sue Fuller with $815.50
import java.util.*;
import java.io.*;
public class PayRoll
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[]args)throws FileNotFoundException
{
//declare variables
int highNet = 0;
int lowNet = 0;
double totGross, totDeduction, totNet;
//Parallel Arrays
String[] names = new String[5];
double[] gross = new double[5];
double[] deduct = {525.00, 1025.00, 300.00, 730.25, 675.75};
double[] net = new double[5];
//Opening file to get gross pay
Scanner fileReader = new Scanner(new File("grossdata.txt"));
//Read the names into the array from the keyboard
System.out.println("Enter employee names:");
for(int i = 0; i < names.length; i = i + 1)
{
System.out.print(" Enter a name : ");
names[i] = keyboard.nextLine();
System.out.println();
}//end for
//Read the gross pay data from a file
for(int i = 0; i < gross.length; i++)
{
//Fetching gross pay
gross[i] = fileReader.nextDouble();
}//end for
//output in Table format
System.out.println("Name Gross Pay Deductions Net Pay");
System.out.println("__________________________________________________");
for(int i = 0; i < names.length; i++)
{
System.out.println(names[i] + " $" + gross[i] + " $" + deduct[i] + " $" + net[i]);
}//end for
System.out.println("_________________________________________________");
totGross = calcSum(gross);
totDeduction = calcSum(deduct);
totNet = calcSum(net);
System.out.println("Total " + totGross + " " + totDeduction + " " + totNet);
System.out.println("The employee with the highest net pay is " + names[highNet] + " with $ " + net[highNet]);
System.out.println("The employee with the lowest net pay is " + names[lowNet] + " with $ " + net[lowNet]);
}//end main
//Calculate the net pay array amounts by subtracting the deduction amount from the gross pay
public static void calcNetPay(double[] netList)
{
for(int i = 0; i < netList.length; i++)
{
netList[i] = gross[i] - deduct[i];
}//end for
}//end calcNetPay
//Write one method to calculate the sum of the values in any one array
public static double calcSum(double[] sumArray)
{
int total = 0;
for(int i = 0; i < sumArray.length; i++)
total += sumArray[i];
return total;
}//end calcSum
//Determine the employees with the highest net pay
public static void getHighest(double[] netList)
{
double highest = 0;
double highestIndex = 0;
highest = netList[0];
for( int i = 1; i < netList.length; i++)
{
if(highest < netList[i])
{
highest = netList[i];
highestIndex = i;
}//end if
}//end for
}//end getHighest
//Determine the employees with the lowest net pay
public static void getLowest(double[] netList)
{
double lowest = 0;
double lowestIndex = 0;
lowest = netList[0];
for(int i = 1; i < netList.length; i++)
{
if(lowest > netList[i])
{
lowest = netList[i];
lowestIndex = i;
}//end if
}//end for
}//end getLowest
}//end class
Explanation / Answer
Hi, I have fixed all the issue.
Now please check and let me know in case of any issue.
import java.util.*;
import java.io.*;
public class PayRoll
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[]args)throws FileNotFoundException
{
//declare variables
int highNet = 0;
int lowNet = 0;
double totGross, totDeduction, totNet;
//Parallel Arrays
String[] names = new String[5];
double[] gross = new double[5];
double[] deduct = {525.00, 1025.00, 300.00, 730.25, 675.75};
double[] net = new double[5];
//Opening file to get gross pay
Scanner fileReader = new Scanner(new File("grossdata.txt"));
//Read the names into the array from the keyboard
System.out.println("Enter employee names:");
for(int i = 0; i < names.length; i = i + 1)
{
System.out.print(" Enter a name : ");
names[i] = keyboard.nextLine();
System.out.println();
}//end for
//Read the gross pay data from a file
for(int i = 0; i < gross.length; i++)
{
//Fetching gross pay
gross[i] = fileReader.nextDouble();
}//end for
// calculating net pay
calcNetPay(net, gross, deduct);
//output in Table format
System.out.println("Name Gross Pay Deductions Net Pay");
System.out.println("__________________________________________________");
for(int i = 0; i < names.length; i++)
{
System.out.println(names[i] + " $" + gross[i] + " $" + deduct[i] + " $" + net[i]);
}//end for
System.out.println("_________________________________________________");
totGross = calcSum(gross);
totDeduction = calcSum(deduct);
totNet = calcSum(net);
System.out.println("Total $" + totGross + " $" + totDeduction + " $" + totNet);
// getting the index of higest net pay and lowest ne t pay
highNet = getHighest(net);
lowNet = getLowest(net);
System.out.println("The employee with the highest net pay is " + names[highNet] + " with $" + net[highNet]);
System.out.println("The employee with the lowest net pay is " + names[lowNet] + " with $" + net[lowNet]);
}//end main
//Calculate the net pay array amounts by subtracting the deduction amount from the gross pay
public static void calcNetPay(double[] netList, double[] gross, double[] deduct)
{
for(int i = 0; i < netList.length; i++)
{
netList[i] = gross[i] - deduct[i];
}//end for
}//end calcNetPay
//Write one method to calculate the sum of the values in any one array
public static double calcSum(double[] sumArray)
{
int total = 0;
for(int i = 0; i < sumArray.length; i++)
total += sumArray[i];
return total;
}//end calcSum
//Determine the employees with the highest net pay
public static int getHighest(double[] netList)
{
double highest = netList[0];
int highestIndex = 0;
for( int i = 1; i < netList.length; i++)
{
if(highest < netList[i])
{
highest = netList[i];
highestIndex = i;
}//end if
}//end for
return highestIndex;
}//end getHighest
//Determine the employees with the lowest net pay
public static int getLowest(double[] netList)
{
double lowest = netList[0];
int lowestIndex = 0;
for(int i = 1; i < netList.length; i++)
{
if(lowest > netList[i])
{
lowest = netList[i];
lowestIndex = i;
}//end if
}//end for
return lowestIndex;
}//end getLowest
}//end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.