Week 4 Free Response Question No. 1
Week 4 Submissions and Key Learnings
Hacks
FRQ No. 1: Methods and control structures
Iteration with 2D Array
Iteration with 2D Array Questions
Is this program in more of an Imperative Programming Style or OOP style? Explain.
- This program is a more object oriented style as objects and variables such as the
MonkeyLoop
are stored and later accessed in thepublic void
variableprintPoem
. - The monkeys seem to act as an object in the program.
Loops Process
- Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize the variable, or we can use an already initialized variable. It is an optional condition.
- Condition: It is the second condition which is executed each time to test the condition of the loop. It continues execution until the condition is false. It must return boolean value either true or false. It is an optional condition.
- Increment/Decrement: It increments or decrements the variable value. It is an optional condition.
- Statement: The statement of the loop is executed each time until the second condition is false.
Zero based counting
- Zero-based numbering is a way of numbering in which the initial element of a sequence is assigned the index 0, rather than the index 1.
Declaring 2D Arrays
- 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.
Accessing 2D Array Elements
- In Java, when accessing the element from a 2D array using
arr[first][second]
, thefirst
index can be thought of as the desired row, and thesecond
index is used for the desired column. Just like 1D arrays, 2D arrays are indexed starting at0
.