Iteration with 2D Array
Focus is on iteration. This example uses 2D array with simple ASCII art. The idea of this example was to incorporate ASCII art with a nursery rhyme.
/*
* 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);
Hacks (Mini-lab)
Build you own Jupyter Notebook. Feel free to use any ASCII art of your choice, there are some much better ones here. My little guys were made up out of my head while looking at unicode characters.
- Print monkeys horizontally versus vertically.
- Build more or entire rhyme for the "Monkey Jumpers" countdown poem
- Add names or other properties to the monkeys
In you notebook, illustrate or answer some of these questions.
- Is this program in more of an Imperative Programming Style or OOP style? Explain.
- Observe variable assignments.
- Is each Monkey an object?
- Build an where the monkey is an object versus two-dimensional array. This would be leading into Unit 5 requirements.
- Study loops and zero based counting
- Study two-dimensional (2D) array references
- Explain different way you can access a 2D array
/*
* 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 printPoemVertical() {
//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 ");
}
public void printPoemHorizontal() {
int monkeyCount = monkeys.length;
for (int i = monkeyCount; i > 0; i--) {
System.out.println(i + " little monkeys jumping on the bed");
for (int col = 0; col < monkeys[0].length; col++) {
for (int row = 0; row < i; row++) {
System.out.print(monkeys[row][col] + "\t");
}
System.out.println();
}
if (i > 1) {
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!\"");
System.out.println();
} else {
System.out.println("He fell off and bumped his head");
System.out.println("Mama called the doctor and the doctor said");
System.out.println("\"Put those monkeys right to bed!\"");
System.out.println();
}
}
}
/**
* A Java Driver/Test method that is the entry point for execution
*/
public static void main(String[] args) {
new MonkeyLoop().printPoemHorizontal(); //a new monkey list and output in one step
}
}
MonkeyLoop.main(null);
/*
* 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 ");
}
public void printPoemBackwards() {
//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 = 1; i <= monkeyCount; 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 < i; 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
}
//out of all the loops, prints finishing messages
System.out.println("Too many 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().printPoemBackwards(); //a new monkey list and output in one step
}
}
MonkeyLoop.main(null);
Object Orientated Hell
For my final trick, I shall (try) to make an object orientated version of the monkey story. Except it's not monkeys this time, but dinosaurs getting killed by meteorites. Made (mostly) from scratch, this version will involve creating individual objects, each with a list of strings making up a dino.
import java.util.ArrayList;
public class Dinos {
// Stores all the dinos
private static ArrayList<String[]> dinoList = new ArrayList<String[]>();
// Stores individual dino for each object
private String[] dino;
public Dinos(String[] dino) {
this.dino = dino;
dinoList.add(dino);
}
// Prints poem
public static void printPoem() {
int dinoCount = dinoList.size();
for (int i = dinoCount; i > 0; i--) {
System.out.println(i + " dinos strolling around");
for (int col = 0; col < dinoList.get(0).length; col++) {
for (int row = 0; row < i; row++) {
System.out.print(dinoList.get(row)[col] + "\t");
}
System.out.println();
}
if (i > 1) {
System.out.println("A meteor came down and bumped a head");
System.out.println("Uh Oh!");
System.out.println("Now one is dead!");
System.out.println();
} else {
System.out.println("A meteor came down and bumped a head");
System.out.println("Uh Oh!");
System.out.println("Now all are dead!");
System.out.println();
}
}
}
// Prints individual dino of specific object
public void printDino() {
for (String bodyPart : this.dino) {
System.out.println(bodyPart);
}
}
// Two methods to get rid of monkey - by default gets rid of last one, with argument it removes at specified index
public static void removeDino() {
dinoList.remove(dinoList.size() - 1);
}
public static void removeDino(int i) {
dinoList.remove(i);
}
public static void main(String[] args) {
Dinos dino0 = new Dinos(new String[]{
" __ ",
" / _)",
" _/\\/\\/\\_/ /",
" _| / ",
" _| ( | ( | ",
" /__.-'|_|--|_| ",
" Jimmy "
});
Dinos dino1 = new Dinos(new String[]{
" __ ",
" / _)",
" _.----._/ / ",
" _| / ",
" _| ( | ( | ",
" /__.-'|_|--|_| ",
" Jeremy "
});
Dinos dino2 = new Dinos(new String[]{
" __ ",
" / _)",
" _/\\/\\/\\_/ /",
" _| / ",
" _| ( | ( | ",
" /__.-'|_|--|_| ",
" Johnny "
});
Dinos dino3 = new Dinos(new String[]{
" __ ",
" / _)",
" _.----._/ / ",
" _| / ",
" _| ( | ( | ",
" /__.-'|_|--|_| ",
" Jackson "
});
Dinos dino4 = new Dinos(new String[]{
" __ ",
" / _)",
" _/\\/\\/\\_/ /",
" _| / ",
" _| ( | ( | ",
" /__.-'|_|--|_| ",
" James "
});
Dinos.printPoem();
}
}
Dinos.main(null);
So yeah.
As you can see it's a pretty cool conversion to a more object-orientated version, alllowing the programmer to add more monkeys pretty easily by creating another object. Although I literally copied and pasted the code to print the actual poem, most of the new stuff comes from the static 2d ArrayList, which adds the monkey from each new instance to it, making it easy to edit. You can also get rid of a monkey at the very end (or at a specific index) and you can print out the individual monkey stored in one of the objects. Pretty efficient in my opinion.
Good job me.