Assignment 06 – 2D Arrays
Problem 1:
Create a program that stores six months of sales (in dollars) for four salespersons in a 2D array,
prints the table in a neat layout, and implements methods to compute totals by month, find the
month with the highest total, check if a single salesperson’s sales are increasing from January to
June, and get the best month for a given salesperson.
Part A — Declare and Initialize the Array (in the main method)
Use an initializer list (roster notation) to create and populate the matrix (rows = months Jan–Jun,
columns = salespersons in order: John, Mat, Mark, Henry). If a value is missing in the original
table, use 0 as a placeholder.
Month John Mat Mark Henry
Jan 4384 2060 2213 2082
Feb 6655
0 3330 3847 3294
Mar 3305 3796 3823 4054
Apr 0 2867 3428 4094
May 2721 3901 2260 5427
Jun 2703 4267 3574 5958
Part B — Analysis Methods (Return Values)
Implement methods that return the requested results. Your main should call these and then
print/format the returned values.
• Complete method monthlySales(double [][] a, int month) that
returns the total monthly sales of all four salespeople, depending on the month
given.
public static double monthlySales(double[][] a, int month)
• Complete method highestSalesMonth(double [][] a) that returns an
integer value of the highest total monthly sales among the given months. If the
highest sales month is January, it will return 1; if it is February, it will return 2; etc.
public static int highestSalesMonth(double[][] a)
Assignment 06 – 2D Arrays
• Complete method isIncreasing(double [][] a, int salesperson)
that returns true if the monthly sales of the person from January to June are
increasing, otherwise false.
public static boolean isIncreasing(double[][] a, int salesperson)
• Complete method highestMonth(double [][] a, int salesperson)
that returns an int value of the highest month sales that the salesperson has
performed.
Example: highestMonth(a, 2) will look at Mat(the second salesperson) and
determine which month is his highest performing month. The method will return 6
for June.
public static int highestMonth(double[][] a, int salesperson)
Part C — Print the Table
Write a method to print the matrix in the specified layout with headers:
public static void printTable(double[][] a)
• Print headers: Month John Mat Mark Henry.
• Print each month’s row (Jan–Jun) aligned in columns.
Sample Run
Month John Mat Mark Henry
Jan 4384 2060 2213 2082
Feb 0 3330 3847 3294
Mar 3305 3796 3823 4054
Apr 0 2867 3428 4094
May 2721 3901 2260 5427
Jun 2703 4267 3574 5958
monthlySales(sales, 2): 10471.0
highestSalesMonth(sales): 6
isIncreasing(sales, 4): true
highestMonth(sales, 2): 6
Assignment 06 – 2D Arrays
Problem 2:
Create a program that stores a fixed 5×5 integer matrix and implements methods to print: (a) the
middle row, (b) the middle column, (c) the upper half (on/above the main diagonal), and (d) the
lower half (on/below the main diagonal).
Part A — Declare and Initialize the Array (in the main method)
Use an initializer list to create the matrix:
9 1 21 13 12
22 23 5 18 25
8 4 23 2 24
16 4 21 17 20
7 8 3 15 7
Part B — Printing Methods
Implement the following methods. Use fixed-width formatting so columns line up; print blanks
for cells that aren’t part of the requested slice.
• Write a method printMiddleRow(int[][] a) to print only the middle row of
the given array.
Printout: 8 4 23 2 24
public static void printMiddleRow(int[][] a)
• Write a method printMiddleCol(int[][] a) to print only the middle row of
the given array.
Printout: 21 5 23 21 3
public static void printMiddleCol(int[][] a)
• Write a method printUpperHalf(int[][] a) to print only the upper half of the
array.
The result would be: (cells shaded in blue are printed)
9 1 21 13 12
22 23 5 18 25
8 4 23 2 24
16 4 21 17 20
7 8 3 15 7
Assignment 06 – 2D Arrays
public static void printUpperHalf(int[][] a)
• Write a method printLowerHalf(int[][] a) to print only the lower half of the
array.
The result would be: (cells shaded in green are printed)
9 1 21 13 12
22 23 5 18 25
8 4 23 2 24
16 4 21 17 20
7 8 3 15 7
public static void printLowerHalf(int[][] a)
Sample Run
Middle row: 8 4 23 2 24
Middle column: 21 5 23 21 3
Upper half:
9 1 21 13 12
23 5 18 25
23 2 24
17 20
7
Lower half:
9
22 23
8 4 23
16 4 21 17
7 8 3 15 7
Assignment 06 – 2D Arrays
Problem 3:
Create a program that builds two 5×5 integer matrices with random values from 1 – 9, prints both
matrices, and implements methods to:
• add them element-by-element,
• add them mixed (row-major order from the first matrix with column-major order from
the second),
• and compute the transpose of a matrix.
Part A — Create and Randomize the Arrays (in the main method)
• Declare two 5×5 int matrices:
• Seed a random number generator and fill both matrices with values in [1, 9].
Example arrays (used for all illustrations below):
array a array b
4 6 2 4 8 8 7 5 1 2
5 7 5 1 9 8 9 4 2 8
4 9 2 1 2 5 3 1 5 9
5 4 5 2 2 4 9 9 8 9
5 9 7 6 1 3 5 9 7 4
Part B — Required Methods (Sample Computations / Examples)
You will implement these methods in your program. Below are examples (using the arrays in
Part A.2) showing what each method should compute.
• Write a method addArrays(int[][]a, int[][]b) that returns a 2D array of
the sum of the elements at its position.
Examples:
1. result[0][0] = 4 + 8 = 12
2. result[1][3] = 1 + 2 = 3
3. result[4][2] = 7 + 9 = 16
• Write a method addArraysMix(int[][]a, int[][]b) that returns a 2D array
of the sum of the elements by row-major for the first array and by column-major for
the second array. The sum is saved in another array using row-major.
Examples:
1. result [0][1] = a[0][1] + b[1][0] = 6 + 8 = 14
2. result [3][0] = a[3][0] + b[0][3] = 5 + 1 = 6
3. result [2][3] = a[2][3] + b[3][2] = 1 + 9 = 10
Assignment 06 – 2D Arrays
• Write a method transpose(int[][]a) that will rearrange the elements of the
array from row major to column major.
Examples:
1. result [1][3] = a[3][1] = 4
2. result [4][0] = a[0][4] = 8
3. result [2][4] = a[4][2] = 7
Sample Run
array a
4 6 2 4 8
5 7 5 1 9
4 9 2 1 2
5 4 5 2 2
5 9 7 6 1
array b
8 7 5 1 2
8 9 4 2 8
5 3 1 5 9
4 9 9 8 9
3 5 9 7 4
a + b (element-wise)
12 13 7 5 10
13 16 9 3 17
9 12 3 6 11
9 13 14 10 11
8 14 16 13 5
a (row-major) + b (column-major)
12 14 7 8 11
12 16 8 10 14
9 13 3 10 11
6 6 10 10 9
7 17 16 15 5
transpose(a)
4 5 4 5 5
6 7 9 4 9
2 5 2 5 7
4 1 1 2 6
8 9 2 2 1
Assignment 06 – 2D Arrays
Problem 4:
Build an 8×10 integer matrix whose entries are only 0, 1, or 2. Print the matrix, then implement
two methods:
• Replace the second zero in any adjacent horizontal pair (scan row-major) with 2,
without altering the original.
• Replace the last zero in any vertical run (scan column-major) with 8, without altering
the original.
Part A — Create and Randomize the Array (in the main method)
• Declare an 8×10 int matrix:
• Fill it with random values in {0,1,2}
Example array (used for all illustrations below):
2 0 0 1 0 0 1 2 2 2
0 0 2 0 2 1 1 1 2 2
0 0 0 1 1 1 2 2 2 2
0 0 2 2 1 1 2 1 2 2
1 2 2 2 0 0 2 1 0 0
2 1 0 1 1 0 0 0 2 1
1 0 0 2 1 1 0 0 2 2
2 0 1 1 2 1 1 2 2 0
Assignment 06 – 2D Arrays
Part B — Methods & Examples (do not modify the original array)
• Write method replaceSecond(int[][] a) that looks at adjacent (neighbor)
values of zeros and replaces the second occurrence of zero with 2. Use row major
to traverse the array. Ensure that you don’t destroy the original values of the array.
Example:
• Write a method replaceLast(int[][] a) that looks at successive (sequence)
values of zeros and replaces the last occurrence of zero with an 8. Use column
major to traverse the array. Ensure that you don’t destroy the original values of the
array.
Example:
Original array After the method call
Assignment 06 – 2D Arrays
Sample Run
Original (8×10, values in {0,1,2}):
2 0 0 1 0 0 1 2 2 2
0 0 2 0 2 1 1 1 2 2
0 0 0 1 1 1 2 2 2 2
0 0 2 2 1 1 2 1 2 2
1 2 2 2 0 0 2 1 0 0
2 1 0 1 1 0 0 0 2 1
1 0 0 2 1 1 0 0 2 2
2 0 1 1 2 1 1 2 2 0
After replaceSecond (row-major, second zero in each horizontal pair → 2):
2 0 2 1 0 2 1 2 2 2
0 2 2 0 2 1 1 1 2 2
0 2 0 1 1 1 2 2 2 2
0 2 2 2 1 1 2 1 2 2
1 2 2 2 0 2 2 1 0 2
2 1 0 1 1 0 2 0 2 1
1 0 2 2 1 1 0 2 2 2
2 0 1 1 2 1 1 2 2 0
After replaceLast (column-major, last zero in each vertical run → 8):
2 0 0 1 0 0 1 2 2 2
0 0 2 0 2 1 1 1 2 2
0 0 0 1 1 1 2 2 2 2
8 8 2 2 1 1 2 1 2 2
1 2 2 2 0 0 2 1 0 0
2 1 0 1 1 8 0 0 2 1
1 0 8 2 1 1 8 8 2 2
2 8 1 1 2 1 1 2 2 0