# 2D array operations

## The problem

Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type 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 values in the specified row in 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 values in the specified row in the array.

Demonstrate each of the methods in this program.

While we have shown the requirements of the question asked above, each of the methods below translates into a defined behavior for a one dimensional array. Breaking it down further instead of creating methods you could use the following passing in the array you wish to perform the calculation on:

 Method name Behavior getAverage Find average of elements in array getRowTotal/getTotal Add all elements of array to find sum getHighestInRow Find highest value in array getLowestInRow Find lowest value in array

## Level Up

• Write a Junit unit test to validate the logic if each method behaves as you expect
• Instead of passing a two-dimensional array and a subscript, refactor to use the appropriate min, max, sum and count methods.
• Fix all output to be two decimal places by writing a method that accepts a double and returns a formatted string.

2D array operations posted by on

Tagged: java and java-exercises-beginner

Share on: