Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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. 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 <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);
        }
       
        // Display results by calling helper methods
        System.out.println("Total: " + getTotal(array));
        System.out.println("Average: " + getAverage(array));
        System.out.println("Highest: " + getHighest(array));
        System.out.println("Lowest: " + 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

Test Case 1 Failed Show what's missing
Command Line arguments: double_input1.txt
Total: -1813.080000000001 \n
Average: -18.13080000000001 \n
Highest: 985.007 \n
Lowe... OUTPUT TOO LONG
Test Case 2 Failed Show what's missing
Command Line arguments: double_input2.txt
Total: -331368.17752799974|\n|
Average: -165.68408876399988 \n
Highest: 9994.439255 \n
Lowest... OUTPUT TOO LONG
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
expand button
Transcribed Image Text:Test Case 1 Failed Show what's missing Command Line arguments: double_input1.txt Total: -1813.080000000001 \n Average: -18.13080000000001 \n Highest: 985.007 \n Lowe... OUTPUT TOO LONG Test Case 2 Failed Show what's missing Command Line arguments: double_input2.txt Total: -331368.17752799974|\n| Average: -165.68408876399988 \n Highest: 9994.439255 \n Lowest... OUTPUT TOO LONG 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
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education