Iteration with 2D Array
/**
* Class for apples: a 2D array of apples
* As well as method to print the Poem
*/
class AppleLoop {
String [][] apples;
/**
* Constructor initializes a 2D array of apples
*/
public AppleLoop() {
//Storing Data in 2D arrays
apples = new String[][]{
//Apple 0
{
" ,--./,-. ", //[0][0]
" / | ", //[0][1]
" | | ", //[0][2]
" | / ", //[0][3]
" `._,._,' " //[0][4]
},
//Apple 1
{
" ,--./,-. ", //[1][0]
" / # | ", //[1][1]
" | | ", //[1][2]
" | / ", //[1][3]
" `._,._,' " //[1][4]
},
//Apple 2
{
" ,--./,-. ",
" / # | ",
" | @ | ",
" | / ",
" `._,._,' "
},
//Apple 3
{
" ,--|,-(~) ", //[3][0]
" / # )=) ",
" ( (_/ ) ",
" | / ",
" `._,._,' "
},
//Apple 4
{
" ,--./,-. ", //[4][0]
" /,-._.--~| ",
" __} {__ ",
" |`-._,-`-, ",
" `._,._,' "
},
};
}
public void printPoem() {
System.out.println();
System.out.println("You forget to eat your apple, what will happen?");
System.out.println();
int appleCount = apples.length; //.length number of rows
int row = 0; //not included in for loop
for (int i = 4; i < appleCount && i != -1; i--) //i != -1 because we want to stop the for loop after printing the fifth apple, and we starts with i = 4, so we need to stop when i is =-1
{
System.out.println("The apple:");
System.out.println(" ");
for (int col = 0; col < apples[row].length; col++) {
// prints specific part of the apple from the column when row is 0/1/2/3/4
System.out.print(apples[row][col] + " ");
//this is new line between separate parts
System.out.println();
}
row += 1; // print everything that's on row 1/2/3/4, which are Apple 1/2/3/4
System.out.println();
}
//finishing messages
System.out.println("This is the story of an uneaten apple.");
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 AppleLoop().printPoem(); //a new apple list and output in one step
}
}
AppleLoop.main(null);
Version 1 - changing data in the 2d array
/**
* Class for apples: a 2D array of apples
* As well as method to print the Poem
*/
class AppleLoop {
String [][] apples;
/**
* Constructor initializes a 2D array of apples
*/
public AppleLoop() {
//Storing Data in 2D arrays
apples = new String[][]{
{
" ,--./,-. ", //[0][0]
" ,--./,-. ", //[0][1]
" ,--./,-. ", //[0][2]
" ,--|,-(~) ", //[0][3]
" ,--./,-. " //[0][4]
},
{
" / | ", //[1][0]
" / # | ",
" / # | ",
" / # )=) ",
" /,-._.--~| "
},
{
" | | ", //[2][0]
" | | ",
" | @ | ",
" ( (_/ ) ",
" __} {__ "
},
{
" | / ", //[3][0]
" | / ",
" | / ",
" | / ",
" |`-._,-`-, "
},
{
" `._,._,' ", //[4][0]
" `._,._,' ",
" `._,._,' ",
" `._,._,' ",
" `._,._,' "
},
};
}
public void printPoem() {
System.out.println();
System.out.println("You forget to eat your apple, what will happen?");
System.out.println();
System.out.println("The apple:");
System.out.println(" ");
int appleCount = apples.length; //.length number of rows
int row = 0; //not included in for loop
for (int i = 4; i < appleCount && i != -1; i--)
{
for (int col = 0; col < apples[row].length; col++) {
// prints specific part of the apple from the column
System.out.print(apples[row][col] + " ");
}
row += 1; //printing second/third/fourth/fifth row
System.out.println(); //break between each row
}
System.out.println(); // break between apples and the final messages
//finishing messages
System.out.println("This is the story of an uneaten apple.");
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 AppleLoop().printPoem(); //a new apple list and output in one step
}
}
AppleLoop.main(null);
Version 2 - without changing data in the 2d array
/**
* Class for apples: a 2D array of apples
* As well as method to print the Poem
*/
class AppleLoop {
String [][] apples;
/**
* Constructor initializes a 2D array of apples
*/
public AppleLoop() {
//Storing Data in 2D arrays
apples = new String[][]{
//Apple 0
{
" ,--./,-. ", //[0][0]
" / | ", //[0][1]
" | | ", //[0][2]
" | / ", //[0][3]
" `._,._,' " //[0][4]
},
//Apple 1
{
" ,--./,-. ", //[1][0]
" / # | ", //[1][1]
" | | ", //[1][2]
" | / ", //[1][3]
" `._,._,' " //[1][4]
},
//Apple 2
{
" ,--./,-. ",
" / # | ",
" | @ | ",
" | / ",
" `._,._,' "
},
//Apple 3
{
" ,--|,-(~) ", //[3][0]
" / # )=) ",
" ( (_/ ) ",
" | / ",
" `._,._,' "
},
//Apple 4
{
" ,--./,-. ", //[4][0]
" /,-._.--~| ",
" __} {__ ",
" |`-._,-`-, ",
" `._,._,' "
},
};
}
public void printPoem() {
System.out.println();
System.out.println("You forget to eat your apple, what will happen?");
System.out.println();
System.out.println("The apple:");
System.out.println(" ");
int appleCount = apples.length; //.length number of rows
int col = 0; //change row to col
for (int i = 4; i < appleCount && i != -1; i--) //i != -1 because we want to stop the for loop after printing the fifth apple, and we starts with i = 4, so we need to stop when i is =-1
{
for (int row = 0; row < apples.length; row++) { //change col to row
// prints specific part of the apple from the column when row is 0/1/2/3/4
System.out.print(apples[row][col] + " ");
//this is new line between separate parts
System.out.print("\t"); //tab between each heads of the apples instead of break
}
col += 1; // print everything that's on col 1/2/3/4
System.out.println(); //break between the heads/necks...different parts of the apples
}
System.out.println();
//finishing messages
System.out.println("This is the story of an uneaten apple.");
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 AppleLoop().printPoem(); //a new apple list and output in one step
}
}
AppleLoop.main(null);
Version 3 - not changing the 2d array
/**
* Class for apples: a 2D array of apples
* As well as method to print the Poem
*/
class AppleLoop {
String [][] apples;
/**
* Constructor initializes a 2D array of apples
*/
public AppleLoop() {
//Storing Data in 2D arrays
apples = new String[][]{
//Apple 0
{
" ,--./,-. ", //[0][0]
" / | ", //[0][1]
" | | ", //[0][2]
" | / ", //[0][3]
" `._,._,' " //[0][4]
},
//Apple 1
{
" ,--./,-. ", //[1][0]
" / # | ", //[1][1]
" | | ", //[1][2]
" | / ", //[1][3]
" `._,._,' " //[1][4]
},
//Apple 2
{
" ,--./,-. ",
" / # | ",
" | @ | ",
" | / ",
" `._,._,' "
},
//Apple 3
{
" ,--|,-(~) ", //[3][0]
" / # )=) ",
" ( (_/ ) ",
" | / ",
" `._,._,' "
},
//Apple 4
{
" ,--./,-. ", //[4][0]
" /,-._.--~| ",
" __} {__ ",
" |`-._,-`-, ",
" `._,._,' "
},
};
}
public void printPoem() {
System.out.println();
System.out.println("You forget to eat your apple, what will happen?");
System.out.println();
System.out.println("The apple:");
System.out.println(" ");
int appleCount = apples.length; //.length number of rows
int row = 0; //not included in for loop
for (int i = 4; i < appleCount && i != -1; i--) //i != -1 because we want to stop the for loop after printing the fifth apple, and we starts with i = 4, so we need to stop when i is =-1
{
for (int col = 0; col < apples[row].length; col++) {
// prints specific part of the apple from the column when row is 0/1/2/3/4
System.out.print(apples[col][row] + " "); //switch col and row
//this is new line between separate parts
System.out.print(" ");
}
row += 1; // print everything that's on row 1/2/3/4, which are Apple 1/2/3/4
System.out.println();
}
//finishing messages
System.out.println("This is the story of an uneaten apple.");
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 AppleLoop().printPoem(); //a new apple list and output in one step
}
}
AppleLoop.main(null);
/**
* Class for apples: a 2D array of apples
* As well as method to print the Poem
*/
class AppleLoop {
String [][] apples;
/**
* Constructor initializes a 2D array of apples
*/
public AppleLoop() {
//Storing Data in 2D arrays
apples = new String[][]{
//Apple 0
{
" ,--./,-. ", //[0][0]
" / | ", //[0][1]
" | | ", //[0][2]
" | / ", //[0][3]
" `._,._,' " //[0][4]
},
//Apple 1
{
" ,--./,-. ", //[1][0]
" / # | ", //[1][1]
" | | ", //[1][2]
" | / ", //[1][3]
" `._,._,' " //[1][4]
},
//Apple 2
{
" ,--./,-. ",
" / # | ",
" | @ | ",
" | / ",
" `._,._,' "
},
//Apple 3
{
" ,--|,-(~) ", //[3][0]
" / # )=) ",
" ( (_/ ) ",
" | / ",
" `._,._,' "
},
//Apple 4
{
" ,--./,-. ", //[4][0]
" /,-._.--~| ",
" __} {__ ",
" |`-._,-`-, ",
" `._,._,' "
},
};
}
public void printPoem() {
System.out.println();
System.out.println("You forget to eat your apple, what will happen?");
System.out.println();
int appleCount = apples.length; //.length number of rows
int appleNum = 1;
for (int i = 4; i < appleCount && i != -1; i--)
{
System.out.println("The apple:");
System.out.println(" ");
for (int row = 0; row < appleNum; row++) { //if appleNum += 1 once, then 0(row) < 2(1+1), will print Apple 0 & 1
for (int col = 0; col < apples[row].length; col++) {
// prints specific part of the apple from the column
System.out.print(apples[row][col] + " ");
//this is new line between separate parts
System.out.println();
}
System.out.println();
}
appleNum += 1;
}
//finishing messages
System.out.println("This is the story of an uneaten apple.");
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 AppleLoop().printPoem(); //a new apple list and output in one step
}
}
AppleLoop.main(null);