This code checks if String "str1" is the same as String "str2".
Note: There are two possible answers.
str1.equals(str2)
OR
str1.compareTo(str2) == 0
This code prints out the word "Hi" 125 times using a for loop.
for (int i = 0; i < 125; i++){
System.out.println("Hi");
}
This code gives the header for a class named Cookie.
public class Cookie {
This code creates a new array named "words" that stores 12 Strings.
String[] words = new String[12];
This code gives the class header for a class called "Oatmeal" that is a subclass of "Cookie".
public class Oatmeal extends Cookie {
This code checks if the variable "num" is even.
This code prints out the word "Hi" 125 times using a while loop.
int i = 0;
while (i < 125){
System.out.println("Hi");
i++;
}
This code creates an instance variable that is an integer called chips.
private int chips;
This code creates a new ArrayList called "nums" that will store integers.
ArrayList<Integer> nums = new ArrayList<Integer>();
This code is the constructor for class Oatmeal that takes one integer parameter and calls the superconstructor from class Cookie with that integer.
public Oatmeal(int c){
super(c);
}
This code gets the letter at index "i" in the String "word".
word.substring(i, i+1)
This code creates a nested loop where the outer loop runs 5 times and the inner loop runs 6 times.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
// do something
}
}
This code is the constructor for class Cookie that takes an integer parameter and stores it in the instance variable "chips".
public Cookie(int c){
chips = c;
}
This code adds together all the values in an array called nums.
Note: There are two possible answers.
int total = 0;
for (int i = 0; i < nums.length; i++) {
total += nums[i];
}
OR
for(int n : nums){
total += n;
}
This code is in the Oatmeal class and calls the Cookie superclass's method "bake" with the argument "my oven".
super.bake("my oven");
This code returns a random integer between int "MAX" and int "MIN".
(int) (Math.random() * (MAX-MIN+1) + MIN)
This code adds together all the digits in the integer "num".
int total = 0;
while (num > 0) {
int digit = num % 10;
total += digit;
num /= 10;
}
This code is the method header for a method called "bake" that doesn't return anything and takes in a String parameter called "oven".
This code adds up all the values in a 2-dimensional array called "mat".
int total = 0;
for (int row = 0; row < mat.length; row++) {
for (int col = 0; col < mat[row].length; col++){
total += mat[row][col];
}
}
This code is the toString method for the class Oatmeal that calls the superclass's toString method and adds the word "oatmeal".
public String toString(){
return super.toString() + "oatmeal".
}
This is another way of writing
!A || !B.
(Hint: it uses &&)
A && B
This code removes all occurrences of the String "target" from the String "phrase".
while (phrase.indexOf(target) >= 0) {
int i = phrase.indexOf(target);
phrase = phrase.substring(0, i) + phrase.substring(i + target.length());
}
This code is the "getter" method for the integer instance variable chips.
public int getChips(){
return chips;
}
This code finds and returns the index of String "target" within the array "words".
for (int i = 0; i < words.length; i++) {
if (words[i] == target)
return i;
}
return -1;
These are the two things required inside a non-infinite recursive method.
A base case and a recursive call to the same method.