-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModernized.java
More file actions
135 lines (108 loc) · 4.06 KB
/
Copy pathModernized.java
File metadata and controls
135 lines (108 loc) · 4.06 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Modernize {
record Rectangle(double length, double width) {
// Modernized to use the record class compact constructor feature
public Rectangle(double length, double width) {
if (length <= 0 || width <= 0) {
throw new java.lang.IllegalArgumentException(
String.format("Invalid dimensions: %f, %f", length, width));
}
}
}
public static void main(String[] args) {
// Given
String dayName;
int dayOfWeek = 1;
// Modernize
dayName = switch (dayOfWeek) {
case 1 -> "Sunday";
case 2 -> "Monday";
default -> "Unknown";
};
System.out.println(dayName);
///////////////////////
// Given
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Day day = Day.WEDNESDAY;
int numLetters = 0;
// Modernize
numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
default -> throw new IllegalStateException("Invalid day: " + day);
};
System.out.println(day + " has " + numLetters + " letters.");
//////////////////////////
// Given
List<String> arrList = new ArrayList<>();
arrList = List.of("a","b","c");
// Modernize
String[] strArr = arrList.toArray(new String[arrList.size()]);
Arrays.stream(strArr).forEach(System.out::println);
//////////////////////////
// Given
List<String> names = List.of("Alice", "Bob", "Charlie");
// Modernize this
List<String> shortNames = new ArrayList<>();
for (String name : names) {
if (name.length() <= 4) {
shortNames.add(name);
}
}
shortNames.forEach(i -> System.out.println("Short name = " + i));
//////////////////////////
Object obj = "Hello";
if (obj instanceof String s) {
System.out.println(s.toUpperCase());
}
/////////////////////////
public static double getPerimeter(Shape s) throws IllegalArgumentException {
return switch (s) {
case Rectangle r -> 2 * r.length() + 2 * r.width();
case Circle c -> 2 * c.radius() * Math.PI;
default -> throw new IllegalArgumentException("Unrecognized shape");
};
}
/////////////////////////
Stream.iterate(1, n -> <= 19, n -> n + 2)
.forEach(System.out::println);
/////////////////////////
DoubleSupplier onceComputedSupplier = memoize(() -> Math.random() * 100);
System.out.println(onceComputedSupplier.getAsDouble()); // Same value every time
System.out.println(onceComputedSupplier.getAsDouble());
IntSupplier fibSupplier = new IntSupplier() {
private int previous = 0, current = 1;
@Override
public int getAsInt() {
int next = previous + current;
previous = current;
current = next;
return previous;
}
};
IntStream.generate(fibSupplier).limit(10).forEach(System.out::println);
IntSupplier randomSupplier = Modernize::generateRandomNumber;
System.out.println(randomSupplier.getAsInt());
List<Integer> list = IntStream.range(1, 5)
.boxed()
.toList();
}
public static int generateRandomNumber() {
return (int)(Math.random() * 100);
}
public static DoubleSupplier memoize(DoubleSupplier supplier) {
final double cache[] = {Double.NaN};
return () -> {
if (Double.isNaN(cache[0])) {
cache[0] = supplier.getAsDouble();
}
return cache[0];
};
}
}