Chapter 7. PC #16. 2D Array Operations JAVA ECLIPSE Write a program that creates
ID: 3741564 • Letter: C
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 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:
____________________________________________________________________________________
I need it to be able to produce these output.
Test Case 1
input1.txt
input2.txt
input3.txt
input4.txt
Test Case 2
input1.txt
input2.txt
input3.txt
input4.txt
Test Case 3
input1.txt
input2.txt
input3.txt
input4.txt
Test Case 4
input1.txt
input2.txt
input3.txt
input4.txt
Please help!
Command Line Arguments Files in the same directory input1.txtinput1.txt
input2.txt
input3.txt
input4.txt
Explanation / Answer
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 [][] a) {
int i,j,tot=0;
for(i=0;i<a.length;i++) //for each row
for(j=0;j<a[0].length;j++) //for each column
tot+=a[i][j]; //add the elements
return tot;
}
public static double getAverage(double [][] a) {
return (double)getTotal(a)/(a.length*a[0].length);
}
public static double getRowTotal(double [][] a, int n) {
int j;
double tot=0;
for(j=0;j<a[0].length;j++)
tot+=a[n][j];
return tot;
}
public static double getColumnTotal(double [][] a, int n) {
int j;
double tot=0;
for(j=0;j<a.length;j++)
tot+=a[j][n];
return tot;
}
public static double getHighestInRow(double [][] a, int n) {
int j;
double max=a[n][0];
for(j=0;j<a[0].length;j++)
if(a[n][j]>max)
max=a[n][j];
return max;
}
public static double getLowestInRow(double [][] a, int n) {
int j;
double low=a[n][0];
for(j=0;j<a[0].length;j++)
if(a[n][j]<low)
low=a[n][j];
return low;
}
}