Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Here it is. thanks.////// interface UnaryPredicate { public boolean test(T obj); } final class Algorithm { public static int countIf(Collection c, UnaryPredicate p) { int
Here it is. thanks.//////
interface UnaryPredicate {
public boolean test(T obj);
}
final class Algorithm {
public static int countIf(Collection c, UnaryPredicate p) {
int count = 0;
for (T elem : c)
if (p.test(elem))
++count;
return count;
}
}
class OddPredicate implements UnaryPredicate {
public boolean test(Integer i) {
return i % 2 != 0;
}
}
class Test {
public static void main(String[] args) {
Collection ci = Arrays.asList(1, 2, 3, 4);
int count = Algorithm.countIf(ci, new OddPredicate());
System.out.println("Number of odd integers = " + count);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started