Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I asked the same question yesterday but, I forgot to mention the input shoul

ID: 3741785 • Letter: H

Question

Hi, I asked the same question yesterday but, I forgot to mention the input should be from a text file.(it also should validate the input and if the input is invalid ask for the file again)

Chapter 7. PC #16. 2D Array Operations

Write a JAVA 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.

Demonstrate each of the methods in this program.

Explanation / Answer

Below is your code. Let me know if you have any issues: -

Array2D.java

public class Array2D {

/**

* Return true if the indicated row exists in this 2d array.

*

* A row exists if:

* <ul>

* <li>The row index is valid

* <li>The row is not null

* <li>The length of the row is not 0.

* </ul>

*

* @param values

* The two dimensional array

* @param row

* The row index to check

* @return true if the row exists in this array, false otherwise

*/

public static boolean isRowValid(double[][] values, int row) {

if(row < 0) {

return false;

} else if(row > values.length) {

return false;

}

return true;

}

/**

* Return true if the indicated column exists in this 2d array.

*

* A column exists if:

* <ul>

* <li>The column index is non-negative

* <li>At least one row is long enough to contain the column entry

* </ul>

*

* @param values

* The two dimensional array

* @param col

* The column to check

* @return true if the row exists in this array, false otherwise

*/

public static boolean isColumnValid(double[][] values, int col) {

if(col < 0) {

return false;

} else if(col > values[0].length) {

return false;

}

return true;

}

/**

* Return the sum of all the values in one row of the array.

*

* @param values

* The two dimensional array

* @param row

* The index (0 based) of the row to sum

*

* @return The sum of the row array elements. Returns Double.MIN_VALUE if

* the row is not valid.

*

*/

public static double getRowTotal(double[][] values, int row) {

double sum = 0;

for (int i = 0; i < values[0].length; i++) {

sum = sum + values[row][i];

}

return sum;

}

/**

* Return the sum of all the values in the the array.

*

* @param values

* The two dimensional array

* @return The sum of the array elements. Returns Double.MIN_VALUE if there

* are no entries in the array

*/

public static double getTotal(double[][] values) {

double sum = 0;

for (int i = 0; i < values.length; i++) {

for (int j = 0; j < values[0].length; j++) {

sum = sum + values[i][j];

}

}

return sum;

}

// --------------------------------------------------------------------

// METHODS BELOW ARE OPTIONAL. COMPLETE THEM IF YOU WANT MORE PRACTICE.

// --------------------------------------------------------------------

/**

* Return the average of all the values of the array.

*

*

* @param values

* The two dimensional array

* @return The average of the array elements. Returns Double.MIN_VALUE if

* there are no entries in the array

*/

public static double getAverage(double[][] values) {

// HINT: No loops. Use the methods you implemented above.

int count = values.length

* values[0].length; /* number of items in 2D array */

double sum = getTotal(values);

double average = (double) sum / (double) count;

return average;

}

/**

* Return the sum of all the values in one column of the array.

*

* @param values

* The two dimensional array

* @param col

* The number (0 based) of the column to sum

*

* @return The sum of the array elements in that column. Returns

* if the column is not valid.

*

*/

public static double getColumnTotal(double[][] values, int col) {

double sum = 0;

for (int i = 0; i < values.length; i++) {

sum = sum + values[i][col];

}

return sum;

}

/**

* Return the the largest value in one row of the array.

*

* @param values

* The two dimensional array

* @param row

* The number (0 based) of the row to examine

*

* @return The highest of the row array elements.

*/

public static double getHighestInRow(double[][] values, int row) {

/* assume the first is highest and then change it as you check */

double highest = values[row][0];

for (int i = 0; i < values[0].length; i++) {

if (values[row][i] > highest) {

highest = values[row][i];

}

}

return highest;

}

/**

* Return the the lowest value in one row of the array.

*

* @param values

* The two dimensional array

* @param row

* The number (0 based) of the row to examine

*

* @return The lowest of the row array elements.

*/

public static double getLowestInRow(double[][] values, int row) {

/* assume the first is highest and then change it as you check */

double lowest = values[row][0];

for (int i = 0; i < values[0].length; i++) {

if (values[row][i] < lowest) {

lowest = values[row][i];

}

}

return lowest;

}

}

