class AppleLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] apples; //2D Array: AP CSA Unit 8: 2D array of strings
//2D array is like a grid [x][y]
/**
* Constructor initializes a 2D array of Apples
*/
public AppleLoop() {
//Storing Data in 2D arrays
apples = new String[][]{
//Apple 0
{
" ,--./,-.", // Apple
" [ # ] ",
"| 1 |",
" [ ] ",
" `._,._,'"
},
//Apple 1
{
" ,--./,-.",
" [ # ] ",
"| 2 |",
" [ ] ",
" `._,._,'"
},
//Apple 2
{
" ,--./,-.",
" [ # ] ",
"| 3 |",
" [ ] ",
" `._,._,'"
},
//Apple 3
{
" ,--./,-.",
" [ # ] ",
"| 4 |",
" [ ] ",
" `._,._,'' "
},
//Apple 4
{
" ,--./,-.",
" [ # ] ",
"| 5 |",
" [ ] ",
" `._,._,'' "
},
};
}
/**
* Loop and print apples in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("Missing Apple Poem");
// apples (non-primitive) defined in constructor knows its length
int appleCount = apples.length;
for (int i = appleCount; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of Apples
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " apples on the table...");
//how many separate parts are there in a apple apple?
for (int row = 0; row < appleCount; row++) { //cycles through "cells" of 2d array
/*cycles through columns to print
each apple part by part, will eventually print entire column*/
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();
}
//this new line gives separation between stanza of poem
System.out.println("");
}
//countdown for poem, decrementing appleCount variable by 1
appleCount -= 1;
}
//out of all the loops, prints finishing messages
System.out.println(" No more apples are left!");
System.out.println("----------------------------------");
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);