Turtles
Turtle enjoyers.
The below code makes use of a 2D Array and prints turtles horizontally. It is later refactored to be a One Dimensional Array of objects.
class TurtleLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] turtles;
/**
* Constructor initializes a 2D array of Turtles
*/
public TurtleLoop() {
//Storing Data in 2D arrays
turtles = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//Turtle 0
{
" _____", //[0][0]
" oo/><><>\\", //[0][1]
"( -)><><><>", //[0][2]
" L|_|L|_|` " //[0][3]
},
//Turtle 1
{
" _____", //[1][0]
" ^^/xxxxx\\", //[1][1]
"( O)xxxxxxx", //[1][2]
" L|_|L|_|` " //[1][3]
},
//Turtle 2
{
" _____", //[2][0]
" uu/wwwww\\", //[2][1]
"( U)wwwwwww", //[2][2]
" L|_|L|_|` " //[2][3]
},
//Turtle 3
{
" _____", //[3][0]
" rr/nnnnn\\", //[3][1]
"( W)nnnnnnn", //[3][2]
" L|_|L|_|` " //[3][3]
},
//Turtle 4
{
" _____", //[4][0]
" cc/ooooo\\", //[4][1]
"( Y)ooooooo", //[4][2]
" L|_|L|_|` " //[4][3]
},
};
}
/**
* Loop and print turtles in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("The Tragedy of the turtle squad");
// turtles (non-primitive) defined in constructor knows its length
int turtleCount = turtles.length;
for (int i = turtleCount; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of Turtles
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " turtle squad members left");
//how many separate parts are there in a turtle turtle?
for (int row = 0; row < turtles[row].length; row++) { //cycles through "cells" of 2d array
/*cycles through columns to print
each turtle part by part, will eventually print entire column*/
for (int col = 0; col < turtleCount; col++) {
// prints specific part of the turtle from the column
System.out.print(turtles[col][row] + " ");
}
//this new line gives separation between each turtle
System.out.println();
}
//countdown for poem, decrementing turtleCount variable by 1
turtleCount -= 1;
}
//out of all the loops, prints finishing messages
System.out.println("That was the tragedy of the turtle squad :(");
System.out.println("0000000000000000000000000000000000");
}
/**
* A Java Driver/Test method that is the entry point for execution
*/
public static void main(String[] args) {
new TurtleLoop().printPoem(); //a new monkey list and output in one step
}
}
TurtleLoop.main(null);
class Turtle{
public Turtle(String turtleName, String[] turtleDraw){
this.name = turtleName;
this.draw = turtleDraw;
}
String name;
String[] draw;
public void draw(){
for(int row = 0; row < draw.length; row++){
System.out.println(draw[row]);
}
System.out.println();
}
public static void main(String[] args){
String[] neutral = {
" _____", //[0][0]
" oo/><><>\\", //[0][1]
"( -)><><><>", //[0][2]
" L|_|L|_|` " //[0][3]
};
String[] surprised = {
" _____", //[1][0]
" ^^/xxxxx\\", //[1][1]
"( O)xxxxxxx", //[1][2]
" L|_|L|_|` " //[1][3]
};
String[] happy = {
" _____", //[2][0]
" uu/wwwww\\", //[2][1]
"( U)wwwwwww", //[2][2]
" L|_|L|_|` " //[2][3]
};
String[] excited = {
" _____", //[3][0]
" rr/nnnnn\\", //[3][1]
"( W)nnnnnnn", //[3][2]
" L|_|L|_|` " //[3][3]
};
Turtle bob = new Turtle("bob", neutral);
Turtle rick = new Turtle("rick", surprised);
Turtle ted = new Turtle("ted", happy);
Turtle john = new Turtle("john", excited);
Turtle[] turtles = {bob, rick, ted, john};
for(int i = 0; i < turtles.length; i++){
System.out.println("This is " + turtles[i].name + ": ");
turtles[i].draw();
}
}
}
Turtle.main(null);