Homework Hacks

Create a class for 2D array learning, a new object to test out each method in the main function, and methods to initialize a 2D array with arbitrary values, reverse the 2D array and print out the values, that ask for the input of a position and it returns the corresponding value, and that multiply each value in a row and then adds all the products together.

// class for 2D array
public class twoDArray {

    
    private int[][] numbers;

    public twoDArray(){
        numbers = new int [3][5];
        this.numbers = numbers; 
    } 

	// method for initializing 2D array with arbitrary values
    public void initValues(){
        System.out.println("Initial Array");
        Random random = new Random();
        for(int i = 0; i < numbers.length;i++){
        for(int j = 0; j <numbers[i].length;j++){ 
            numbers[i][j] = random.nextInt(11 - 1) + 1;
            System.out.print(numbers[i][j]+" ");
        }
        System.out.println(" ");
        }
    }

	// method for reversing 2D array and printing out values
    public void revArray() {
        System.out.println("\nReversed Array");
        for(int i = numbers.length-1;i>=0;i--){
        for(int j = 0; j <numbers[i].length;j++){ 
            System.out.print(numbers[i][j]+" ");
        }
        System.out.println(" ");
        }
        System.out.println("\n");
        
    }

	// method that multiplies each value in a row and then adds all the products together
	public void productSum() {
	
		int finalProduct = 1;
		for(int i = 0;i<numbers.length;i++){
			int product = 1; 
			for(int j = 0; j < numbers[i].length;j++){ 
				product = product * numbers[i][j];
				
			}
			System.out.print("When row is: " + i + ", product is: " + product);
			System.out.println(" ");
			finalProduct += product; 
		}

		System.out.print("The sum of all of the products is: " + finalProduct);
		System.out.println("\n");

	}

	// method that asks for the input of a position and it returns the corresponding value
    public void outputArrayValue() {

        Scanner myObj = new Scanner(System.in);  
        System.out.println("Input a row number");

        String arrayValue1 = myObj.nextLine();  
        System.out.println(arrayValue1);
        int numArrayValue1 = Integer.parseInt(arrayValue1); 

        System.out.println("Input a column number");

        String arrayValue2 = myObj.nextLine();  
        System.out.println(arrayValue2);
        int numArrayValue2 = Integer.parseInt(arrayValue2); 

        System.out.println("At that position in the array, the value is: " + numbers[numArrayValue1][numArrayValue2]);
        System.out.println("\n");
            }

	
}
// new object to test out each method in the main function
twoDArray anArray = new twoDArray(); 
anArray.initValues();
anArray.revArray();
anArray.outputArrayValue(); 
anArray.productSum();
Initial Array
2 1 1 9 4  
5 2 10 7 10  
2 8 2 8 7  

Reversed Array
2 8 2 8 7  
5 2 10 7 10  
2 1 1 9 4  


Input a row number
1
Input a column number
2
At that position in the array, the value is: 10


When row is: 0, product is: 72 
When row is: 1, product is: 7000 
When row is: 2, product is: 1792 
The sum of all of the products is: 8865