Rohan G Iteration Mini-Lab
Week 4 Iteration Mini-Lab
/*
* Creator: Nighthawk Coding Society
* Mini Lab Name: Hello Series,featuring Cat Jumpers
*/
/**
* Class for Cats: a 2D array of Cats
* As well as method to print the Poem
*/
class CatLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] Cats; //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 Cats
*/
public CatLoop() {
//Storing Data in 2D arrays
Cats = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//Cat 0
{
" /\\_/\\ ",
" ( o.o ) ",
" > ^ < ",
},
//Cat 1
{
" /\\_/\\ ",
" ( o o ) ",
" ==_Y_== ",
" `-' ",
},
//Cat 2
{
" |\\__/,| (`\\", //[2][0]
" |o o |_ ) )",
" -(((---(((--------",
},
//Cat 3
{
"|\\---/|", //[3][0]
"| o_o | ",
" \\_^_/ ",
},
//Cat 4
{
" |\\ _,,,---,,_ ", //[4][0]
" ZZZzz /,`.-'`' -. ;-;;,_ ", //[4][1]
" |,4- ) )-,_. ,\\ ( `'-' ", //[4][2]
" '---''(_/--' `-'\\_) " //[4][3]
},
};
}
/**
* Loop and print Cats in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("Catown Poem in Java Loopy");
// Cats (non-primitive) defined in constructor knows its length
int CatCount = Cats.length;
for (int i = CatCount; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of Cats
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " left in Catown...");
//how many separate parts are there in a Cat Cat?
for (int row = 0; row < CatCount; row++) { //cycles through "cells" of 2d array
/*cycles through columns to print
each Cat part by part, will eventually print entire column*/
for (int col = 0; col < Cats[row].length; col++) {
// prints specific part of the Cat from the column
System.out.print(Cats[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 CatCount variable by 1
CatCount -= 1;
}
//out of all the loops, prints finishing messages
System.out.println("No more Cats in CATOWN");
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 CatLoop().printPoem(); //a new Cat list and output in one step
}
}
CatLoop.main(null);
class CatLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] cats; //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 Cats
*/
public CatLoop() {
//Storing Data in 2D arrays
cats = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//Cat 0
{
" /\\_/\\ ",
" ( o.o ) ",
" > ^ < ",
},
//Cat 1
{
" /\\_/\\ ",
" ( o o ) ",
" ==_Y_== ",
" `-' ",
},
//Cat 2
{
" |\\__/,| (`\\", //[2][0]
" |o o |_ ) )",
" -(((---(((--------",
},
//Cat 3
{
" |\\---/|", //[3][0]
" | o_o | ",
" \\_^_/ ",
},
//Cat 4
{
" |\\ _,,,---,,_ ", //[4][0]
" ZZZzz /,`.-'`' -. ;-;;,_ ", //[4][1]
" |,4- ) )-,_. ,\\ ( `'-' ", //[4][2]
" '---''(_/--' `-'\\_) " //[4][3]
},
};
}
public void HorizontalCats() {
int catCount = cats.length;
int i = catCount;
System.out.println(i + " little cats in CATOWN");
for (int col = 0; col < cats[0].length; col++) {
for (int row = 0; row < i; row++) {
System.out.print(cats[row][col] + "\t" );
}
System.out.println();
}
}
/**
* A Java Driver/Test method that is the entry point for execution
*/
public static void main(String[] args) {
new CatLoop().HorizontalCats(); //a new cat list and output in one step
}
}
CatLoop.main(null);
class CatLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] cats; //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 Cats
*/
public CatLoop() {
//Storing Data in 2D arrays
cats = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//Cat 0
{
" /\\_/\\ ",
" ( o.o ) ",
" > ^ < ",
},
//Cat 1
{
" /\\_/\\ ",
" ( o o ) ",
" ==_Y_== ",
" `-' ",
},
//Cat 2
{
" |\\__/,| (`\\", //[2][0]
" |o o |_ ) )",
" -(((---(((--------",
},
//Cat 3
{
" |\\---/|", //[3][0]
" | o_o | ",
" \\_^_/ ",
},
//Cat 4
{
" |\\ _,,,---,,_ ", //[4][0]
" ZZZzz /,`.-'`' -. ;-;;,_ ", //[4][1]
" |,4- ) )-,_. ,\\ ( `'-' ", //[4][2]
" '---''(_/--' `-'\\_) " //[4][3]
},
};
}
public void CatBackwards() {
//begin the poem
System.out.println();
System.out.println("Cats in Catown backwards");
// cats (non-primitive) defined in constructor knows its length
int catCount = cats.length;
for (int i = 1; i <= catCount; i++) //loops through 2D array length backwards
{
//this print statement shows current count of Cats
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " little cats jumping on the bed...");
//how many separate parts are there in a cat cat?
for (int row = 0; row < i; row++) { //cycles through "cells" of 2d array
/*cycles through columns to print
each cat part by part, will eventually print entire column*/
for (int col = 0; col < cats[row].length; col++) {
// prints specific part of the cat from the column
System.out.print(cats[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 catCount variable by 1
}
//out of all the loops, prints finishing messages
System.out.println("5 Cats in CATOWN");
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 CatLoop().CatBackwards(); //a new cat list and output in one step
}
}
CatLoop.main(null);
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 Cat an object?
- Build an where the Cat 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
- This program is a lot more imperative. The cats are not stored as objects, and are just arrays stored as Strings not objects. While the Strings are not primitives, they still are not objects
- We access arrays through the rows and columns they appear in