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
public class Homework{ // Create a class for 2D array learning.
   
    private int[][] numbers;
    private int [][] reverse; // reversed 2D array


    public Homework(){

        numbers = new int[5][5];
        reverse = new int[5][5];  // Initialize a 2D array with arbitrary values

    }


    public void init(){ // Initialize a 2D array and set arbitrary values

        for(int i = 0; i < numbers.length; i++){

            for(int j = 0; j < numbers[i].length; j++){

                numbers[i][j] = (int) (Math.random() * (100 - 1)) + 1; 
            }

        }

    }
    
    public void print(int[][] temp){

        for(int i = 0; i < temp.length; i++){

            for(int j = 0; j < temp[i].length; j++){

                System.out.print(temp[i][j] + " "); 
            }
            
            System.out.println(); 

        }
    }

    public void reverse(){ // Create a method to reverse the 2D array and print out the values

        for(int i = 0; i < numbers.length; i++){

            for(int j = 0; j < numbers[i].length; j++){

                reverse[numbers.length-1-i][numbers[i].length-1-j] = numbers[i][j];

            }

        }

    }

    public String inputIndex(){  // Create a method that asks for the input of a position and it returns the corresponding value

        Scanner sc = new Scanner(System.in);

        System.out.println("Row 1 - 5: ");
        int num1 = sc.nextInt()-1;

        System.out.println("Column 1 - 5: ");
        int num2 = sc.nextInt()-1;

        return ("Value:" + numbers[num1][num2]);
    }

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

    public static void main(String[] args) { // Create a new object to test out each method in the main function
        Homework objArray = new Homework();
        objArray.init();
        System.out.println("Original Array");
        objArray.print(objArray.numbers);

        System.out.println(" ");
        objArray.reverse();
        System.out.println("Reversed Array");
        objArray.print(objArray.reverse);

        System.out.println();
        System.out.println(objArray.inputIndex());

        System.out.println();
        objArray.productSum();
    }
}

Homework.main(null);
Original Array
55 47 29 97 13 
37 97 20 41 97 
61 55 37 96 72 
25 63 80 65 34 
25 42 51 62 50 
 
Reversed Array
50 62 51 42 25 
34 65 80 63 25 
72 96 37 55 61 
97 41 20 97 37 
13 97 29 47 55 

Row 1 - 5: 
Column 1 - 5: 
Value:50

Product Sum: 1682486045

Extra

class MonkeyLoop {
    String [][] monkeys;

    //hint: missing code
    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;
        for (int i = 0; i < monkeyCount; i++)
        {
            //hint: missing code
            System.out.println(monkeys[i][0]); // 2D array monkeys is 1 column with multiple rows
        }
        //hint: missing code, another for loop
        for(int j = 0; j < cthulhus[0].length; j++){
            System.out.println(cthulhus[0][j]); // 2D array cthulhus is 1 row with multiple columns
        }
        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