/*
* 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 MonkeyLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] Monkeys; //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 MonkeyLoop() {
//Storing Data in 2D arrays
Monkeys = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//Monkey 0
{
"ʕง ͠° ͟ل͜ ͡°)ʔ ", //[0][0] eyes
" \\_⏄_/ ", //[0][1] chin
" --0-- ", //[0][2] body
" ⎛ ⎞ " //[0][3] legs
},
//Monkey 1
{
" ʕ༼ ◕_◕ ༽ʔ", //[1][0]
" \\_⎏_/ ",
" ++1++ ",
" ⌋ ⌊ "
},
//Monkey 2
{
" ʕ(▀ ⍡ ▀)ʔ", //[2][0]
" \\_⎐_/ ",
" <-2-> ",
" 〈 〉 "
},
//Monkey 3
{
"ʕ ͡° ͜ʖ ° ͡ʔ", //[3][0]
" \\_⍾_/ ",
" ==3== ",
" _/ \\_ "
},
//Monkey 4
{
" (◕‿◕✿) ", //[4][0]
" \\_⍾_/ ", //[4][1]
" ==4== ", //[4][2]
" _/ \\_ " //[4][3]
},
};
}
/**
* Loop and print Monkeys in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("Monkey Jumpers Poem in Java Loopy");
// Monkeys (non-primitive) defined in constructor knows its length
int MonkeyCount = Monkeys.length;
for (int i = MonkeyCount; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of Monkeys
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " little Monkeys jumping on the bed...");
//how many separate parts are there in a Monkey Monkey?
for (int row = 0; row < MonkeyCount; 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 < Monkeys[row].length; col++) {
// prints specific part of the Monkey from the column
System.out.print(Monkeys[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 Monkeys jumping on the bed");
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);
/*
* 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 MonkeyLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] Monkeys; //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 MonkeyLoop() {
//Storing Data in 2D arrays
Monkeys = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
//Monkey 0
{
"ʕง ͠° ͟ل͜ ͡°)ʔ ", //[0][0] eyes
" \\_⏄_/ ", //[0][1] chin
" --0-- ", //[0][2] body
" ⎛ ⎞ " //[0][3] legs
},
//Monkey 1
{
" ʕ༼ ◕_◕ ༽ʔ", //[1][0]
" \\_⎏_/ ",
" ++1++ ",
" ⌋ ⌊ "
},
//Monkey 2
{
" ʕ(▀ ⍡ ▀)ʔ", //[2][0]
" \\_⎐_/ ",
" <-2-> ",
" 〈 〉 "
},
//Monkey 3
{
"ʕ ͡° ͜ʖ ° ͡ʔ", //[3][0]
" \\_⍾_/ ",
" ==3== ",
" _/ \\_ "
},
//Monkey 4
{
" (◕‿◕✿) ", //[4][0]
" \\_⍾_/ ", //[4][1]
" ==4== ", //[4][2]
" _/ \\_ " //[4][3]
},
};
}
/**
* Loop and print Monkeys in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
//begin the poem
System.out.println();
System.out.println("Monkey Jumpers Poem in Java Loopy");
// Monkeys (non-primitive) defined in constructor knows its length
int MonkeyCount = Monkeys.length;
for (int i = MonkeyCount; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of Monkeys
// concatenation (+) of the loop variable and string to form a countdown message
System.out.println(i + " little Monkeys jumping on the bed...");
//how many separate parts are there in a Monkey Monkey?
for (int col = 0; col < 4; col++) { //cycles through "cells" of 2d array
/*cycles through columns to print
each Monkey part by part, will eventually print entire column*/
for (int row = 0; row < MonkeyCount; row++) {
// prints specific part of the Monkey from the column
System.out.print(Monkeys[row][col] + " ");
//this is new line between separate parts
}
//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 Monkeys jumping on the bed");
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);
/*
* 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 MonkeyLoop {
//The area between class definition and the 1st method is where we keep data for object in Java
String [][] Monkeys; //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 MonkeyLoop() {
//Storing Data in 2D arrays
Monkeys = new String[][]{ //2D array above is just a name, "new" makes a container ("object")
{
// Monkey set one
"----------", // [0][1]
"| |", // [0][2]
"| o |", // [0][3]
"| |", // [0][4]
"----------" // [0][5]
},
{
"----------", // [1][0]
"| o o |",
"| |",
"| o o |",
"----------"
},
{
"----------", // [2][0]
"| o o |",
"| o |",
"| o o |",
"----------"
},
{
"----------", // [2][0]
"| o |",
"| o |",
"| o |",
"----------"
},
{
"----------", // [3][0]
"| o o |",
"| o o |",
"| o o |",
"----------"
},
{
"----------", // [4][0]
"| o |",
"| |",
"| o |",
"----------"
},
};
}
/**
* Loop and print Monkeys in array
* ... repeat until you reach zero ...
*/
public void printPoem() {
Scanner userInput = new Scanner(System.in); // Create a Scanner object
System.out.println("How many times do you want to roll? (Only values 1-6)");
System.out.println();
int rollNum = userInput.nextInt(); // Read user input
System.out.println("Rolling " + rollNum + " times..."); // Output user input
// Monkeys (non-primitive) defined in constructor knows its length
int MonkeyCount = Monkeys.length;
for (int i = rollNum; i >= 1; i--) //loops through 2D array length backwards
{
//this print statement shows current count of Monkeys
// concatenation (+) of the loop variable and string to form a countdown message
//how many separate parts are there in a Monkey Monkey?
for (int row = 0; row < (rollNum-1); row++) { //cycles through "cells" of 2d array
// prints specific part of the Monkey from the column
System.out.print(Monkeys[row][0] + " ");
System.out.println();
System.out.print(Monkeys[row][1] + " ");
System.out.println();
System.out.print(Monkeys[row][2] + " ");
System.out.println();
System.out.print(Monkeys[row][3] + " ");
System.out.println();
System.out.print(Monkeys[row][4] + " ");
System.out.println();
rollNum -= 1;
}
//this new line gives separation between stanza of poem
System.out.println();
}
//countdown for poem, decrementing MonkeyCount variable by 1
//out of all the loops, prints finishing messages
int min = 1;
int max = 6;
int random_int = (int)Math.floor(Math.random()*(max-min+1)+min);
System.out.println(Monkeys[random_int-1][0]);
System.out.println(Monkeys[random_int-1][1]);
System.out.println(Monkeys[random_int-1][2]);
System.out.println(Monkeys[random_int-1][3]);
System.out.println(Monkeys[random_int-1][4]);
System.out.println("You landed on... " + random_int);
}
/**
* 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);
Questions
Is this program in more of an Imperative Programming Style or OOP style? Explain.
This program is more of an Imperative Programming Style because each step is laid out, and each step had to be written out. There are no methods, and the monkeys are not objects because they don't have any attributes or behaviors. Each step is given on how to get the result.
Object-oriented programming
- Computer programming model that organizes software design around objects (data) rather than functions/logic
- Objects: data field that has unique attributes and behavior
- Focuses on objects that programmers want to manipulate
- Well-suited for programs that are large, complex, and actively updated
- Once an object is known, it is labeled with a class of objects that defines the kind of data it contains and any methods that can manipulate it
- Objects can communicate with well-defined interfaces called messages
- The structure of OOP are:
- Classes: user-defined data types that act as the blueprint for individual objects, attributes, and methods
- Objects: instances of a class created with specifically defined data; when class is defined initially, the description is the only object that is defined
- Methods: functions that are defined inside a class that describe the behavior of an object; contained a reference to an instance object; used for reusability
- Attributes: defined in the class template and represent the state of an object
- Inheritance: Classes can reuse code from other classes. Relationships and subclasses between objects can be assigned, enabling developers to reuse common logic while still maintaining a unique hierarchy
- Benefits: being faster and easier to execute; providing a clear structure for a program; making code easier to modify, debug and maintain; making it easier to reuse code
Imperative Programming
- Paradigm where functions are implicitly coded in every step required to solve a problem
- Every operator is coded and the code itself specifies how the problem is to be solved, meaning precoded models are not called on
- Requires an understanding of functions necessary to solve a problem
- Focus on how the problem should be solved
- The written code performs the functions instead of models, the programmer must code each step
- Imperative provides step by step instructions on how to arrive at a given destination
Is each Monkey an object? Each monkey is not an object because it does not have attributes or behaviors. There also aren't any classes that specifiy the behaviors and attributes.
Build an where the monkey is an object versus two-dimensional array. This would be leading into Unit 5 requirements. This could be done by creating a 2d object for each monkey with the different parts ot the money as different attributes like the head.
Study two-dimensional (2D) array references. Explain different way you can access a 2D array
- Using 2d arrays, you can store so much data at one moment, which can be passed at any number of functions whenever required.
- Each element in the 2D array must by the same type,
- An element in 2-dimensional array is accessed by using the subscripts. That is, row index and column index of the array. int x = a[1,1]
- EX: GiftCard[][] family = new GiftCard[3][4]
- creates a 3 by 4 table