Unit 2 wrap-upPut it together

| Topic | The one thing to remember |
|---|---|
| Boolean expressions (2.2) | Relational operators give true or false; == on doubles is risky |
| if, else, else if (2.3-2.4) | Separate ifs are all checked; a chain runs only its first true branch |
| &&, ||, ! (2.5) | Short-circuit: the right side may never run |
| Equivalence (2.6) | De Morgan flips the operator; equals for contents, == for same object |
| while, for (2.7-2.8) | Check before every pass; count passes from the bounds |
| Algorithms (2.9-2.10) | Sum, count, max/min, any/all, digits, String traversal |
| Nested loops (2.11-2.12) | The inner loop runs fully for each outer pass; multiply or add up |
?Wait, when do I use a for loop and when a while loop?
Three threads run through this unit. First, Boolean expressions drive everything: every if, every while and every for has a test, and short-circuiting and De Morgan's laws are about those tests. Second, order matters: the first true branch of a chain wins, the condition of a loop is checked before every pass, and the inner loop finishes before the outer loop moves on. Third, patterns repeat: count, sum, max, any or all, digits, and String traversal show up again in Unit 4 with arrays and ArrayLists.
I thought about it. Show me
Traps that cost points
Unit 2 syntax sheet
== != < > <= >=a && b a || b !a!(a && b) is !a || !bif (...) { } else if (...) { } else { }while (cond) { ... }for (int i = a; i < b; i++)n % d == 0n % 10 n /= 10s.substring(i, i + 1)n(n + 1) / 2FRQ strategy for Unit 2
- Unit 2 is the control-structure half of Question 1, Methods and Control Structures (7 points): part A usually needs a loop with an if inside, and part B a String traversal. Loops and ifs also appear in every other FRQ.
- Name the pattern before you write: count, sum, max or min, any or all, digits, or build a String. Then write its three parts: set up, loop, use the result.
- Write the loop header from the first and last values you need. Test it on the shortest input the preconditions allow, including an empty String or a one-digit number.
- Put return inside the loop only for an early answer (found it, or it failed). Put the default return after the loop.
- Compare Strings with equals. Get single characters with substring(i, i + 1).
- Don't print when the method should return. Don't change a parameter you'll need later; copy it into a local variable first.
- Pattern: build a String. Before the loop: String result = "";.
Why?
Strings are immutable, so you can't delete letters from s. You build a new String out of the letters you keep. - Visit every index: for (int i = 0; i < s.length(); i++), with String ch = s.substring(i, i + 1); inside.
- Keep ch only if it isn't a vowel. One test does it: "aeiou".indexOf(ch) == -1 is true exactly when ch is not one of the five vowels. Then result += ch;.
Why?
You could also write five equals tests joined by ||, wrapped in !( ). indexOf is shorter and harder to get wrong. - After the loop, return result;. Check "education": e, u, a, i, o are dropped, leaving d, c, t, n: "dctn".
Answer:
public static String removeVowels(String s)
{
String result = "";
for (int i = 0; i < s.length(); i++)
{
String ch = s.substring(i, i + 1);
if ("aeiou".indexOf(ch) == -1)
{
result += ch;
}
}
return result;
}removeVowels("education") returns "dctn" and removeVowels("rhythm") returns "rhythm" (checked under Java 17).
Stuck? Get a hint
- Which pattern builds a new String? Start from "".
- Get each character with substring(i, i + 1).
- "aeiou".indexOf(ch) is -1 when ch is not a vowel. Keep those.
int n = 30405;
int count = 0;
while (n > 0)
{
int d = n % 10;
if (d % 2 == 0)
{
count++;
}
n /= 10;
}
System.out.println(count);- A4
- B3
- C5
- D2
Show the answer
B. The loop looks at each digit of 30405 from the right: 5, 0, 4, 0, 3. The test d % 2 == 0 counts the even digits, 0, 4 and 0, so it prints 3. Choice C counts every digit, and choice D forgets that 0 is even.
- Ax < 1 && x > 100
- B!(x >= 1 || x <= 100)
- Cx < 1 || x > 100
- Dx <= 1 || x >= 100
Show the answer
C. "Between 1 and 100, inclusive" is x >= 1 && x <= 100. By De Morgan, its negation is x < 1 || x > 100. Choice A can never be true: no x is both below 1 and above 100. Choice B negates the wrong expression (it uses ||), and choice D wrongly excludes 1 and 100.
