Iteration with 2D Arrays
class CatLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] cats; //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 CatLoop() {
//Storing Data in 2D arrays
cats = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//Cat 0
{
"\\ 1 /\\ ", //[0][0] eyes
" ) ( ') ", //[0][1] chin
" ( / ) ", //[0][2] body
" \\(__)| " //[0][3] legs
},
//Cat 1
{
"\\ 2 /\\ ", //[1][0] eyes
" ) ( ') ", //[1][1] chin
" ( / ) ", //[1][2] body
" \\(__)| " //[1][3] legs
},
//Cat 2
{
"\\ 3 /\\ ", //[2][0] eyes
" ) ( ') ", //[2][1] chin
" ( / ) ", //[2][2] body
" \\(__)| " //[2][3] legs
},
//Cat 3
{
"\\ 4 /\\ ", //[3][0] eyes
" ) ( ') ", //[3][1] chin
" ( / ) ", //[3][2] body
" \\(__)| " //[3][3] legs
},
//Cat 4
{
"\\ 5 /\\ ", //[4][0] eyes
" ) ( ') ", //[4][1] chin
" ( / ) ", //[4][2] body
" \\(__)| " //[4][3] legs
},
//Cat 5
{
"\\ 6 /\\ ", //[5][0] eyes
" ) ( ') ", //[5][1] chin
" ( / ) ", //[5][2] body
" \\(__)| " //[5][3] legs
},
//Cat 6
{
"\\ 7 /\\ ", //[6][0] eyes
" ) ( ') ", //[6][1] chin
" ( / ) ", //[6][2] body
" \\(__)| " //[6][3] legs
},
};
}
/**
* Loop and print cats in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("Oh No the Cats Are Leaving!");
// cats (non-primitive) defined in constructor knows its length
int catCount = cats.length;
for (int i = catCount; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of Cats
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " cats are hanging out, but then another leaves...");
System.out.println();
//how many separate parts are there in a cat cat?
for (int row = 0; row < catCount; row++) { //cycles through "cells" of 2d array
/*cycles through columns to print
each cat part by part, will eventually print entire column*/
for (int col = 0; col < cats[row].length; col++) {
// prints specific part of the cat from the column
System.out.print(cats[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 catCount variable by 1
catCount -= 1;
}
//out of all the loops, prints finishing messages
System.out.println("Oh No, all the Cats have left...");
}
/**
* A Java Driver/Test method that is the entry point for execution
*/
public static void main(String[] args) {
new CatLoop().printPoem(); //a new cat list and output in one step
}
}
CatLoop.main(null);
class CatLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] cats; //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 CatLoop() {
//Storing Data in 2D arrays
cats = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//Cat 0
{
"\\ 1 /\\ ", //[0][0] eyes
" ) ( ') ", //[0][1] chin
" ( / ) ", //[0][2] body
" \\(__)| " //[0][3] legs
},
//Cat 1
{
"\\ 2 /\\ ", //[1][0] eyes
" ) ( ') ", //[1][1] chin
" ( / ) ", //[1][2] body
" \\(__)| " //[1][3] legs
},
//Cat 2
{
"\\ 3 /\\ ", //[2][0] eyes
" ) ( ') ", //[2][1] chin
" ( / ) ", //[2][2] body
" \\(__)| " //[2][3] legs
},
//Cat 3
{
"\\ 4 /\\ ", //[3][0] eyes
" ) ( ') ", //[3][1] chin
" ( / ) ", //[3][2] body
" \\(__)| " //[3][3] legs
},
//Cat 4
{
"\\ 5 /\\ ", //[4][0] eyes
" ) ( ') ", //[4][1] chin
" ( / ) ", //[4][2] body
" \\(__)| " //[4][3] legs
},
//Cat 5
{
"\\ 6 /\\ ", //[5][0] eyes
" ) ( ') ", //[5][1] chin
" ( / ) ", //[5][2] body
" \\(__)| " //[5][3] legs
},
//Cat 6
{
"\\ 7 /\\ ", //[6][0] eyes
" ) ( ') ", //[6][1] chin
" ( / ) ", //[6][2] body
" \\(__)| " //[6][3] legs
},
};
}
/**
* Loop and print cats in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("Oh No the Cats Are Leaving!");
// cats (non-primitive) defined in constructor knows its length
int catCount = 7;
for (int i = catCount; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of Cats
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(" ");
System.out.println(" ");
System.out.println(i + " cats are hanging out, but then another leaves...");
System.out.println(" ");
// printing the cats horizontally
for (int col = 0; col < 4; col++) { //cycles through the columns after each row is completed
for (int row = 0; row < catCount; row++) { //cycles through each row first
System.out.print(cats[row][col] + " ");
}
System.out.println(" ");
}
catCount -= 1;
}
//out of all the loops, prints finishing messages
System.out.println(" ");
System.out.println(" ");
System.out.println("Oh No, all the Cats have left...");
}
/**
* A Java Driver/Test method that is the entry point for execution
*/
public static void main(String[] args) {
new CatLoop().printPoem(); //a new cat list and output in one step
}
}
CatLoop.main(null);
Further Questions
Is this program in more of an Imperative Programming Style or OOP style?
Java is an imperative programming language, so you have to be very specific with what you want the code to do, and not leave it open to interpretation. I think this program is more of an Imperative Programming Style, because you needed to be very specific to make sure the code worked, and Java didn't guess what you wanted based on the goal, you had to drag it there.
What did you Observe about Variable Assignments?
Variables were mostly assigned to moving parts of the program, and mainly for keeping track and counting down the monkeys, along with the monkey rows and columns. Nonessential things like the names of the rows or cols weren't made into variables.
Is Each Monkey an Object?
No, each monkey is not an object, but its parts are separate components in an array. Those parts of the array are called throughout the program and rearranged.
How do 2D Array References work?
Referencing an array or specific parts of it have a standard format of name-of-the-array[row#][col#], where row and col are integers or variables that have int values, like in this program.