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
![Test Case 1 Failed Show what's missing
Command Line arguments: double_input1.txt
Total: -1,813.080 \n
Average: -18.131\n
Highest: 985.007 \n
Lowest: -989.128 \n
Test Case 2 Failed Show what's missing
Command Line arguments: double_input2.txt
Total: -331, 368.178 \n
Average: -165.684\n
Highest: 9,994.439 \n
Lowest: -9,988.269\n
Test Case 3 Passed!
Command Line arguments: double_input3.txt
File: double_input3.txt does not exist.\n
Test Case 4 Passed!
Usage: java ArrayOperations <filename>\n](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F8c85f2d6-e493-4b5c-b884-672c3e62262d%2F8ec0cbc3-80b7-4f49-b086-47fe4c7c3c7b%2F52athu4_processed.png&w=3840&q=75)
![Test Case 1 Failed Show what's missing
Command Line arguments: double_input1.txt
Total: -1813.080 \n
Average: -18.131\n
Highest: 985.007 \n
Lowest: -989.128 \n
Test Case 2 Failed Show what's missing
Command Line arguments: double_input2.txt
Total: -331368.178|\n|
Average: -165.684 \n
Highest: 9994.439 \n
Lowest: -9988.269|\n
Test Case 3 Passed!
Command Line arguments: double_input3.txt
File: double_input3.txt does not exist.\n
Test Case 4 Passed!
Usage: java ArrayOperations <filename>\n](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F8c85f2d6-e493-4b5c-b884-672c3e62262d%2F8ec0cbc3-80b7-4f49-b086-47fe4c7c3c7b%2Fuudbuih_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 4 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)