Question: In this lab, you will practice writing Lambda expressions against a given functional interface called FruitInspector , which has just one method that takes a

In this lab, you will practice writing Lambda expressions against a given functional interface called FruitInspector, which has just one method that takes a Fruit and returns a boolean value representing pass for fail. Your job is to review the given start up code and complete the main program class InspectFruits.

You need to write a Lambda expression for the goodInspector who will inspect the fruit according to the following rules:

Only the fruits that meet the following criteria will pass:

1. at least 15 grams in weight.

2. 30 days of age or younger unless it has a hardshell

3. color cannot be black

Hint: You need to change the following statement in the startup InspectFruits.java file.

FruitInspector goodInspector = x -> {return true;};

STARTER CODE:

Banana.java:

public class Banana implements Fruit { double weight; int age; String color; public Banana(double weight, int age, String color) { this.weight = weight; this.age = age; this.color = color; }

@Override public double getWeight() { return weight; }

@Override public int getAge() { return age; } @Override public boolean hasHardShell() { return false; }

@Override public String getColor() { return color; }

@Override public String toString() { return "Banana [weight=" + weight + ", age=" + age + ", color=" + color + "]"; }

} Coconut.java:

public class Coconut implements Fruit { double weight; int age; public Coconut(double weight, int age) { this.weight = weight; this.age = age; }

@Override public double getWeight() { return weight; }

@Override public int getAge() { return age; }

@Override public boolean hasHardShell() { return true; }

@Override public String getColor() { return "brown"; }

@Override public String toString() { return "Coconut [weight=" + weight + ", age=" + age + "]"; } } Kiwi.java:

public class Kiwi implements Fruit { double weight; int age; public Kiwi(double weight, int age) { this.weight = weight; this.age = age; }

public double getWeight() { return weight; }

public int getAge() { return age; }

@Override public boolean hasHardShell() { return false; }

@Override public String getColor() { return "green"; }

@Override public String toString() { return "Kiwi [weight=" + weight + ", age=" + age + "]"; } } Fruit.java:

public interface Fruit { double getWeight(); int getAge(); boolean hasHardShell(); String getColor(); } InspectFruit.java:

import java.util.Arrays; import java.util.Collection; import java.util.List;

public class InspectFruit {

public static void main(String[] args) { List fruits = Arrays.asList( new Banana(35.0, 20, "yellow"), new Banana(37.5, 100, "black"), new Banana(5.5, 5, "green"), new Banana(37.2, 20, "yellow"), new Kiwi(25.1, 19), new Kiwi(6.4, 3), new Kiwi(21.6, 10), new Kiwi(9.9, 5), new Kiwi(33.0, 9), new Coconut(67.0, 30), new Coconut(100.0, 100), new Coconut(45.9, 20), new Coconut(21.9, 2), new Coconut(9.0, 30) ); FruitInspector goodInspector = x -> {return true;}; FruitInspector badInspector = x -> {return true;}; System.out.println("Let's inspect some fruits..."); System.out.println("Good inspector: "); System.out.println("---------------"); performInspection(fruits, goodInspector); System.out.println("Bad inspector: "); System.out.println("---------------"); performInspection(fruits, badInspector); } private static void performInspection(Collection fruits, FruitInspector fruitInspector) { fruits.forEach(f -> { System.out.println("Inspection result of " + f + ": " + (fruitInspector.pass(f) ? "PASS" : "FAIL")); }); }

} FruitInspector.Java:

@FunctionalInterface public interface FruitInspector { boolean pass(Fruit fruit); }

.

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!