Question: /** * LABTEST 3 - EECS1710, F2017 * * * FIRST NAME: XXXXXXXXXXXXXXXXXXXXX * LAST NAME: YYYYYYYYYYYYYYYYYYYYY * LOGIN: ZZZZZZZZZ * * SID: SSSSSSSSS *
/**
* LABTEST 3 - EECS1710, F2017
*
*
* FIRST NAME: XXXXXXXXXXXXXXXXXXXXX
* LAST NAME: YYYYYYYYYYYYYYYYYYYYY
* LOGIN: ZZZZZZZZZ
*
* SID: SSSSSSSSS
*
*
*
*/
// QUESTIONS TO ANSWER + INSTRUCTIONS ARE IN MAIN METHOD BELOW
public class Question01 {
// This method will print out a 2D array (DO NOT MODIFY)
public static void printGrid(int[][] grid, int m, int n) {
// This method will print out the m x n array
// DO NOT CHANGE THIS METHOD
for (int i=0; i < m; i++) { // iterates over rows
for (int j=0; j < n; j++) { // iterates over columns
System.out.printf("%5d");
}
System.out.println();
}
}
// This method is for part 1b - you need to modify and complete it
public static int sumColumn(int[][] grid, int col) {
int sum = 0;
// COMPLETE THIS METHOD (for Part 1b)
return sum;
}
public static void main(String[] args) {
// *************************************************************************
// TASK 1a: [5 marks]
//
// Declare and initialize a 2D array of integers called "myGrid"
//
// The size of the array is given by the constants M and N (defined below)
//
// Initialize the array (at declaration or with a loop) such that each
// of the positions of the array are assigned unique integers starting
// from 1, and incrementing by 1 up to the value M*N.
//
// Here is an example of the expected output when printed using the printGrid
// method (provided for you):
//
// myGrid =
//
// 1 2 3 4 5
// 6 7 8 9 10
// 11 12 13 14 15
// 16 17 18 19 20
//
//
//
final int M = 4;
final int N = 5;
// CODE FOR 1(a) GOES HERE
System.out.println(" Part 1a: ");
System.out.println("myGrid = ");
// call to printGrid method (outputs myGrid)
// note printGrid should NOT be modified!
printGrid(myGrid, M, N);
// *************************************************************************
// TASK 1b: [5 marks total]
//
// Complete the method "sumColumn(int grid, int col)" at the top of this file,
// by writing a loop to find the sum of all the elements in a 2D array
// that are in the column specified by "col".
//
// e.g. if col=2, then only the numbers denoted by '*' (shown below)
// would be added in the sum
//
// myGrid =
//
// 1 2 * 4 5
// 6 7 * 9 10
// 11 12 * 14 15
// 16 17 * 19 20
//
//
// If you could not do 1(a), create another MxN array "myGrid" for this part
// with any values you wish
int col = 2;
// CODE FOR 1(b) GOES IN METHOD sumColumn defined at top of this file
// CODE GOES THERE, NOT HERE!!
int sumCol = sumColumn(myGrid, col);
System.out.println(" Part 1b: ");
System.out.println("The sum of elements in column " + col + "of myGrid = " + sumCol);
System.out.println();
// *************************************************************************
// TASK 1c: [5 marks total]
//
// Modify the elements in myGrid (using a loop), so that the following
// is printed to the screen by the printGrid() method
//
// DO NOT MODIFY THE printGrid() METHOD !!
// Only modify how you set the values for the myGrid array
//
// myGrid =
//
// 1 5 9 13 17
// 2 6 10 14 18
// 3 7 11 15 19
// 4 8 12 16 20
//
//
// CODE FOR 1(c) GOES HERE
System.out.println(" Part 1c: ");
System.out.println("myGrid = ");
printGrid(myGrid, M, N);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
