/*
 * Creator: Nighthawk Coding Society
 * Mini Lab Name: Hello Series,featuring Monkey Jumpers
 */

/**
 * Class for Monkeys: a 2D array of Monkeys
 * As well as method to print the Poem
 */
class MonkeyLoop {
    //The area between class definition and the 1st method is where we keep data for object in Java
    String [][] monkeys;    //2D Array: AP CSA Unit 8: 2D array of strings
                            //2D array is like a grid [x][y]
                            // or like a spreadsheet [row][column]

    /**
     * Constructor initializes a 2D array of Monkeys
     */
    public MonkeyLoop() {
        //Storing Data in 2D arrays
        monkeys = new String[][]{   //2D array above is just a name, "new" makes a container ("object")
                //Changed the monkeys
                //Monkey 1
                {
                    "w  c(..)o   (",
                     "\\__(-)    __)",
                     "    /\\   (",
                     "    /(_)___)",
                     "    w /|",
                     "    | \\",
                     "    a  a  "       
                },
                //Monkey 2
                {
                    "w  c(..)o   (",
                    "\\__(-)    __)",
                    "      /\\   (",
                    "    /(_)___)",
                    "        w /|",
                    "         | \\",
                    "      b  b  "       
                },
                //Monkey 3
                {
                    "w  c(..)o   (",
                    "\\__(-)    __)",
                    "       /\\   (",
                    "    /(_)___)",
                    "        w /|",
                    "         | \\",
                    "      c  c  "  
                },
                //Monkey 4
                {
                    "w  c(..)o   (",
                    "\\__(-)    __)",
                    "       /\\   (",
                    "    /(_)___)",
                    "        w /|",
                    "         | \\",
                    "      d  d  "     
                },

        };
    }

    /**
     * Loop and print monkeys in array
     * ... repeat until you reach zero  ...
     */
    public void printPoem() {
        //begin the poem
        System.out.println();
        System.out.println("Monkey Jumpers Poem in Java Loopy");

        // monkeys (non-primitive) defined in constructor knows its length
        int monkeyCount = monkeys.length;
        for (int i = monkeyCount; i >= 1; i--)  //loops through 2D array length backwards
        {
     
            //this print statement shows current count of Monkeys
            //  concatenation (+) of the loop variable and string to form a countdown message
            System.out.println(i + " little monkeys jumping on the bed...");

            //Loop through column then row to print out the monkeys horizontally 
            for (int col = 0; col < 7; col++) {  //cycles through "cells" of 2d array

                /*cycles through columns to print
                each monkey part by part, will eventually print entire column*/
                for (int row = 0; row < monkeyCount; row++) {

                    // prints specific part of the monkey from the column
                    System.out.print("       " + monkeys[row][col] + "           ");

                    //this is new line between separate parts
                    //System.out.println();
                }

                //this new line gives separation between stanza of poem
                System.out.println();
            }
            //Finish the rhyme 
            System.out.println("One fell off and bumped his head");
            System.out.println("Mama called the doctor and the doctor said");
            System.out.println("No more monkeys jumping on the bed\n");


            //countdown for poem, decrementing monkeyCount variable by 1
            monkeyCount -= 1;
        }

        //out of all the loops, prints finishing messages
        System.out.println("No more monkeys jumping on the bed");
        System.out.println("0000000000000000000000000000000000");
        System.out.println("             THE END              ");
    }

    /**
    * A Java Driver/Test method that is the entry point for execution
    */
    public static void main(String[] args)  {
        new MonkeyLoop().printPoem();   //a new monkey list and output in one step
    }

}
MonkeyLoop.main(null);
Monkey Jumpers Poem in Java Loopy
4 little monkeys jumping on the bed...
       w  c(..)o   (                  w  c(..)o   (                  w  c(..)o   (                  w  c(..)o   (           
       \__(-)    __)                  \__(-)    __)                  \__(-)    __)                  \__(-)    __)           
           /\   (                        /\   (                         /\   (                         /\   (           
           /(_)___)                      /(_)___)                      /(_)___)                      /(_)___)           
           w /|                          w /|                          w /|                          w /|           
           | \                           | \                           | \                           | \           
           a  a                          b  b                          c  c                          d  d             
