In Java This exercise is focused on making you more familiar with the usage of t
ID: 3742803 • Letter: I
Question
In Java This exercise is focused on making you more familiar with the usage of the lambda expressions in conjunction with Functional Interfaces (namely `IntPredicate`) We already know that the following piece of code allows us to evaluate IntPredicate for a particular input value. private static boolean applyPredicate(int value, IntPredicate predicate) { return predicate.test(value); } It tells us whether our integer value satisfies the condition conveyed by the predicate. e.g. applyPredicate(2, x -> x > 100) returns false because 2 is not greater than 100 applyPredicate(15, x -> (x % 5 == 0)) returns true because 15 is divisible by 5 create a class `TeenCount` that implements the method `public static int[] teenLess(int[] array)` which takes in an array of integers as the input, applies the appropriate predicate over all the elements of the array and then returns a new array containing only those values which are not teens i.e. values which don't lie between `13 and 19 inclusive`. Feel free to use the `applyPredicate()` method in your program You can write a main method to test your code, if you may wish to. Sample Input 1 Sample Output 1 {13, 15, 4, 5} {4, 5} Sample Input 2 Sample Output 2 {13, 19, 13, 14} {} Sample Input 3 Sample Output 3 {4, 3, 9, 0} {4, 3, 9, 0}
Explanation / Answer
import java.util.Arrays; import java.util.function.IntPredicate; public class TeenCount { private static boolean applyPredicate(int value, IntPredicate predicate) { return predicate.test(value); } public static int[] teenLess(int[] array) { int count = 0; for(int i = 0; i 19; } })) { count++; } } int[] arr = new int[count]; int index = 0; for(int i = 0; i 19; } })) { arr[index++] = array[i]; } } return arr; } public static void main(String[] args) { System.out.println(Arrays.toString(teenLess(new int[] {13, 15, 4, 5}))); System.out.println(Arrays.toString(teenLess(new int[] {13, 19, 13, 14}))); System.out.println(Arrays.toString(teenLess(new int[] {4, 3, 9, 0}))); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.