Homework

  • Create a class for 2D array learning.
  • Create a method too initialize a 2D array with arbitrary values
  • Create a method to reverse the 2D array and print out the values
  • Create a method that asks for the input of a position and it returns the corresponding value
  • Create a method that multiplies each value in a row and then adds all the products together
  • Create a new object to test out each method in the main function
import java.util.Scanner;

public class Arrays {
    int[][] arr = new int[5][5];

    public void setArbitraryArray() {
        for (int i = 0; i<arr.length; i++) {
            for (int j = 0; j<arr[i].length; j++) {
                arr[i][j] = (int) (Math.random()*20)/ (i+2);
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    public void reverseArray() {
        for (int i = arr.length-1; i >= 0; i--) {
            for (int j = arr[i].length-1; j >= 0; j--) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    public void findValue() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter Row: ");
        int row = scanner.nextInt();
        System.out.println(row);
        System.out.print("Enter Column: ");
        int col = scanner.nextInt();
        System.out.println(col);
        System.out.println(arr[row][col]);
    }

    public void rowProduct() {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            int prod = 1;
            for (int j = 0; j <arr[i].length; j++) {
                prod = prod * arr[i][j];
            }
            sum += prod;
        }
        System.out.println("Sum of row products: " + sum);
    }

    public static void main(String[] args) {
        Arrays arr = new Arrays();
        System.out.println("Arbitrary Values:");
        arr.setArbitraryArray();
        System.out.println("Reversed Array:");
        arr.reverseArray();
        System.out.println("Value of arr[4][2]:");
        arr.findValue();
        arr.rowProduct();
    }
}
Arrays.main(null);
Arbitrary Values:
7 6 2 6 7 
2 2 3 1 3 
0 4 4 0 0 
2 3 1 2 1 
1 0 1 1 1 
Reversed Array:
1 1 1 0 1 
1 2 1 3 2 
0 0 4 4 0 
3 1 3 2 2 
7 6 2 6 7 
Value of arr[4][2]:
Enter Row: 4
Enter Column: 2
1
Sum of row products: 3576

Extra Credit

class MonkeyLoop {
    String [][] monkeys;
    String [][] cthulhus;


    public MonkeyLoop() {
        monkeys = new String[][]{

                {
                        "ʕง ͠° ͟ل͜ ͡°)ʔ ",      //[0][0] eyes
                },

                {
                        "  \\_⏄_/  ",      //[1][0] chin

                },

                {
                        "  --0--   ",       //[2][0] body

                },

                {
                        "  ⎛   ⎞   "        //[3][0] legs
                },


        };

        cthulhus = new String[][]{
            {
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⡀⠀⠀⠀⠀⠀⠀⢎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ʕง ͠° ͟ل͜ ͡°)ʔ ",
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠂⢱⠀⠀⢀⣤⡀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀  \\_⏄_/  ",
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠻⠉⣧⣿⣿⣿⠀⠀⢸⠇⠀⠐⠉⡆⠀⠀⠀⠀⠀⠀  --0--   ",
                    "⠀⠀⠀⠀⢀⠔⠒⢦⠀⢻⡄⠀⢿⣻⣿⡿⢀⣴⣋⣄⣄⣌⣠⠃⠀⠀⠀⠀⠀⠀  ⎛   ⎞   ",
                    "⠀⠀⠀⠀⠈⠀⢀⡞⠀⠈⠛⣷⣾⣿⣿⣿⣿⣿⣯⣥⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀         ",
                    "⠀⠀⠀⠀⠀⠀⠈⠷⣦⣴⡾⢿⣿⡿⢿⣿⣋⣽⠶⢦⠙⢷⡀⠀⠀⠀⠀⠀⠀⠀         ",
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⡏⢀⡆⠈⠉⠁⡄⠈⡇⠘⢇⠀⢈⡆⠀⠀⠀⠀         ",
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡯⠀⠸⠁⠀⠀⠸⣧⡀⡇⠀⠈⠉⠉⠀⠀⠀⠀⠀         ",
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⡴⠁⠀⠀⠀⠀⠙⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀          "
            },
        };
    }

    public void printPoem() {
        System.out.println();
        System.out.println("A story of a Monkey and Cthulhus");
        int monkeyCount = monkeys.length;
        // Missing code
        for (int i = 1; i >= 1; i--)
        {
            //hint: missing code
            for (int row = 0; row < monkeyCount; row++) {
                for (int col = 0; col < monkeys[row].length; col++) {
                    System.out.print(monkeys[row][col] + " ");
                }
                System.out.println();
            }
        }
            //hint: missing code, another for loop
        int cthulhuCount = cthulhus[0].length;
        for (int col1 = 0; col1 < cthulhuCount; col1++) {
            for (int row1 = 0; row1 < 1; row1++){
                System.out.print(cthulhus[row1][col1]);
            }
            System.out.println();
        }
        // End of missing code
        System.out.println("0000000000000000000000000000000000");
        System.out.println("             THE END              ");
    }


    public static void main(String[] args)  {
        new MonkeyLoop().printPoem();
    }
}
MonkeyLoop.main(null);
A story of a Monkey and Cthulhus
ʕง ͠° ͟ل͜ ͡°)ʔ  
  \_⏄_/   
  --0--    
  ⎛   ⎞    
⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⡀⠀⠀⠀⠀⠀⠀⢎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ʕง ͠° ͟ل͜ ͡°)ʔ 
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠂⢱⠀⠀⢀⣤⡀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀  \_⏄_/  
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠻⠉⣧⣿⣿⣿⠀⠀⢸⠇⠀⠐⠉⡆⠀⠀⠀⠀⠀⠀  --0--   
⠀⠀⠀⠀⢀⠔⠒⢦⠀⢻⡄⠀⢿⣻⣿⡿⢀⣴⣋⣄⣄⣌⣠⠃⠀⠀⠀⠀⠀⠀  ⎛   ⎞   
⠀⠀⠀⠀⠈⠀⢀⡞⠀⠈⠛⣷⣾⣿⣿⣿⣿⣿⣯⣥⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀         
⠀⠀⠀⠀⠀⠀⠈⠷⣦⣴⡾⢿⣿⡿⢿⣿⣋⣽⠶⢦⠙⢷⡀⠀⠀⠀⠀⠀⠀⠀         
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⡏⢀⡆⠈⠉⠁⡄⠈⡇⠘⢇⠀⢈⡆⠀⠀⠀⠀         
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡯⠀⠸⠁⠀⠀⠸⣧⡀⡇⠀⠈⠉⠉⠀⠀⠀⠀⠀         
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⡴⠁⠀⠀⠀⠀⠙⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀          
0000000000000000000000000000000000
             THE END