One fell off and bumped his head
Mama called the doctor and the doctor sai
No more monkeys jumping on the bed

3 little monkeys jumping on the bed...
       w  c(..)o   (                  w  c(..)o   (                  w  c(..)o   (           
       \__(-)    __)                  \__(-)    __)                  \__(-)    __)           
           /\   (                        /\   (                         /\   (           
           /(_)___)                      /(_)___)                      /(_)___)           
           w /|                          w /|                          w /|           
           | \                           | \                           | \           
           a  a                          b  b                          c  c             
One fell off and bumped his head
Mama called the doctor and the doctor sai
No more monkeys jumping on the bed

2 little monkeys jumping on the bed...
       w  c(..)o   (                  w  c(..)o   (           
       \__(-)    __)                  \__(-)    __)           
           /\   (                        /\   (           
           /(_)___)                      /(_)___)           
           w /|                          w /|           
           | \                           | \           
           a  a                          b  b             
One fell off and bumped his head
Mama called the doctor and the doctor sai
No more monkeys jumping on the bed

1 little monkeys jumping on the bed...
       w  c(..)o   (           
       \__(-)    __)           
           /\   (           
           /(_)___)           
           w /|           
           | \           
           a  a             
One fell off and bumped his head
Mama called the doctor and the doctor sai
No more monkeys jumping on the bed

No more monkeys jumping on the bed
0000000000000000000000000000000000
             THE END              

Is this program in more of an Imperative Programming Style or OOP style? Explain.

This program is more of an imperative programming style since here each monkey is not an object but rather elements in a 2D array with the 4 monkeys being the 4 rows and each part of the monkey (head, mouth, etc) being columns. Each monkey does not have methods/attributes.

Build an where the monkey is an object versus two-dimensional array.

public class monkey {

    String[] body;
    String name;
    int age;
}
    
        
monkey curiousGeorge = new monkey();
curiousGeorge.body = new String[]{
            "w  c(..)o   (",
            "\\__(-)    __)",
            "    /\\   (",
            "    /(_)___)",
            "    w /|",
            "    | \\",
            "    a  a  "    
        };
        
curiousGeorge.name = "Curious George";
curiousGeorge.age = 4;
System.out.println("The first monkey's name is " + curiousGeorge.name);
System.out.println("The monkey is " + String.valueOf(curiousGeorge.age) + " years old!");
The first monkey's name is Curious George
The monkey is 4 years old!

2D Arrays

Study loops and zero based counting

- Study two-dimensional (2D) array references
- Explain different way you can access a 2D array
  • To access an element from the 2D array, use the syntax arrayName[row][column]
  • For example, to print out the number 4 from the 2D array, you would use System.out.println(numbers[1][1])
  • Note:index starts from 0 so the first row (in this case odd numbers) would have an index of 0 and the second row (even numbers) would have an index of 1 and same for columns.
  • You can also loop through a 2D array to see every element
//2D array of odd and even integers


//loop throw every row in the array 
for (int row = 0; row < numbers.length; row++) {
    //loop through every column in each row
    for(int column = 0; column < numbers[row].length; column++) {
    // will print out every element in the array in order
      System.out.println(numbers[row][column]);
    }
  }

// can also change the values
numbers[0][0] = 100; 
System.out.println("\nModified array");
for (int row = 0; row < numbers.length; row++) {
    //loop through every column in each row
    for(int column = 0; column < numbers[row].length; column++) {
    // will print out every element in the array in order
      System.out.println(numbers[row][column]);
    }
  }
1
3
5
7
9
2
4
6
8
10

Modified array
100
3
5
7
9
2
4
6
8
10