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
} FruitInspector.Java:
@FunctionalInterface public interface FruitInspector { boolean pass(Fruit fruit); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
