JAVA PROGRAM Lab #2. Chapter 7. PC #11. Array Operations (Page 491) Write a program that accepts a file name from command line, then initializes an array with test data using that text file as an input. The file should contain floating point numbers (use double data type). The program should also have the following methods: * getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array. * getAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array. * getHighest. This method should accept a one-dimensional array as its argument and return the highest value in the array. * getLowest. This method should accept a one-dimensional array as its argument and return the lowest value in the array. PLEASE MOFDIFY THIS PROGRAM SO THERE ARE ONLY THREE DIGITS AFTER THE DECIMAL POINT FOR LOWEST, HIGHTEST, AVERAGE AND TOTAL. ALSO ADD A COMMA FOR THE RESULT FOR LOWEST, HIGHTEST, AVERAGE AND TOTAL AND ADD A COMA FOR THE RESULT ALSO. BECAUSE WHEN I UPLOAD IT TO HYPERGRADE IT DOES PASS ALL THE TEST CASES. IT ONLY PASSES 2 OUT OF 4 TEST CASES. IT HAS TO PASS ALL THE TEST CASES. I HAVE PROVIDED PROOF OF THE FAILED TEST CASES. THANK YOU. import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class ArrayOperations { // Method to calculate the total of array elements public static double getTotal(double[] array) { double total = 0; for (double value : array) { total += value; } return total; } // Method to calculate the average of array elements public static double getAverage(double[] array) { return getTotal(array) / array.length; } // Method to find the highest value in the array public static double getHighest(double[] array) { double highest = array[0]; for (double value : array) { if (value > highest) { highest = value; } } return highest; } // Method to find the lowest value in the array public static double getLowest(double[] array) { double lowest = array[0]; for (double value : array) { if (value < lowest) { lowest = value; } } return lowest; } // Main function public static void main(String[] args) throws IOException { // Check the number of command-line arguments if (args.length != 1) { System.out.println("Usage: java ArrayOperations "); return; } // Check if file exists File file = new File(args[0]); if (!file.exists()) { System.out.println("File: " + args[0] + " does not exist."); return; } // Initialize Scanner to read the file Scanner fileReader = new Scanner(file); ArrayList arrayList = new ArrayList<>(); // Read file and populate ArrayList while (fileReader.hasNextDouble()) { arrayList.add(fileReader.nextDouble()); } // Close the file reader fileReader.close(); // Convert ArrayList to array double[] array = new double[arrayList.size()]; for (int i = 0; i < arrayList.size(); i++) { array[i] = arrayList.get(i); } // Format and display results by calling helper methods System.out.printf("Total: %.2\n", getTotal(array)); System.out.printf("Average: %.2f\n", getAverage(array)); System.out.printf("Highest: %.2f\n", getHighest(array)); System.out.printf("Lowest: %.2f\n", getLowest(array)); } } Input1.txt -283.760 -456.167 19.815 -322.301 344.949 -850.533 -672.360 -188.767 646.462 -118.775 808.613 -746.865 -370.432 219.607 -166.298 -508.636 -989.128 -205.020 -928.165 -180.699 300.753 316.036 709.371 -886.860 -585.388 -554.302 -394.801 -970.233 905.941 787.854 -181.240 74.665 802.453 -951.292 -510.656 -203.999 -276.199 -350.575 398.501 -519.799 469.199 592.120 713.424 155.967 585.481 -780.846 387.700 457.806 560.933 -343.916 486.806 -43.184 237.494 191.488 309.275 -64.415 -206.333 -377.696 -409.553 -734.282 -777.221 -318.800 -695.745 40.631 -384.036 -937.134 -380.501 -77.083 756.524 -720.959 -579.412 -215.301 542.097 -402.541 468.721 151.050 573.296 -342.210 758.038 200.691 882.294 8.042 -87.760 634.811 -777.953 767.795 570.716 594.012 596.648 900.010 -370.754 985.007 785.562 -838.952 -71.182 72.845 -500.759 698.946 527.167 1.082
JAVA PROGRAM
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayOperations {
// Method to calculate the total of array elements
public static double getTotal(double[] array) {
double total = 0;
for (double value : array) {
total += value;
}
return total;
}
// Method to calculate the average of array elements
public static double getAverage(double[] array) {
return getTotal(array) / array.length;
}
// Method to find the highest value in the array
public static double getHighest(double[] array) {
double highest = array[0];
for (double value : array) {
if (value > highest) {
highest = value;
}
}
return highest;
}
// Method to find the lowest value in the array
public static double getLowest(double[] array) {
double lowest = array[0];
for (double value : array) {
if (value < lowest) {
lowest = value;
}
}
return lowest;
}
// Main function
public static void main(String[] args) throws IOException {
// Check the number of command-line arguments
if (args.length != 1) {
System.out.println("Usage: java ArrayOperations <filename>");
return;
}
// Check if file exists
File file = new File(args[0]);
if (!file.exists()) {
System.out.println("File: " + args[0] + " does not exist.");
return;
}
// Initialize Scanner to read the file
Scanner fileReader = new Scanner(file);
ArrayList<Double> arrayList = new ArrayList<>();
// Read file and populate ArrayList
while (fileReader.hasNextDouble()) {
arrayList.add(fileReader.nextDouble());
}
// Close the file reader
fileReader.close();
// Convert ArrayList to array
double[] array = new double[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++) {
array[i] = arrayList.get(i);
}
// Format and display results by calling helper methods
System.out.printf("Total: %.2\n", getTotal(array));
System.out.printf("Average: %.2f\n", getAverage(array));
System.out.printf("Highest: %.2f\n", getHighest(array));
System.out.printf("Lowest: %.2f\n", getLowest(array));
}
}
-283.760
-456.167
19.815
-322.301
344.949
-850.533
-672.360
-188.767
646.462
-118.775
808.613
-746.865
-370.432
219.607
-166.298
-508.636
-989.128
-205.020
-928.165
-180.699
300.753
316.036
709.371
-886.860
-585.388
-554.302
-394.801
-970.233
905.941
787.854
-181.240
74.665
802.453
-951.292
-510.656
-203.999
-276.199
-350.575
398.501
-519.799
469.199
592.120
713.424
155.967
585.481
-780.846
387.700
457.806
560.933
-343.916
486.806
-43.184
237.494
191.488
309.275
-64.415
-206.333
-377.696
-409.553
-734.282
-777.221
-318.800
-695.745
40.631
-384.036
-937.134
-380.501
-77.083
756.524
-720.959
-579.412
-215.301
542.097
-402.541
468.721
151.050
573.296
-342.210
758.038
200.691
882.294
8.042
-87.760
634.811
-777.953
767.795
570.716
594.012
596.648
900.010
-370.754
985.007
785.562
-838.952
-71.182
72.845
-500.759
698.946
527.167
1.082
Step by step
Solved in 4 steps with 4 images