-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModernize.java
More file actions
155 lines (128 loc) · 4.58 KB
/
Copy pathModernize.java
File metadata and controls
155 lines (128 loc) · 4.58 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Modernize {
record Rectangle(double length, double width) {
// Modernize this 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));
}
this.length = length;
this.width = width;
}
}
public static void main(String[] args) {
// Given
String dayName;
int dayOfWeek = 1;
// Modernize
switch (dayOfWeek) {
case 1: dayName = "Sunday"; break;
case 2: dayName = "Monday"; break;
default: dayName = "Unknown";
}
System.out.println(dayName);
///////////////////////
// Given
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Day day = Day.WEDNESDAY;
int numLetters = 0;
// Modernize
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
numLetters = 6;
break;
case TUESDAY:
numLetters = 7;
break;
case THURSDAY:
case SATURDAY:
numLetters = 8;
break;
case WEDNESDAY:
numLetters = 9;
break;
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));
//////////////////////////
// Given
Object obj = "Hello";
// Modernize
if (obj instanceof String) {
String s = (String) obj;
System.out.println(s.toUpperCase());
}
/////////////////////////
public static double getPerimeter(Shape s) throws IllegalArgumentException {
if (s instanceof Rectangle r) {
return 2 * r.length() + 2 * r.width();
} else if (s instanceof Circle c) {
return 2 * c.radius() * Math.PI;
} else {
throw new IllegalArgumentException("Unrecognized shape");
}
}
/////////////////////////
Stream.iterate(1, n -> n + 2) // Generates an infinite sequence
.limit(10) // Limits it to 10 elements
.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];
};
}
}