Monkey Poem in Java Loop Using Iteration

/*
 * 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")
                //Monkey 0
                {
                        "ʕง ͠° ͟ل͜ ͡°)ʔ ",      //[0][0] eyes
                        "  \\_⏄_/  ",      //[0][1] chin
                        "  --0--   ",       //[0][2] body
                        "  ⎛   ⎞   "        //[0][3] legs
                },
                //Monkey 1
                {
                        " ʕ༼ ◕_◕ ༽ʔ",       //[1][0]
                        "  \\_⎏_/  ",
                        "  ++1++  ",
                        "   ⌋ ⌊   "
                },
                //Monkey 2
                {
                        " ʕ(▀ ⍡ ▀)ʔ",       //[2][0]
                        "  \\_⎐_/ ",
                        "  <-2->  ",
                        "  〈  〉 "
                },
                //Monkey 3
                {
                        "ʕ ͡° ͜ʖ ° ͡ʔ",        //[3][0]
                        "  \\_⍾_/  ",
                        "  ==3==  ",
                        "  _/ \\_  "
                },
                //Monkey 4
                {
                        "  (◕‿◕✿) ",          //[4][0]
                        "  \\_⍾_/ ",          //[4][1]
                        "  ==4==  ",          //[4][2]
                        "  _/ \\_ "           //[4][3]
                },

        };
    }

    /**
     * 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...");

            //how many separate parts are there in a monkey monkey?
            for (int row = 0; row < monkeyCount; row++) {  //cycles through "cells" of 2d array

                /*cycles through columns to print
                each monkey part by part, will eventually print entire column*/
                for (int col = 0; col < monkeys[row].length; col++) {

                    // 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();
            }

            //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
5 little monkeys jumping on the bed...
ʕง ͠° ͟ل͜ ͡°)ʔ  
  \_⏄_/   
  --0--    
  ⎛   ⎞    

 ʕ༼ ◕_◕ ༽ʔ 
  \_⎏_/   
  ++1++   
   ⌋ ⌊    

 ʕ(▀ ⍡ ▀)ʔ 
  \_⎐_/  
  <-2->   
  〈  〉  

ʕ ͡° ͜ʖ ° ͡ʔ 
  \_⍾_/   
  ==3==   
  _/ \_   

  (◕‿◕✿)  
  \_⍾_/  
  ==4==   
  _/ \_  

4 little monkeys jumping on the bed...
ʕง ͠° ͟ل͜ ͡°)ʔ  
  \_⏄_/   
  --0--    
  ⎛   ⎞    

 ʕ༼ ◕_◕ ༽ʔ 
  \_⎏_/   
  ++1++   
   ⌋ ⌊    

 ʕ(▀ ⍡ ▀)ʔ 
  \_⎐_/  
  <-2->   
  〈  〉  

ʕ ͡° ͜ʖ ° ͡ʔ 
  \_⍾_/   
  ==3==   
  _/ \_   

3 little monkeys jumping on the bed...
ʕง ͠° ͟ل͜ ͡°)ʔ  
  \_⏄_/   
  --0--    
  ⎛   ⎞    

 ʕ༼ ◕_◕ ༽ʔ 
  \_⎏_/   
  ++1++   
   ⌋ ⌊    

 ʕ(▀ ⍡ ▀)ʔ 
  \_⎐_/  
  <-2->   
  〈  〉  

2 little monkeys jumping on the bed...
ʕง ͠° ͟ل͜ ͡°)ʔ  
  \_⏄_/   
  --0--    
  ⎛   ⎞    

 ʕ༼ ◕_◕ ༽ʔ 
  \_⎏_/   
  ++1++   
   ⌋ ⌊    

1 little monkeys jumping on the bed...
ʕง ͠° ͟ل͜ ͡°)ʔ  
  \_⏄_/   
  --0--    
  ⎛   ⎞    

No more monkeys jumping on the bed
0000000000000000000000000000000000
             THE END              

Hack/Mini-Lab Questions

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

This program is more of an Imperative Programming Style because we are just printing a 2-dimensional array. There are no objects or values being assigned in the program. There are also no classes in the program.

  1. Observe variable assignments. The variables are assigned within the class.

  2. Is each Monkey an object?

Each monkey is not an object because we are not defining objects in a class. We are printing a 2-dimensional array, so each monkey is a different row in the array.

  1. Build an where the monkey is an object versus two-dimensional array. This would be leading into Unit 5 requirements.

My code is shown above. I used a one dimensional array to print the ants horizontally.

  1. Study loops and zero based counting

Zero based counting is a way of numbering in which the initial element of a sequence is assigned the index 0, rather than the index 1 as is typical in everyday non-mathematical or non-programming circumstances. In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.

  1. Study two-dimensional (2D) array references

Each cell of the array is a variable that can hold a value and works like any variable. As with one dimensional arrays, every cell in a 2D array is of the same type. The type can be a primitive type or an object reference type.

  1. Explain different way you can access a 2D array

