Question
Chapter 7. PC #16. 2D Array Operations JAVA ECLIPSE Write a program that creates a two-dimensional array initialized with test data. Use any primitive data
Chapter 7. PC #16. 2D Array Operations
JAVA ECLIPSE
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods:
getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array.
getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array.
getRowTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the total of the values in the specified row.
getColumnTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a column in the array. The method should return the total of the values in the specified column.
getHighestInRow. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the
subscript of a row in the array. The method should return the highest value in the specified row of the array.
getLowestInRow. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the lowest value in the specified row of the array.
___________________________________________________________________________________
I have an incomplete code looks like this:
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ArrayOpperation { public static void main(String[] args) throws FileNotFoundException { String fileName = args[0]; File file = new File(filename); if(file.exists()) { double [][] array = getArray(file); for(int row = 0; row < array.length;row++) System.out.printf("Row %d total: %f ", row, getRowTotal(array, row)); for(int col = 0; col < array[0].length; col++); System.out.printf("Coulumn %d total: %f ", col, getColumnTotal(array, col)); }else{ System.out.printf("file %'s does not exist. , filename"); } } public static double[][] getArray(File file) throws FileNotFoundException { int rows = 0; int columns = 0; Scanner inputFile = new Scanner(file); while (inputFile.hasNext()) { String line = inputFile.nextLine(); if(rows == 0) { Scanner row = new Scanner(line); while (row.hasNext()) { row.nextDouble(); columns++; } row.close(); } rows++; } inputFile.close(); double [][] result = new double [rows][columns]; inputFile = new Scanner(file); for(int row = 0; row < rows; row++) { for(int col = 0; col < columns; col++) { result[row][col] = inputFile.nextDouble(); } } inputFile.close(); return result; } public static double getTotal(double [][] array) { } public static double getAverage(double [][] array) { } public static double getRowTotal(double [][] array) { } public static double getcolumnTotal(double [][] array) { } public static double getHighestInRow(double [][] array) { } public static double getLowestInRow(double [][] array) { }
____________________________________________________________________________________
I need it to be able to produce these output.
Test Case 1
Command Line Arguments | Files in the same directory |
---|---|
input1.txt | input1.txt input2.txt input3.txt input4.txt |
Row 0 total: 6.0 Row 1 total: 15.0 Row 2 total: 24.0 Column 0 total: 12.0 Column 1 total: 15.0 Column 2 total: 18.0 Array total: 45.0
Test Case 2
Command Line Arguments | Files in the same directory |
---|---|
input2.txt | input1.txt input2.txt input3.txt input4.txt |
Row 0 total: 1012.55 Row 1 total: 1802.0 Row 2 total: 814.33 Column 0 total: 1568.77 Column 1 total: 1801.42 Column 2 total: 258.69 Array total: 3628.8799999999997
Test Case 3
Command Line Arguments | Files in the same directory |
---|---|
input3.txt | input1.txt input2.txt input3.txt input4.txt |
Row 0 total: 2025.1000000000001 Row 1 total: 3604.0 Row 2 total: 1628.6599999999999 Column 0 total: 1568.77 Column 1 total: 1801.42 Column 2 total: 258.69 Column 3 total: 1568.77 Column 4 total: 1801.42 Column 5 total: 258.69 Array total: 7257.759999999999
Test Case 4
Command Line Arguments | Files in the same directory |
---|---|
input4.txt | input1.txt input2.txt input3.txt input4.txt |
Row 0 total: 1012.55 Row 1 total: 1802.0 Row 2 total: 814.33 Row 3 total: 1012.55 Row 4 total: 1802.0 Row 5 total: 814.33 Row 6 total: 1012.55 Row 7 total: 1802.0 Row 8 total: 814.33 Column 0 total: 4706.3099999999995 Column 1 total: 5404.26 Column 2 total: 776.07 Array total: 10886.64
Please help!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started