Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
141 lines (124 loc) · 4.39 KB

File metadata and controls

141 lines (124 loc) · 4.39 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package students;
import java.util.ArrayList;
import java.util.List;
interface StudentCriterion {
boolean test(Student s);
}
interface Silly {
boolean daft(Student s);
}
//class SmartnessCriterion implements StudentCriterion {
// @Override
// public boolean test(Student s) {
// return s.getGpa() > 3.0F;
// }
//}
//
//class EnthusiasmCriterion implements StudentCriterion {
// @Override
// public boolean test(Student s) {
// return s.getCourses().size() > 3;
// }
//}
//
public final class School {
public static StudentCriterion inverse(StudentCriterion crit) {
return s -> !crit.test(s);
}
public static StudentCriterion and(StudentCriterion c1, StudentCriterion c2) {
return s -> c1.test(s) && c2.test(s);
}
public static StudentCriterion or(StudentCriterion c1, StudentCriterion c2) {
return s -> c1.test(s) || c2.test(s);
}
public static void showAll(List<Student> ls) {
for (Student s : ls) {
System.out.println("> " + s);
}
System.out.println("========================================");
}
// BAD!!! Mixes selection of smart students with what to do with them!
// public static void showAllSmart(List<Student> ls) {
// for (Student s : ls) {
// if (s.getGpa() > 3) {
// System.out.println("> " + s);
// }
// }
// System.out.println("========================================");
// }
public static List<Student> getByCriterion(Iterable<Student> ls, StudentCriterion criterion) {
List<Student> result = new ArrayList<>();
for (Student s : ls) {
if (criterion.test(s)) {
result.add(s);
}
}
return result;
}
public static List<Student> getSmart(Iterable<Student> ls, float threshold) {
List<Student> result = new ArrayList<>();
for (Student s : ls) {
if (s.getGpa() > threshold) {
result.add(s);
}
}
return result;
}
public static List<Student> getEnthusiastic(Iterable<Student> ls, int threshold) {
List<Student> result = new ArrayList<>();
for (Student s : ls) {
if (s.getCourses().size() > threshold) {
result.add(s);
}
}
return result;
}
// public static List<Student> getFairlySmart(Iterable<Student> ls) {
// List<Student> result = new ArrayList<>();
// for (Student s : ls) {
// if (s.getGpa() > 2.6) {
// result.add(s);
// }
// }
// return result;
// }
//
public static void main(String[] args) {
List<Student> school = List.of(
Student.ofNameGpaCourses("Fred", 2.2F, "Math", "Physics", "Politics", "History of Art"),
Student.ofNameGpaCourses("Jim", 2.7F, "Math"),
Student.ofNameGpaCourses("Tony", 2.3F, "Math", "Art", "Engineering"),
Student.ofNameGpaCourses("Alice", 3.5F, "Math"),
Student.ofNameGpaCourses("Sarah", 3.7F, "Math", "Philosophy", "German Literature"),
Student.ofNameGpaCourses("Sheila", 3.8F, "Math", "Physics", "Astrophysics", "Quantum Mechanics")
);
showAll(school);
showAll(getSmart(school, 3.0F));
// showAll(getFairlySmart(school));
showAll(getSmart(school, 2.6F));
showAll(getEnthusiastic(school, 1));
System.out.println("Using behavior parameter");
showAll(getByCriterion(school, Student.getSmartnessCriterion(2.6F)));
showAll(getByCriterion(school, Student.getEnthusiasmCriterion()));
showAll(getByCriterion(school, s -> s.getCourses().size() > 3));
StudentCriterion x = s -> s.getCourses().size() > 3;
// ((Silly)(s -> s.getCourses().size() > 3)).test(school.get(0)) ;
// ((StudentCriterion)(s -> s.getCourses().size() > 3)).test(school.get(0)) ;
System.out.println("--------------------------------");
StudentCriterion smart = Student.getSmartnessCriterion(3F);
System.out.println("Smart: ");
showAll(getByCriterion(school, smart));
StudentCriterion notSmart = inverse(smart);
System.out.println("Not Smart: ");
showAll(getByCriterion(school, notSmart));
System.out.println("--------------------------------");
StudentCriterion enthusiastic = Student.getEnthusiasmCriterion();
System.out.println("Enthusiastic: ");
showAll(getByCriterion(school, enthusiastic));
StudentCriterion notEnthusiastic = inverse(enthusiastic);
System.out.println("Not Enthusiastic: ");
showAll(getByCriterion(school, notEnthusiastic));
System.out.println("Enthusiastic and not smart");
showAll(getByCriterion(school, and(enthusiastic, notSmart)));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.