I am having some difficulties getting this code to work properly. I cannot figure out how to correctly return all of my methods because I have the println listed inside the methods. So when I run my program, I get the println's , plus the return value. But I don't know how to fix this. Also on my getHighestInRow and getLowestInRow, I am not getting the correct output. I am getting the Highest and Lowest only from the first row instead of each of the rows. Any help would be greatly appreciated. Thanks!
public class Array2DOperations
{
public static void main(String[] args)
{
int [][] numbers = { {100, 75, 90, 83, 85, 98},
{100, 100, 100, 100, 100, 100} };
for (int row = 0; row < numbers.length; row++)
{
for (int col = 0; col < numbers[row].length; col++)
System.out.print(numbers[row][col] + " ");
System.out.println();
}
System.out.println(" ");
// Display the sum of all the values in the array.
System.out.println("The sum of the values " +
"is " + getTotal(numbers));
System.out.println(" ");
// Display the average of all the values in the array.
System.out.println("The average of the values " +
"is " + getAverage(numbers));
System.out.println(" ");
// Display the total of the rows.
System.out.println(getRowTotal(numbers));
System.out.println(" ");
// Display the total of the columns.
System.out.println(getColumnTotal(numbers));
System.out.println(" ");
// Display the highest value in the row.
System.out.println(getHighestInRow(numbers));
System.out.println(" ");
// Display the lowest value in the row.
System.out.println(getLowestInRow(numbers));
}
/**
The getTotal method accepts a 2D array
as its argument and returns the total of
all the values in the array.
@param array The array to sum.
@return The sum of the array elements.
*/
public static int getTotal(int[][] array)
{
int total = 0; // Accumulator
// Sum the values in the array
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[row].length; col++)
total += array[row][col];
}
return total;
}
/**
The getAverage method accepts a 2D array
as its argument and returns the average of
all the values in the array.
@param array The array to average.
@return The average of the array elements.
*/
public static double getAverage(int[][] array)
{
double total = 0.0; // Accumulator
// Sum the values in the array
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[row].length; col++)
total += array[row][col];
//int size = array[row][col].length;
}
double average = total / 12;
return average;
}
/**
The getRowTotal method accepts a 2D array
as its first argument and an integer as its
second argument. The second argument is a
subscript of a row in the array. The method
returns the total of the values in a specified row.
@param array The row in the array to total.
@return The total of the values in a specified row.
*/
public static int getRowTotal(int[][] array)
{
int rowTotal=0;
// Sum the values in the rows of the array
for (int row = 0; row < array.length; row++)
{
rowTotal=0;
// Sum a row
for (int col = 0; col < array[row].length; col++)
rowTotal += array[row][col];
//Display the row's total.
System.out.println("Total of row " + (row) + " is: " + rowTotal);
}
return rowTotal;
}
/**
The getColumnTotal method accepts a 2D array
as its first argument and an integer as its
second argument. The second arguement is a
subscript of a column in the array. The method
returns the total of the values in a specified column.
@param array The array to average.
@return The total of the values in a specified column.
*/
public static int getColumnTotal(int[][] array)
{
int colTotal=0;
// Sum the values in the rows of the array
for (int col = 0; col < array[0].length; col++)
{
colTotal=0;
// Sum a column
for (int row = 0; row < array.length; row++)
colTotal += array[row][col];
//Display the row's total.
System.out.println("Total of column " + (col) + " is: " + colTotal);
}
return colTotal;
}
/**
The getHighestInRow method accepts a 2D array
as its first argument and an integer as its
second argument. The second arguement is a
subscript of a row in the array. The method
returns the highest value in a specified row
of the array.
@param array The array to average.
@return The highest value in a specified row
of the array.
*/
public static int getHighestInRow(int[][] array)
{
int highest = array[0][0];
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[row].length; col++)
{
if (array[row][col] >= highest)
highest = array[row][col];
}
//Display the row's total.
System.out.println("Highest in Row " + (row) + " is: " + highest);
}
return highest;
//Display the row's total.
//System.out.println("Total of row: " + (row) + " is " + rowTotal);
}
/**
The getLowestInRow method accepts a 2D array
as its first argument and an integer as its
second argument. The second argument is a
subscript of a row in the array. The method
returns the lowest value in a specified row
of the array.
@param array The array to average.
@return The lowest value in a specified row
of the array.
*/
public static int getLowestInRow(int[][] array)
{
int lowest = array[0][0];
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[row].length; col++)
{
if (array[row][col] <= lowest)
lowest = array[row][col];
}
//Display the row's total.
System.out.println("Lowest in Row " + (row) + " is: " + lowest);
}
return lowest;
}
}