Array2DTest.java

//Class to test the functions

public class Array2DTest {

//main method

public static void main(String[] args) {

//try clause to check if File not FoundException or any other Exception is there

try {

File file = null;

Scanner input = new Scanner(System.in);

while (true) {

System.out.println("Please enter the filename (with address): ");

String fileName = input.next();

file = new File(fileName);

if (file.exists()) {

break;

} else {

System.out.println("File does not exists..");

}

}

// scanner to read file

Scanner scan = new Scanner(file);

//getting rows and column from first line

String rowsAndCols = scan.nextLine();

int row = Integer.parseInt(rowsAndCols.split(" ")[0]);

int col = Integer.parseInt(rowsAndCols.split(" ")[1]);

double[][] arr2D = new double[row][col];

//reading array from file

for (int i = 0; i < arr2D.length; i++) {

for (int j = 0; j < arr2D[0].length; j++) {

arr2D[i][j] = Double.parseDouble(scan.next());

}

}

//printing the array

System.out.println("Array from file: ");

for (int i = 0; i < arr2D.length; i++) {

for (int j = 0; j < arr2D[0].length; j++) {

System.out.print(arr2D[i][j] + " ");

}

System.out.println();

}

//Printing total and average of the array

System.out.println("Total of the array: " + Array2D.getTotal(arr2D));

System.out.println("Average of the array: " + Array2D.getAverage(arr2D));

//NOTE: - Cols and Rows start from 0

//read row or columns subsequent from file

//format is first character says whether it is column or row

// second tells the corresponing row and column number

while (scan.hasNextLine()) {

String comm = scan.nextLine();

String commArr[] = comm.split(" ");

//checking input validity for row and columns

// if valid printing the corresponding results

if (commArr[0].equals("R") && !Array2D.isRowValid(arr2D, Integer.parseInt(commArr[1]))) {

System.out.println("Row is not valid. Please check your file again. ");

break;

} else if (commArr[0].equals("C") && !Array2D.isColumnValid(arr2D, Integer.parseInt(commArr[1]))) {

System.out.println("Column is not valid. Please check your file again. ");

break;

} else if (commArr[0].equals("R")) {

System.out.println("Get Highest in Row #" + commArr[1] + ": "

+ Array2D.getHighestInRow(arr2D, Integer.parseInt(commArr[1])));

System.out.println("Get Lowest in Row #" + commArr[1] + ": "

+ Array2D.getLowestInRow(arr2D, Integer.parseInt(commArr[1])));

System.out.println("Total of row #" + commArr[1] + " is : "

+ Array2D.getRowTotal(arr2D, Integer.parseInt(commArr[1])));

} else if (commArr[0].equals("C")) {

System.out.println("Total of col #" + commArr[1] + " is : "

+ Array2D.getColumnTotal(arr2D, Integer.parseInt(commArr[1])));

}

}

} catch (FileNotFoundException e) {

System.out.println("File not found. Please ensure you have given correct location");

} catch (Exception e){

System.out.println("There is some problem is parsing file. Please check content of file again");

}

}

}

matrix.txt

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
R 2
C 1
C -1

Output

Array from file:
1.0 2.0 3.0 4.0 5.0
6.0 7.0 8.0 9.0 10.0
11.0 12.0 13.0 14.0 15.0
16.0 17.0 18.0 19.0 20.0
Total of the array: 210.0
Average of the array: 10.5
Get Highest in Row #2: 15.0
Get Lowest in Row #2: 11.0
Total of row #2 is : 65.0
Total of col #1 is : 38.0
Column is not valid. Please check your file again.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote