Java method help I need help with this method for my beginne
Java method help... I need help with this method for my beginner Java course, so as basic code as possible.
Complete the method, named bigSum, in the class named ArrayOps.java. The only parameter to this method is a two-dimensional array of integers with only 2 rows. This method should sum each row of integers separately, and then decide which of the two sums is larger. This larger sum is the return value for this method.
For example, suppose the array contains the following data:
The sum of the first row is (25 + 16 + 14) = 55, while the sum of the second row is (13 + 27 + 22) = 62. Comparing, we see that the sum of the second row is the larger sum; hence, the method returns the integer 62.
You might think of this as comparing the test grades of two students.
Complete the following file:
public class ArrayOps
{
/**
This method sums up both rows of a two-dimensional array
(the only parameter to the method) and returns the greater sum.
@param theArray, a 2-D array of integers
@return, the greater row sum
*/
public static int bigSum(int[][] theArray)
{
// your work here
// loop though theArray, summing first row
// your work here
// loop though theArray, summing second row
// your work here
// return larger sum
// your work here
}
}
Solution
public class ArrayOps {
public static void main(String[] args) {
int arr[][]={{25, 16, 14 },{13, 27, 22 }};
int result=bigSum(arr);
System.out.println(\"Max values is=\"+result);
}
public static int bigSum(int[][] theArray){
int rowSum=0;
int len=theArray.length;
int [] sums = new int[len];
int maxValue = sums[0];
for(int i=0;i<len;i++){// reading array
for(int j=0;j<len+1;j++){
rowSum=rowSum+theArray[i][j];// adding each row into rowSum
//System.out.print(theArray[i][j]+\" \");
}
sums[i]=rowSum; // storing each row into sums array
//System.out.println(sums[i]);
rowSum=0;// then assaiging eachrow sum into zero becasue we need to calculate next rowSum
}
for (int i = 1; i < sums.length; i++) {// here finding max value in sums array
if (sums[i] > maxValue) {
maxValue = sums[i];
}
}
return maxValue;
}
}

