Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


ist242 — Welch

Lab 7: Generalizing with Higher Order Functions and Predicates


In this lab, you’ll explore how to define and utilize higher order functions and predicates in Java to write reusable, generic methods.

To get started, create a file in an IntelliJ project called Hi gherOrderFns.java and write all of your code in it.

Note: All methods you define in this lab should be marked static.


Part 1: Practice generalizing with Predicate

Implement the boolean-valued methods described below.

1. Write a method isAllEven that returns true if and only if each integer in the input list, nums, is even; false otherwise.

2. Write a method isAllOdd that functions like the above method (but only returns true if all the numbers passed in the input list are odd).

3. Write a method consistsOfBigWords that takes a list of strings as input, and returns true if and only if the length of each string in the input list is greater than 5; false otherwise.

4. Now write one more method... this one should be “parameterized” by a generic type T and should be named isConformalWith. The method should accept as input a List<T> along with a Predicate<T>. It should be capable of generalizing the logic for questions 1-3 above. Hint: you can ‘apply’ a predicate p to some argument x by saying p.test(x).

Hint: skip to section 3 to get an idea of how to “simulate” the isAllEven method’s function-ality using higher order predicates/lambda expressions and the isConformalWith method from question 4.


Part 2: Practice generalizing with Function

First, recall that a Function differs from a Predicate only in the sense that predicates must always return strictly the values true or false, while functions may return any entity of another type (including booleans).

In implementing each of the methods detailed below, do not modify the input lists. Rather, populate and return a new list with the adjustments detailed in each question.

1. Write a method that takes as input a list of integers, nums, and returns a new list of integers with the number 5 added to number in the input list.

2. Write a method, lowerAll that takes as input a list of strings, and returns a new list in which all strings in the input list have been lowercased.

Here’s an example: lowerAll(List.of("ToM", "jERRy", "MOOO")) should return a list containing: ["tom", "jerry", "mooo"].

3. Write a method that takes a list of integers, nums, and returns a list of boolean indicating true if the number at list index i is positive (or zero); false otherwise.

Here’s an example: convertToBools(List.of(1, -5, 6, 0, -1)) should return a list containing: [true, false, true, true, false].

4. Now write a method, apply, that generalizes the logic for questions 1-3. Your method should:

(a) be parameterized by two generic types: T and R

(b) take as input a list of T (i.e. List<T>)

(c) take as input a function f that accepts an element of type T and produces an element of type R (i.e.: a Function<T, R>)

The method should return a new list containing entries of generic type R (i.e.: those ‘trans-formed’ by the function f passed in).


Part 3: Testing

Write some tests in a main method verifying that your methods work. Then reproduce the tests using lambda expressions. For reference: here’s the syntax for creating a lambda expression of type Predicate<String> named mooPred that simply tests if a given string s is equal to the string "moooo":

Predicate < String > mooPred = ( String s ) -> s . equals ( " moooo " );
System . out . println ( mooPred ( " catz " )); // prints false
String moo = " moooo "
System . out . println ( mooPred ( moo )); // prints true

And here’s how we would do it with the isAllOdd method from part 1:

println ( isAllEven ( List . of (2 , 2 , 4 , 2)); // prints true
println (
isConformalWith ( List . of (2 , 2 , 4 , 2) ,
( Integer x ) -> x % 2 == 0); // prints true


Handin Instructions

Submit your HigherOrderFns.java file on canvas (not zyLabs).

You should verify that you’ve submitted the correct .zip file (download, unzip it, and check). It is your responsibility to ensure you’ve turned in what you intended to turn in.