![]() |
Home | Methods & Control Structures | Class Implementation | Arrays/ArrayLists | 2D Arrays |
Question 1 — Methods & Control Structures
Notes on FRQ Question
IMPORTANT NOTE: Main focus on these questions is the logic, not the specific syntax
Small typos are ok
Strings
String methods/properties:
- Getting size (
str.length()
) - Getting char at index (
str.charAt(index)
) - Compare strings lexicographically (
str.compareTo(str2)
)- 0 if equal, negative if str is less than str2 (or less characters), positive if str is greater than str2 (or more characters)
- Check equality (
str.equals(str2)
) - Get substring (
str.substring(start, end)
)- Start is inclusive, end is exclusive
Control flow
- Just use if, else, else if
- Make sure you think about the order in which the statements will be checked
Example - 2020 FRQ Part 1
9/9 Points
public class HailStone {
public static int hailstoneLength(int n) {
int length = 0;
while (n != 1) {
length++;
if (n%2 == 0) {
n = n/2;
} else {
n = 3n+1;
}
}
}
public static boolean isLongSeq(int n) {
return hailstoneLength(n) > n;
}
public static double propLong(int n) {
int longcount = 0;
for (int i = 1; i <= n; i++) {
if (isLongSeq(n)) {
longcount++;
}
}
return longcount/n;
}
}