Repository navigation
Expand file tree
/
Copy pathQ4_ControlFlow.java
More file actions
68 lines (58 loc) · 2.07 KB
/
Copy pathQ4_ControlFlow.java
File metadata and controls
68 lines (58 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class Q4_ControlFlow {
// ── PREDICT: What does main() print for lines 1-5? ──
static boolean log(String label) {
System.out.println(" eval " + label);
return true;
}
public static void main(String[] args) {
// Q4a: which "eval" lines actually print?
System.out.println("1:");
if (false && log("A")) {}
if (true || log("B")) {}
if (false || log("C")) {}
// Q4b: switch fallthrough
System.out.println("2:");
int val = 2;
switch (val) {
case 1: System.out.println(" one");
case 2: System.out.println(" two");
case 3: System.out.println(" three");
default: System.out.println(" default");
}
// Q4c: what's the final value of sum?
int sum = 0;
for (int i = 0; i < 10; i++) {
if (i % 3 == 0) continue;
if (i > 7) break;
sum += i;
}
System.out.println("3: " + sum);
// Q4d: post-increment in expression
int n = 5;
int result = n++ + ++n;
System.out.println("4: " + result + " " + n);
// Q4e
System.out.println("5: " + (true ? "yes" : "no") + (1 > 2 ? "!" : "?"));
// ── WRITE ──
// Implement fizzBuzz(int n) that returns:
// "FizzBuzz" if divisible by both 3 and 5
// "Fizz" if divisible by 3 only
// "Buzz" if divisible by 5 only
// the number as a String otherwise
//
// Use a SWITCH EXPRESSION (Java 14+), not if/else.
// Hint: you can switch on a computed value.
assert fizzBuzz(15).equals("FizzBuzz");
assert fizzBuzz(9).equals("Fizz");
assert fizzBuzz(10).equals("Buzz");
assert fizzBuzz(7).equals("7");
assert fizzBuzz(30).equals("FizzBuzz");
assert fizzBuzz(1).equals("1");
System.out.println("fizzBuzz ok");
}
// TODO: implement with a switch expression
static String fizzBuzz(int n) {
// your code here
return "";
}
}