The topic is arrays!!! i need this program in JAVA for BEGINNERS! please explain
ID: 3865686 • Letter: T
Question
The topic is arrays!!! i need this program in JAVA for BEGINNERS! please explain each step with COMMENT lines
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. Demonstrate each of the methods in this program.
Explanation / Answer
public class FormattedTablePrint {
public static void printRow(int[] row) {
for (int i : row) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
}
public static void main(String[] args) {
int twoDm[][]= new int[7][5];
int i,j,k=1;
for(i=0;i<7;i++) {
for(j=0;j<5;j++) {
twoDm[i][j]=k;
k++;
}
}
for(int[] row : twoDm) {
printRow(row);
}
}
public static int size(Object object) {
if (!object.getClass().isArray()) {
return 1;
}
int size = 0;
for (int i = 0; i < Array.getLength(object); i++) {
size += size(Array.get(object, i));
}
return size;
}
private static double getAverage(){
int counter=0;
double sum = 0;
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
sum = sum+array[i][j];
counter++;
}
}
return sum / counter;
}
private static int getSumOfRow(int[][] array, int rowNum) {
int sumOfRows = 0;
for(int i = 0; i < array[rowNum].length; i++)
sumOfRows += array[rowNum][i];
return sumOfRows;
}
private static int getColumnSum(int[][] array, int columnNum) {
int sumOfColumn = 0;
for(int i = 0; i < array.length; i++)
sumOfColumn += array[i][columnNum];
return sumOfColumn;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.