FRQ + Iteration ASCII
A simple blog containing information concerning FRQ 2 on the 2022 AP CSA Exam and some knowledge on Iterations
- Here is an example of Iterations, using an implementation from a childhood song.
- Answering some of the Hacks questions
public class Textbook extends Book {
private int edition;
public Textbook(String bookTitle, double bookPrice, int bookEdition) {
super(bookTitle, bookPrice);
edition = bookEdition;
}
public int getEdition() {
returns edition;
}
public String getBookInfo(){
return super.getBookInfo + "-" + edition;
}
public boolean canSubstituteFor(Textbook book2){
return this.getTitle().equals(book2.getTitle()) && this.getEdition() >= book2.getEdition();
}
}
/* Example of Code (writeup about below)
* Creator: Nighthawk Coding Society
* Mini Lab Name: Hello Series,featuring Monkey Jumpers
*/
/**
* Class for Monkeys: a 2D array of Monkeys
* As well as method to print the Poem
*/
class BasketballLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] basketballs; //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 BasketballLoop() {
//Storing Data in 2D arrays
basketballs = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//Hoop 0
{
" __| ",
" vv`| ",
" | ",
" | ",
" | ",
" | "
},
//Hoop 1
{
" __| ",
" vv`| ",
" | ",
" | ",
" | ",
" | "
},
//Hoop 2
{
" __| ",
" vv`| ",
" | ",
" | ",
" | ",
" | "
},
//Hoop 3
{
" __| ",
" vv`| ",
" | ",
" | ",
" | ",
" | "
},
//Hoop 4
{
" __| ",
" vv`| ",
" | ",
" | ",
" | ",
" | "
},
};
}
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("Basketball Hoops");
int hoopCount = basketballs.length;
for (int i = hoopCount; i >= 1; i--) //loops through 2D array length backwards
{
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " basketball hoops...");
//how many separate parts are there in a monkey monkey?
for (int row = 0; row < hoopCount; 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 < basketballs[row].length; col++) {
// prints specific part of the monkey from the colum
System.out.print(basketballs[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 monkeyCount variable by 1
monkeyCount -= 1;
}
//out of all the loops, prints finishing messages
System.out.println("No more basketball hoops!");
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 MonkeyLoop().printPoem(); //a new monkey list and output in one step
}
}
MonkeyLoop.main(null);
Answering some of the Hacks questions
-
I believe the program is more of Object Oriented Programming, through the use of parent classes and child classes in the code.
-
Each monkey is not an object; defined using an if statement and for loop.
-
In Java, 2D arrays are stored as arrays of arrays. Therefore, the way 2D arrays are declared is similar 1D array objects. 2D arrays are declared by defining a data type followed by two sets of square brackets.
-
A two-dimensional array is similar to a one-dimensional array, but it can be visualised as a grid (or table) with rows and columns. Many games use two dimensional arrays to plot the visual environment of a game.