In: Computer Science
class Airplane – highlights
-- modelID : String
-planeType: String //”propeller” “jet”
--fuelOnBoard : int
--fuelCapacity: int //gallons
…………...
Assume getters and setters, toString(), constructor with parameters
and constructor
……………
Write lambdas using a standard functional interface to:
a) Given a plane object and “propeller” or “jet” as an input, determine if the given plane matched the given type
b) Output the plane description and “plane needs fuel” if fuel on board is less than 80% of fuel capacity
c) Adjust the fuel by a specified number of gallons and write an example for each showing the usage, using an Airplane instance plane1
Please use Java, don't use other coding languages to solve it. Thank you
Explanation:I have written Airplane class, all the three functional interfaces and an AirplaneTester class to test the interfaces.I have also shown the example for each in the output, please find the image attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation
//code
//Airplane class
public class Airplane {
private String modelID;
private String planeType;
private int fuelOnBoard;
private int fuelCapacity;
public Airplane(String modelID, String planeType,
int fuelOnBoard,
int
fuelCapacity) {
super();
this.modelID = modelID;
this.planeType = planeType;
this.fuelOnBoard =
fuelOnBoard;
this.fuelCapacity =
fuelCapacity;
}
public String getModelID() {
return modelID;
}
public void setModelID(String modelID) {
this.modelID = modelID;
}
public String getPlaneType() {
return planeType;
}
public void setPlaneType(String planeType) {
this.planeType = planeType;
}
public int getFuelOnBoard() {
return fuelOnBoard;
}
public void setFuelOnBoard(int fuelOnBoard) {
this.fuelOnBoard =
fuelOnBoard;
}
public int getFuelCapacity() {
return fuelCapacity;
}
public void setFuelCapacity(int fuelCapacity)
{
this.fuelCapacity =
fuelCapacity;
}
@Override
public String toString() {
return "Airplane [modelID=" +
modelID + ", planeType=" + planeType
+ ", fuelOnBoard=" + fuelOnBoard + ",
fuelCapacity="
+ fuelCapacity + "]";
}
}
//MatchTypeInterface
@FunctionalInterface
public interface MatchTypeInterface {
public boolean matches(Airplane plane, String type);
}
//DescriptionInterface
@FunctionalInterface
public interface DescriptionInterface {
public void display(Airplane plane);
}
//AdjustFuelInterface
@FunctionalInterface
public interface AdjustFuelInterface {
public void adjustFuel(Airplane plane, int
noOfGallons);
}
//AirplaneTest class
public class AirplaneTest {
public static void main(String[] args) {
MatchTypeInterface
matchInterface = (plane, type) -> {
return
plane.getPlaneType() == type;
};
Airplane plane1 = new
Airplane("A123", "propeller", 1400, 2000);
String planeType =
"propeller";
System.out.println(matchInterface.matches(plane1,
planeType));
planeType = "jet";
System.out.println(matchInterface.matches(plane1,
planeType));
System.out.println();
DescriptionInterface
descriptionInterface = (plane) -> {
System.out.println("Description:");
System.out.println(plane.toString());
if
(plane.getFuelOnBoard() < (0.8 * plane.getFuelCapacity()))
{
System.out.println("plane needs fuel");
}
};
descriptionInterface.display(plane1);
System.out.println();
AdjustFuelInterface
adjustFuelInterface = (plane, noOfGallons) -> {
System.out.println("Before adjustment fuel onboard: "
+
plane.getFuelOnBoard());
plane.setFuelOnBoard(plane.getFuelOnBoard() + noOfGallons);
System.out.println(
"After adjustment fuel
onboard: " + plane.getFuelOnBoard());
};
adjustFuelInterface.adjustFuel(plane1, 4);
}
}
Output: