2d Arrays Hacks
2d Arrays
class AnimalLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] animals; //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 AnimalLoop() {
//Storing Data in 2D arrays
animals = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//cat 0
{
" /\\_/\\ ",
" ( o o ) ",
" ==_Y_== ",
" `-' "
},
//bird 1
{
" /\\ /\\ ",
" ((ovo))",
" ():::()",
" VVV "
},
//spider 2
{
" || ||", //[2][0]
" \\\\()// ",
" //(__)\\\\ ",
" || || "
},
//cow 3
{
" ((...))" , //[3][0]
" ( O O ) ",
" \\ / ",
" (`_`) "
},
{
" __ (\\_ ", //[4][0]
" (_ \\ ( '> ", //[4][1]
" ) \\/_)= ", //[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(" My Farm ");
int animalCount = animals.length;
//how many separate parts are there in a monkey monkey?
for (int row = 0; row < animalCount; 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 < animals[row].length; col++) {
// prints specific part of the monkey from the column
System.out.print(animals[row][col] + " ");
//this is new line between separate parts
System.out.println();
}
//this new line gives separation between stanza of poem
System.out.println();
}
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 AnimalLoop().printPoem(); //a new monkey list and output in one step
}
}
AnimalLoop.main(null);
import java.util.*;
public class AnimalLoop {
String[][] animals = {
//cat 0
{
" /\\_/\\ ",
" ( o o ) ",
" ==_Y_== ",
" `-' "
},
//bird 1
{
" /\\ /\\ ",
" ((ovo))",
" ():::()",
" VVV "
},
//spider 2
{
" || ||", //[2][0]
" \\\\()// ",
" //(__)\\\\ ",
" || || "
},
//cow 3
{
" ((...)) " , //[3][0]
" ( O O ) ",
" \\ / ",
" (`_`) "
},
//squirrel 4
{
" __ (\\_ ", //[4][0]
" (_ \\ ( '> ", //[4][1]
" ) \\/_)= ", //[4][2]
" (_(_ )_ " //[4][3]
},
};
public void printAnimals () {
for (int animalCount = animals.length; animalCount >= 1; animalCount--) {
for (int i = 0; i<4; i++) {
for (int j = 0; j<animalCount; j++) {
System.out.print(animals[j][i] + " ");
}
System.out.println();
}
System.out.println();
}
}
}
AnimalLoop animal = new AnimalLoop();
animal.printAnimals();
import java.util.*;
public class AnimalLoop {
String[][] animals = {
//cat 0
{
" /\\_/\\ ",
" ( o o ) ",
" ==_Y_== ",
" `-' "
},
//bird 1
{
" /\\ /\\ ",
" ((ovo))",
" ():::()",
" VVV "
},
//spider 2
{
" || ||", //[2][0]
" \\\\()// ",
" //(__)\\\\ ",
" || || "
},
//cow 3
{
" ((...)) " , //[3][0]
" ( O O ) ",
" \\ / ",
" (`_`) "
},
//squirrel 4
{
" __ (\\_ ", //[4][0]
" (_ \\ ( '> ", //[4][1]
" ) \\/_)= ", //[4][2]
" (_(_ )_ " //[4][3]
},
};
public void printBackwards () {
for (int row = animals.length-1; row>=0; row--) {
for (int col = 0; col<animals[0].length; col++){
System.out.println(animals[row][col]+ " ");
}
System.out.println();
}
}
}
AnimalLoop animal = new AnimalLoop();
animal.printBackwards();