JAVA PROGRAM

Systems Architecture
7th Edition
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Stephen D. Burd
Chapter8: Data And Network Communication Technology
Section: Chapter Questions
Problem 41VE
icon
Related questions
Question
JAVA PROGRAM
 
PLEASE MODIFY THIS PROGRAM SO WHEN I UPLOAD IT TO HYPERGRADE IT PASSES ALL TEST CASES. BECAUSE IT DOES NOT PASS ALL THE TEXT CASES. IT ONLY PASSES 8 PUT OF 16 TEXT CASES. ALL LENGHTS, HEIGHS AND ARRAY ELEMENTS ARE IN THREE DIGITS AFTER THE DEICIMAL POINT. ALSO, WHEN YOU TYPE BADFILE.TXT IT SHOULD SAY AFER THAT Please enter the file name AGAIN or type QUIT to exit:\n THEN YOU TYPE IN THE FILE NAME.  I PROVIDED SCREENSHOTS OF THE FAILED TEST CASES.AND THE INPUTS. 
 

import java.io.*;
import java.util.*;

public class ArrayListOperations {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
       
        while(true) {
            System.out.print("Please enter the file name or type QUIT to exit:\n");
            String filename = scanner.nextLine();
           
            if(filename.equalsIgnoreCase("QUIT")) {
                break;
            }
           
            File file = new File(filename);
            if(!file.exists()) {
                System.out.println("File: " + filename + " does not exist.");
                continue;
            }

            ArrayList<ArrayList<Double>> data = loadFromFile(filename);
            for(int i = 0; i < data.size(); i++) {
                System.out.printf("Row %d Length: %d, Subtotal: %.3f\n", i, data.get(i).size(), getRowSubtotal(data, i));
            }
            for(int j = 0; j < getMaxColumnLength(data); j++) {
                System.out.printf("Column %d Height: %d, Subtotal: %.3f\n", j, getColumnHeight(data, j), getColSubtotal(data, j));
            }
            System.out.printf("Array Elements: %d, Total: %.3f\n", getTotalElements(data), getTotal(data));
            break;
        }
    }

    public static ArrayList<ArrayList<Double>> loadFromFile(String filename) {
        ArrayList<ArrayList<Double>> data = new ArrayList<>();
        try {
            Scanner fileScanner = new Scanner(new File(filename));
            while(fileScanner.hasNextLine()) {
                String[] parts = fileScanner.nextLine().split(" ");
                ArrayList<Double> row = new ArrayList<>();
                for(String part : parts) {
                    row.add(Double.parseDouble(part));
                }
                data.add(row);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public static double getRowSubtotal(ArrayList<ArrayList<Double>> data, int rowIndex) {
        double sum = 0.0;
        for(double d : data.get(rowIndex)) {
            sum += d;
        }
        return sum;
    }

    public static double getColSubtotal(ArrayList<ArrayList<Double>> data, int colIndex) {
        double sum = 0.0;
        for(ArrayList<Double> row : data) {
            if(row.size() > colIndex) {
                sum += row.get(colIndex);
            }
        }
        return sum;
    }

    public static double getTotal(ArrayList<ArrayList<Double>> data) {
        double sum = 0.0;
        for(ArrayList<Double> row : data) {
            for(double d : row) {
                sum += d;
            }
        }
        return sum;
    }

    public static int getMaxColumnLength(ArrayList<ArrayList<Double>> data) {
        int max = 0;
        for(ArrayList<Double> row : data) {
            if(row.size() > max) {
                max = row.size();
            }
        }
        return max;
    }

    public static int getColumnHeight(ArrayList<ArrayList<Double>> data, int colIndex) {
        int count = 0;
        for(ArrayList<Double> row : data) {
            if(row.size() > colIndex) {
                count++;
            }
        }
        return count;
    }

    public static int getTotalElements(ArrayList<ArrayList<Double>> data) {
        int count = 0;
        for(ArrayList<Double> row : data) {
            count += row.size();
        }
        return count;
    }
}

Test Case 1

Please enter the file name or type QUIT to exit:\n
quitENTER
 

Test Case 2

Please enter the file name or type QUIT to exit:\n
QUITENTER

 

 

Test Case 5

 
Please enter the file name or type QUIT to exit:\n
badfile.txtENTER
File: badfile.txt does not exist.\n
Please enter the file name again or type QUIT to exit:\n
quitENTER
 

Test Case 6

Please enter the file name or type QUIT to exit:\n
badfile.txtENTER
File: badfile.txt does not exist.\n
Please enter the file name again or type QUIT to exit:\n
QUITENTER

Test Case 11

 
 Please enter the file name or type QUIT to exit:\n
badfile.txtENTER
File: badfile.txt does not exist.\n
Please enter the file name again or type QUIT to exit:\n
badfile2.txtENTER
File: badfile2.txt does not exist.\n
Please enter the file name again or type QUIT to exit:\n
input3.txtENTER
Row 0 Length: 6, Subtotal: 2025.100\n
Row 1 Length: 6, Subtotal: 3604.000\n
Row 2 Length: 6, Subtotal: 1628.660\n
Column 0 Height: 3, Subtotal: 1568.770\n
Column 1 Height: 3, Subtotal: 1801.420\n
Column 2 Height: 3, Subtotal: 258.690\n
Column 3 Height: 3, Subtotal: 1568.770\n
Column 4 Height: 3, Subtotal: 1801.420\n
Column 5 Height: 3, Subtotal: 258.690\n
Array Elements: 18, Total: 7257.760\n
 
Input1.txt
1 2 3
4 5 6
7 8 9
Input2.txt
555.66 333.44 123.45
778.88 900.00 123.12
234.23 567.98 12.12
Input3.txt
555.66 333.44 123.45 555.66 333.44 123.45
778.88 900.00 123.12 778.88 900.00 123.12
234.23 567.98 12.12 234.23 567.98 12.12
Input4.txt
555.66 333.44 123.45
778.88 900.00 123.12
234.23 567.98 12.12
555.66 333.44 123.45
778.88 900.00 123.12
234.23 567.98 12.12
555.66 333.44 123.45
778.88 900.00 123.12
234.23 567.98 12.12
Input5.txt
555.66 333.44 123.45 555.66 333.44 123.45
778.88 900.00 123.12 778.88 900.00
234.23 567.98 12.12 234.23
Input6.txt
555.66 333.44 123.45 555.66
778.88 900.00 123.12 778.88 900.00
234.23 567.98 12.12 234.23 567.98 12.12
Input 7.txt
555.66 333.44 123.45 755.23
778.88 900.00 123.12
234.23 567.98
555.66
778.88 900.00 123.12 999.99
234.23 567.98 12.12
555.66 333.44
778.88
234.23 567.98 12.12 987.65 432.11
820.17
Input8.txt
55.66
778.88 900.00
234.23 567.98 333.44
555.66 123.45 755.23 888.88
778.88 900.00 123.12 999.99 777.77
778.88 900.00 123.12 999.99 777.77 666.66
778.88 900.00 123.12 999.99 777.77 555.55 444.44
778.88 900.00 123.12 999.99 777.77 333.33 222.22 111.11
Transcribed Image Text:Input1.txt 1 2 3 4 5 6 7 8 9 Input2.txt 555.66 333.44 123.45 778.88 900.00 123.12 234.23 567.98 12.12 Input3.txt 555.66 333.44 123.45 555.66 333.44 123.45 778.88 900.00 123.12 778.88 900.00 123.12 234.23 567.98 12.12 234.23 567.98 12.12 Input4.txt 555.66 333.44 123.45 778.88 900.00 123.12 234.23 567.98 12.12 555.66 333.44 123.45 778.88 900.00 123.12 234.23 567.98 12.12 555.66 333.44 123.45 778.88 900.00 123.12 234.23 567.98 12.12 Input5.txt 555.66 333.44 123.45 555.66 333.44 123.45 778.88 900.00 123.12 778.88 900.00 234.23 567.98 12.12 234.23 Input6.txt 555.66 333.44 123.45 555.66 778.88 900.00 123.12 778.88 900.00 234.23 567.98 12.12 234.23 567.98 12.12 Input 7.txt 555.66 333.44 123.45 755.23 778.88 900.00 123.12 234.23 567.98 555.66 778.88 900.00 123.12 999.99 234.23 567.98 12.12 555.66 333.44 778.88 234.23 567.98 12.12 987.65 432.11 820.17 Input8.txt 55.66 778.88 900.00 234.23 567.98 333.44 555.66 123.45 755.23 888.88 778.88 900.00 123.12 999.99 777.77 778.88 900.00 123.12 999.99 777.77 666.66 778.88 900.00 123.12 999.99 777.77 555.55 444.44 778.88 900.00 123.12 999.99 777.77 333.33 222.22 111.11
Test Case 5 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
badfile.txt ENTER
File: badfile.txt does not exist.\n
Please enter the file name or type QUIT to exit: \n
quit ENTER
Test Case 6
Failed
Please enter the file name or type QUIT to exit: \n
badfile.txt ENTER
Show what's missing
File: badfile.txt does not exist.\n
Please enter the file name or type QUIT to exit: \n
QUIT ENTER
Test Case 7 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
badfile.txt ENTER
File: badfile.txt does not exist.\n
Please enter the file name or type QUIT to exit: \n
QuIt ENTER
Test Case 10 Failed
Show what's missing D
Please enter the file name or type QUIT to exit: \n
badfile.txt ENTER
Test Case 11
File: badfile.txt does not exist.\n
Please enter the file name or type QUIT to exit: \n
input2.txt ENTER
Row 8 Length: 3, Subtotal: 1012.550 \n
Row 1 Length: 3, Subtotal: 1802.000 \n
Row 2 Length: 3, Subtotal: 814.330\n
Column
Height: 3, Subtotal: 1568.770 \n
Column 1 Height: 3, Subtotal: 1801.420 \n
Column 2 Height: 3, Subtotal: 258.690 \n
Array Elements: 9, Total: 3628.880 \n
Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
badfile.txt ENTER
File: badfile.txt does not exist.\n
Please enter the file name or type QUIT to exit: \n
badfile2.txt ENTER
File: badfile2.txt does not exist. \n
Please enter the file name or type QUIT to exit: \n
input3.txt ENTER
Row 8 Length: 6, Subtotal: 2025.100 \n
Row 1 Length: 6, Subtotal: 3604.000 \n
Row 2 Length: 6, Subtotal: 1628.660\n
Column
Height: 3, Subtotal: 1568.770 \n
Column 1 Height: 3, Subtotal: 1801.420 \n
Column 2 Height: 3, Subtotal: 258.690 \n
Column 3 Height: 3, Subtotal: 1568.770 \n
Column 4 Height: 3, Subtotal: 1801.420|\n]
Column 5 Height: 3, Subtotal: 258.690 \n
Array Elements: 18, Total: 7257.760 \n
Test Case 5 Failed
Show what's missing
Please enter the file name or type QUIT to exit: \n]
badfile.txt ENTER
File: badfile.txt does not exist.\n
Please enter the file name again or type QUIT to exit: \n
quit ENTER
Test Case 6 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
badfile.txt ENTER
File: badfile.txt does not exist.\n
Please enter the file name again or type QUIT to exit: \n
QUIT ENTER
Test Case 7 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
badfile.txt ENTER
File: badfile.txt does not exist.\n
Please enter the file name again or type QUIT to exit: \n
QuIt ENTER
Test Case 10 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
badfile.txt ENTER
File: badfile.txt does not exist.\n
Please enter the file name again or type QUIT to exit: \n
input2.txt ENTER
RowLength: 3, Subtotal: 1012.550\n
Row 1 Length: 3, Subtotal: 1802.000 \n
Row 2 Length: 3, Subtotal: 814.330 \n
Column Height: 3, Subtotal: 1568.770 \n
Column 1 Height: 3, Subtotal: 1801.420 \n
Column 2 Height: 3, Subtotal: 258.690\n
Array Elements: 9, Total: 3628.880 \n
Test Case 11 Failed Show what's missing
Please enter the file name or type QUIT to exit: \n
badfile.txt ENTER
File: badfile.txt does not exist.\n
Please enter the file name again or type QUIT to exit: \n
badfile2.txt ENTER
File: badfile2.txt does not exist.\n
Please enter the file name again or type QUIT to exit: \n
input3.txt ENTER
Row 0 Length: 6, Subtotal: 2025.100 \n
Row 1 Length: 6, Subtotal: 3604.000 \n
Row 2 Length: 6, Subtotal: 1628.660 \n
Column Height: 3, Subtotal: 1568.770 \n
Column 1 Height: 3, Subtotal: 1801.420 \n
Column 2 Height: 3, Subtotal: 258.690\n
Column 3 Height: 3, Subtotal: 1568.770 \n
Column 4 Height: 3, Subtotal: 1801.420 \n
Column 5 Height: 3, Subtotal: 258.690\n
Array Elements: 18, Total: 7257.760\n
Transcribed Image Text:Test Case 5 Failed Show what's missing Please enter the file name or type QUIT to exit: \n badfile.txt ENTER File: badfile.txt does not exist.\n Please enter the file name or type QUIT to exit: \n quit ENTER Test Case 6 Failed Please enter the file name or type QUIT to exit: \n badfile.txt ENTER Show what's missing File: badfile.txt does not exist.\n Please enter the file name or type QUIT to exit: \n QUIT ENTER Test Case 7 Failed Show what's missing Please enter the file name or type QUIT to exit: \n badfile.txt ENTER File: badfile.txt does not exist.\n Please enter the file name or type QUIT to exit: \n QuIt ENTER Test Case 10 Failed Show what's missing D Please enter the file name or type QUIT to exit: \n badfile.txt ENTER Test Case 11 File: badfile.txt does not exist.\n Please enter the file name or type QUIT to exit: \n input2.txt ENTER Row 8 Length: 3, Subtotal: 1012.550 \n Row 1 Length: 3, Subtotal: 1802.000 \n Row 2 Length: 3, Subtotal: 814.330\n Column Height: 3, Subtotal: 1568.770 \n Column 1 Height: 3, Subtotal: 1801.420 \n Column 2 Height: 3, Subtotal: 258.690 \n Array Elements: 9, Total: 3628.880 \n Failed Show what's missing Please enter the file name or type QUIT to exit: \n badfile.txt ENTER File: badfile.txt does not exist.\n Please enter the file name or type QUIT to exit: \n badfile2.txt ENTER File: badfile2.txt does not exist. \n Please enter the file name or type QUIT to exit: \n input3.txt ENTER Row 8 Length: 6, Subtotal: 2025.100 \n Row 1 Length: 6, Subtotal: 3604.000 \n Row 2 Length: 6, Subtotal: 1628.660\n Column Height: 3, Subtotal: 1568.770 \n Column 1 Height: 3, Subtotal: 1801.420 \n Column 2 Height: 3, Subtotal: 258.690 \n Column 3 Height: 3, Subtotal: 1568.770 \n Column 4 Height: 3, Subtotal: 1801.420|\n] Column 5 Height: 3, Subtotal: 258.690 \n Array Elements: 18, Total: 7257.760 \n Test Case 5 Failed Show what's missing Please enter the file name or type QUIT to exit: \n] badfile.txt ENTER File: badfile.txt does not exist.\n Please enter the file name again or type QUIT to exit: \n quit ENTER Test Case 6 Failed Show what's missing Please enter the file name or type QUIT to exit: \n badfile.txt ENTER File: badfile.txt does not exist.\n Please enter the file name again or type QUIT to exit: \n QUIT ENTER Test Case 7 Failed Show what's missing Please enter the file name or type QUIT to exit: \n badfile.txt ENTER File: badfile.txt does not exist.\n Please enter the file name again or type QUIT to exit: \n QuIt ENTER Test Case 10 Failed Show what's missing Please enter the file name or type QUIT to exit: \n badfile.txt ENTER File: badfile.txt does not exist.\n Please enter the file name again or type QUIT to exit: \n input2.txt ENTER RowLength: 3, Subtotal: 1012.550\n Row 1 Length: 3, Subtotal: 1802.000 \n Row 2 Length: 3, Subtotal: 814.330 \n Column Height: 3, Subtotal: 1568.770 \n Column 1 Height: 3, Subtotal: 1801.420 \n Column 2 Height: 3, Subtotal: 258.690\n Array Elements: 9, Total: 3628.880 \n Test Case 11 Failed Show what's missing Please enter the file name or type QUIT to exit: \n badfile.txt ENTER File: badfile.txt does not exist.\n Please enter the file name again or type QUIT to exit: \n badfile2.txt ENTER File: badfile2.txt does not exist.\n Please enter the file name again or type QUIT to exit: \n input3.txt ENTER Row 0 Length: 6, Subtotal: 2025.100 \n Row 1 Length: 6, Subtotal: 3604.000 \n Row 2 Length: 6, Subtotal: 1628.660 \n Column Height: 3, Subtotal: 1568.770 \n Column 1 Height: 3, Subtotal: 1801.420 \n Column 2 Height: 3, Subtotal: 258.690\n Column 3 Height: 3, Subtotal: 1568.770 \n Column 4 Height: 3, Subtotal: 1801.420 \n Column 5 Height: 3, Subtotal: 258.690\n Array Elements: 18, Total: 7257.760\n
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Introduction to computer system
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.
Recommended textbooks for you
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning