Answered step by step
Verified Expert Solution
Link Copied!
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

image text in transcribed
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);
}
}
EXERCISE Change this line: int count = Algorithm.countIf(ci, new OddPredicate()); 1. To use Anonymous class, and use it. 2. To use Lambda Expression, and use it. class OddPredicate implements UnaryPredicate { public boolean test(Integer i) { return i % 2 != 0; class Test { public static void main(String[] args) { Collections Integer> ci = Arrays.asList(1, 2, 3, 4); int count = Algorithm.countIf(ci, new OddPredicate()); System.out.println("Number of odd integers = " + count); EXERCISE Change this line: int count = Algorithm.countIf(ci, new OddPredicate()); 1. To use Anonymous class, and use it. 2. To use Lambda Expression, and use it. class OddPredicate implements UnaryPredicate { public boolean test(Integer i) { return i % 2 != 0; class Test { public static void main(String[] args) { Collections Integer> 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

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students explore these related Databases questions