Notes

  • 2D Array is an array of arrays
  • Kind of like a grid, have as many as columns and rows
  • Declaring a 2D Array:
    • int[][] numbers;
    • String[][] names;
  • to initialize a 2D array, you have to iterate through each row and each column
    • have a nested for loop
  • Display values backwards/upside down

Homework

  • [X] Create a class for 2D array learning.
  • [X] Create a method too initialize a 2D array with arbitrary values
  • [X] Create a method to reverse the 2D array and print out the values
  • [X] Create a method that asks for the input of a position and it returns the corresponding value
  • [X] Create a method that multiplies each value in a row and then adds all the products together
  • [X] Create a new object to test out each method in the main function
/**
 * 2D Array Lesson HW
 * 
 * @author Natalie Beckwith
 * @version 1
 */

import java.util.Scanner;

public class DoubleArrayLearning
{
     final static private int NUM_ROWS = 4;
     final static private int NUM_COLUMNS = 4;
    
    /**
     * Creates an array and sets values to the array
     * 
     * @return the array with assigned values
     */
    public int[][] getArray()
    {
        int[][] array =
        {
            { 4, 0, 7, 12},
            { 8, 11, 5, 9},
            { 13, 3, 6, 1},
            { 10, 2, 15, 14}
        };
        return array;
    }

    /**
     * Prints the array
     * 
     * @param array
     */
    public void printArray(int[][] array)
    {
        // iterates through each row
        for (int row = 0; row < NUM_ROWS; row++)
        {
            // iterates through each column
            for (int column = 0; column < NUM_COLUMNS; column++)
            {
                // prints value in the array
                System.out.printf("%d\t", array[row][column]);
            }
            System.out.printf("\n");
        }
    }

    /**
     * Prints the array in reverse order
     * 
     * @param array
     */
    public void reverseArray(int[][] array)
    {
        // iterates backwards starting at the last row
        for (int row = NUM_ROWS - 1; row >= 0; row--)
        {
            // iterates through each column starting at the last element
            for (int column = NUM_COLUMNS - 1; column >= 0; column--)
            {
                System.out.printf("%d\t", array[row][column]);
            }
            System.out.printf("\n");
        }
    }

    /**
     * User enters row and column and is given the value of the element they entered
     * 
     * @param array
     */
    public void userArray(int[][] array)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.printf("Enter a row: ");
        int row = scanner.nextInt();

        System.out.printf("\nEnter a column: ");
        int column = scanner.nextInt();

        System.out.printf("\nThe element you entered was [%d],[%d], which has the value: %d\n",
                row, column, array[row][column]);
        scanner.close();
    }

    /**
     * Multiplies each value in a row and adds all products together
     * 
     * @param array
     */
    public void product(int[][] array)
    {
        int sums = 0;
        
        // iterates through each row
        for (int row = 0; row < NUM_ROWS; row++)
        {
            int product = 1;
            
            // iterates through each column
            for (int column = 0; column < NUM_COLUMNS; column++)
            {
                product *= array[row][column];
            }
            System.out.printf("%d\n", product);
            
            sums += product;
        }
        // prints values in array
        System.out.printf("\nSum of products: %d", sums);
    }

    /**
     * Main/Tester method
     * 
     * @param Args
     */
    public static void main(String[] Args)
    {
        DoubleArrayLearning doubleArrayLearning = new DoubleArrayLearning();
        int[][] nums = doubleArrayLearning.getArray();

        // prints the original array
        System.out.printf("Part #1:\n");
        doubleArrayLearning.printArray(nums);

        // reverses the order of the array
        System.out.printf("\nPart #2:\n");
        doubleArrayLearning.reverseArray(nums);

        // prompts the user to enter a row and column
        System.out.printf("\nPart #3:\n");
        doubleArrayLearning.userArray(nums);

        // multiplies each row and adds the together
        System.out.printf("\nPart #4:\n");
        doubleArrayLearning.product(nums);
    }
}

DoubleArrayLearning.main(null);
Part #1:
4	0	7	12	
8	11	5	9	
13	3	6	1	
10	2	15	14	

Part #2:
14	15	2	10	
1	6	3	13	
9	5	11	8	
12	7	0	4	

Part #3:
Enter a row: 
Enter a column: 
The element you entered was [2],[1], which has the value: 3

Part #4:
0
3960
234
4200

Sum of products: 8394