In Java, when accessing the element from a 2D array using arr[first][second] , the first index can be thought of as the desired row, and the second index is used for the desired column. Just like 1D arrays, 2D arrays are indexed starting at 0 .

/**
 * Class for Monkeys: a 2D array of Ants
 * As well as method to print the Poem
 */
class AntLoop {
    // The area between class definition and the 1st method is where we keep data for object in Java
    String [][] ants;    // 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 AntLoop() {
        //Storing Data in 2D arrays
        ants = new String[][]{   // 2D array above is just a name, "new" makes a container ("object")
                //Ant 0
                {
                        " -(**)- ",      //[0][0] eyes
                        " -(0 )- ",      //[0][1] chin
                        " -(__)- ",       //[0][2] body
                        
                },
                //Ant 1
                {
                        " -(**)- ",      //[1][0] eyes
                        " -(1 )- ",      //[1][1] chin
                        " -(..)- ",      //[1][2] body
                },
                //Ant 2
                {
                        " -(**)- ",       //[2][0] eyes
                        " -(2 )- ",       //[2][1] chin
                        " -(**)- ",       //[2][2] body
                },
                //Ant 3
                {
                        " -(**)- ",        //[3][0] eyes
                        " -(3 )- ",        //[3][1] chin
                        " -(--)- ",        //[3][2] body
                },
                //Ant 4
                {
                        " -(--)- ",          //[4][0] eyes
                        " -(4 )- ",          //[4][1] chin
                        " -(**)- ",          //[4][2] body
                },

        };
    }

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

        // monkeys (non-primitive) defined in constructor knows its length
        int antCount = ants.length; // number of monkeys
        for (int i = antCount; 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 + " the ants go marching one by one... horrah! horrah!");
            System.out.println(i + " the ants go marching one by one... horrah! horrah!");
            System.out.println("the ants go marching one by one, the little one stopped to play the drum");
            System.out.println("and they all go marching down");

            // conditional determining last line of each stanza
            if(i == 1){
                System.out.println("'Put those monkeys right to bed!'");
            }
            else{
                System.out.println("'No more monkeys jumping on the bed!'");
            }

            // start by determining how many horizontal rows for an individual monkey will be in the output
                // in this case, 4 rows for eyes, chin, body and legs
            for(int col = 0; col < ants[col].length; col++){

                // goal is to print first row of all eyes, then print next row of all chins...

                // incrementing by row to specify which ant
                for(int row = 0; row < antCount; row++){

                    // print certain body part
                    System.out.print(ants[row][col]);

                    // separate body part from other monkeys' with tab
                    System.out.print("\t");
                }

                // new line for new body part
                System.out.println();
            }

            System.out.println(); // new stanza

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

        // out of all the loops, prints finishing messages
        System.out.println("No more ants marching up the hill");
        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 AntLoop().printPoem();   // a new monkey list and output in one step
    }

}
AntLoop.main(null);
Marching Ants Poem in Java Loop

5 the ants go marching one by one... horrah! horrah!
5 the ants go marching one by one... horrah! horrah!
the ants go marching one by one, the little one stopped to play the drum
and they all go marching down
'No more monkeys jumping on the bed!'
 -(**)- 	 -(**)- 	 -(**)- 	 -(**)- 	 -(--)- 	
 -(0 )- 	 -(1 )- 	 -(2 )- 	 -(3 )- 	 -(4 )- 	
 -(__)- 	 -(..)- 	 -(**)- 	 -(--)- 	 -(**)- 	

4 the ants go marching one by one... horrah! horrah!
4 the ants go marching one by one... horrah! horrah!
the ants go marching one by one, the little one stopped to play the drum
and they all go marching down
'No more monkeys jumping on the bed!'
 -(**)- 	 -(**)- 	 -(**)- 	 -(**)- 	
 -(0 )- 	 -(1 )- 	 -(2 )- 	 -(3 )- 	
 -(__)- 	 -(..)- 	 -(**)- 	 -(--)- 	

3 the ants go marching one by one... horrah! horrah!
3 the ants go marching one by one... horrah! horrah!
the ants go marching one by one, the little one stopped to play the drum
and they all go marching down
'No more monkeys jumping on the bed!'
 -(**)- 	 -(**)- 	 -(**)- 	
 -(0 )- 	 -(1 )- 	 -(2 )- 	
 -(__)- 	 -(..)- 	 -(**)- 	

2 the ants go marching one by one... horrah! horrah!
2 the ants go marching one by one... horrah! horrah!
the ants go marching one by one, the little one stopped to play the drum
and they all go marching down
'No more monkeys jumping on the bed!'
 -(**)- 	 -(**)- 	
 -(0 )- 	 -(1 )- 	
 -(__)- 	 -(..)- 	

1 the ants go marching one by one... horrah! horrah!
1 the ants go marching one by one... horrah! horrah!
the ants go marching one by one, the little one stopped to play the drum
and they all go marching down
'Put those monkeys right to bed!'
 -(**)- 	
 -(0 )- 	
 -(__)- 	

No more ants marching up the hill
0000000000000000000000000000000000
             THE END