Question: First, create an interface called IMeasurable that exports the following method signatures: getMeasure (returns an int and takes no formal parameters) updateMeasure (returns an IMeasurable

First, create an interface called IMeasurable that exports the following method signatures:

getMeasure (returns an int and takes no formal parameters)

updateMeasure (returns an IMeasurable and takes one formal parameter: int updatedMeasure)

Step 2: Implementing IMeasurable Two Ways

Now create a class Country that implements IMeasurable. The class should store:

the name of the country,

its estimated population.

The class constructor should read these in and initialize them.

Provide getters for the above fields and then override the getMeasurable() method to return the population of the country. Next, override the updateMeasure method as follows:

@Override public IMeasurable updateMeasure(int updatedMeasure) { // this method is not mutating the original object's population field // (instead returns a copy with the changes made) return new Country(name, updatedMeasure); }

Now provide a toString that renders the country like so: "country: , ".

Second, create another class, BankAccount, that implements IMeasurable and stores two fields: an accountHolder (String) and a balance (int). Override getMeasure() to return the balance and then override updateMeasurable in the same way you did above --- only it will return a new BankAccount whose balance is updatedMeasure.

Lastly, override toString to render BankAccount objects like so: "bank account for: ($)"

Step 3: Creating a Functional Interface: IMeasurablePredicate

Next, add another interface called IMeasurablePredicate. The interface should export a single method signature with the following contract:

/** * Returns {@code true} if and only if {@code x} satisfies this * predicate; {@code false} otherwise. * * @param x the measurable object being tested. * @return whether or not {@code x} satisfies this predicate. */ boolean test(IMeasurable x);

This interface will serve as a predicate for IMeasurable objects.

Step 4: Generalizing with Higher Order Methods in a Tester

In a Tester, implement the following static method (you'll have to translate the rough pseudocode in the body):

/** * Filters the given {@code measurables} list given and returns a new list * consisting of only elements that satisfy the provided predicate * {@code pred}. * * @param measurables a list of measurable objects. * @param pred the predicate that tests/checks each measurable. * @return a list of measurables containing only those that satisfy {@code pred}. */ public static List filterMeasurables( List measurables, IMeasurablePredicate pred) { result, initialize it to a new array list>  result list> <return result> }

The above method is a general method for filtering any list of objects that implement IMeasurable. Now write three additional static methods:

1. negate: this method should ONLY take a list of IMeasurable objects and return a new list in which each IMeasurable object in the returned list has a negated measure. This method will look nearly the same as the one outlined above (though it will not take the predicate as a parameter and will need to use the updateMeasure(..) method inside the for loop).

2. make50: this method will also accept a list of IMeasurable objects and return a new list, only here -- rather than inverting each IMeasurable object -- you'll instead make the measure the number 50 exactly (hint: use the updateMeasure(..) method). This method should be 99% similar to the negate method you just implemented (the only thing different should be inside the loop).

3. map: this method generalizes the previous two and should take as input:

a list of IMeasurables,

a function F that accepts something of type IMeasurable and produces something of type IMeasurable (i.e.: a Function).

The method body will look very similar to the previous two:

- first, declare a new empty list of IMeasurables called result,

- then iterate over each measurable m in the input list and apply the function F to m (i.e.: F.apply(m)); then add the result of the apply call to the result list,

- after the loop, return the result list.

Now paste in the following main() method and answer the questions using the provided myMeasurables list, passing in the appropriate lambda expressions:

public static void main(String[] args) { List myMeasurables = List.of( new BankAccount("Bob", 3000),  new BankAccount("Viggo Mortenson", 3000000),  new Country("San Marino", 33685),  new Country("US", 334125343),  new BankAccount("Alice", 20000),  new BankAccount("Moo", 4),  new BankAccount("Chedder-the-mouse", 40),  new Country("Pitcairn Islands", 40),  new Country("Belarus", 9349645),  new Country("Austria", 8976467),  new BankAccount("Josh", 200),  new BankAccount("Fred", 2500),  new BankAccount("Jimmy 'Brick top' Pulford", 2000000),  new BankAccount("Mark", 1000),  new Country("Vatican City", 825) ); }

Questions:

1) call filterMeasurables to retrieve the list of COUNTRIES whose balance is less than 50000; print out the list that results.

To get you going, here's one way to construct the lambda needed to answer the question above -- this is in my main() in the Tester class:

IMeasurablePredicate p = x -> x.getMeasure() < 50000 && x instanceof Country; // now pass predicate p into the filterMeasurables method and print the resulting list

here's the output from looping over and printing the list that results from the call to filterMeasurables with the lambda expression above:

country: San Marino, 33685 country: Pitcairn Islands, 40 country: Vatican City, 825

Now we'll use our other methods: make50(..), negate(..), and map(..).

2) call make50 on myMeasurables and print out the resulting list (every bank account and country in the returned list should now have a measure of 50)

3) call negate on myMeasurables and print out the resulting list (every bank account and country in the returned list should now have a negated measure)

Now we'll use the map method and lambda expressions to accomplish what you did in questions 2 and 3 above. Here's an example of how to use map to answer question 2 above:

// applies the lambda expression 'x -> x.updateMeasure(50)' to every element in myMeasurables List q2out = map(myMeasurables, x -> x.updateMeasure(50)); 

(you can print the resulting list each time after each call to confirm its working -- note that it should match the output you got from question 2)

4) Now call map and pass in a lambda expression that does what the negate method did in question 3 above; print the resulting list to ensure that it matches the list that negate produced in question 3.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!