This assignment is to teach the creation and processing of 2 dimensional int arr
ID: 3692857 • Letter: T
Question
This assignment is to teach the creation and processing of 2 dimensional int arrays.
Assume that "intar" is a 2 dimensional int array (made out of rows and columns as in pages 509-521), which contains the following int values:
10 13 26 34 60 90
25 46 57 88 77 91
29 30 41 52 82 92
Write a java program that you will call Assign7.java that has a main method and declares:
The above two dimensional array intar properly as is.(5 points)
A method printArrayAsIs() which has no parameter and returns nothing and is implemented as follows:
public static void printArrayAsIs(){}
Use a nested for loop (two consecutive for loops) to print the 2 dimensional array intar declared above in the exact following order (use the nested for loop in pages 480-484 as an example) (10 points):
10 13 26 34 60 90 25 46 57 88 77 91 29 30 41 52 82 92
printArrayAsIs() is called from main(). (10 points)
A method printArrayColumnWise() which has not parameter and returns nothing and is implemented as follows:
public static void printArrayColumnWise(){}
uses a nested for loop( two consecutive for loops) to print the 2 dimensional array intar declared above in the exact following order (use the nested for loop in pages 480-484 as an example) (25 points):
10 25 29 13 46 30 26 57 41 34 88 52 60 77 82 90 91 92
That is: to print the values of intar going across the columns instead of across the rows.
printArrayColumnWise() is called from the main() method. (10 points)
A method sumDifference() which has no parameter and returns nothing and is implemented as follows:
public static void sumDifference(){}
uses a nested for loop( two consecutive for loops) to print the sum of the elements of intar declared above in the exact following order (use the nested for loop in pages 480-484 as an example) (30 points):
10 - 13 + 26 - 34 + 60 - 90 + 25 - 46 + 57- 88 + 77- 91 + 29 - 30 + 41 - 52 + 82 - 92
That is: to print the final result of the sum of elements as calculated above = -129
sumDifference() is called from the main() method.(10 points)
Do not forget to compile and run Assign7 to verify that the class is running.
Explanation / Answer
Hi, Please find below class implementation as per your requirement and also please note that i have implemented the methods what exactly you want. But here the case is while calling these methods we have to pass that two dimentional array to these methods so that we can deal with that array. Declare in main and handle that array in another method with out passing it is not possible. if you declare that array in main then you have to pass that array to other methods to handle it for operations.
Assign7.java
public class Assign7 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int intar[][] = {{10,13, 26, 34, 60, 90}, {25, 46, 57, 88, 77, 91}, {29, 30, 41, 52, 82, 92}};
printArrayAsIs(intar);
System.out.println(" ");
printArrayColumnWise(intar);
System.out.println(" ");
sumDifference(intar);
}
public static void printArrayAsIs(int intar[][]){
for(int i=0;i<intar.length; i++)
for(int j=0; j<intar[i].length; j++){
System.out.print(intar[i][j]+" ");
}
}
public static void printArrayColumnWise(int intar[][]){
for(int i=0;i<intar[0].length; i++)
for(int j=0; j<intar.length; j++){
System.out.print(intar[j][i]+" ");
}
}
public static void sumDifference(int intar[][]){
int sum = 0;
for(int i=0;i<intar.length; i++){
for(int j=0; j<intar[i].length; j++){
if(j % 2 == 0){
System.out.print(intar[i][j]+" - ");
sum = sum + intar[i][j];
}
else{
if(i == intar.length - 1 && j == intar[i].length-1)
System.out.print(intar[i][j]+" = ");
else
System.out.print(intar[i][j]+" + ");
sum = sum - intar[i][j];
}
}
}
System.out.print(sum);
}
}
Output:
10 13 26 34 60 90 25 46 57 88 77 91 29 30 41 52 82 92
10 25 29 13 46 30 26 57 41 34 88 52 60 77 82 90 91 92
10 - 13 + 26 - 34 + 60 - 90 + 25 - 46 + 57 - 88 + 77 - 91 + 29 - 30 + 41 - 52 + 82 - 92 = -129