Question

In: Computer Science

class Airplane – highlights -- modelID : String -planeType: String //”propeller” “jet” --fuelOnBoard : int --fuelCapacity:...

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

Solutions

Expert Solution

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:


Related Solutions

Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top = -1; } //Assume that your answers to 3.1-3.3 will be inserted here, ex: // full() would be here //empty() would be here... //push() //pop() //displayStack() } 1. Write two boolean methods, full( ) and empty( ). 2. Write two methods, push( ) and pop( ). Note: parameters may be expected for push and/or pop. 3. Write the method displayStack( ) that prints the...
1. The angle of an airplane propeller makes with the horizontal as a function of time...
1. The angle of an airplane propeller makes with the horizontal as a function of time is given by θ=(125rad/s)t+(42.5rad/s^2)t^2. estimate the instantaneous angular velocity at t=0.00s by calculating the average angular velocity from t=0.00s to t=0.010s A. Estimate the angular speed at t = 0 by calculating the average angular speed between ti = 0 and tf = 0s. B. Estimate the angular speed at t = 1.00 by calculating the average angular speed between ti = 1.00 and...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string • speed : float Create a FigherPlane class that inherits from the Airplane class and adds the following attributes: • numberOfMissiles : short
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); //...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); // converts a hexadecimal number (represented as a STRING) to decimal string dectobin(int); // converts a decimal number to binary (represted as a STRING) string dectohex(int); // converts a decimal number to hexadecimal (represted as a STRING) //the addbin and addhex functions work on UNsigned numbers string addbin(string, string); // adds two binary numbers together (represented as STRINGS) string addhex(string, string); // adds two hexadecimal...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT