Advertisement
vvccs

E3_Vehicle_Interface

Nov 8th, 2023 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. // Vehicle interface
  2. interface Vehicle {
  3.     String name = "Unknown";
  4.     int maxPassengers = 0;
  5.     double maxSpeed = 0;
  6.  
  7.     void displayInfo();
  8. }
  9.  
  10. // LandVehicle interface extending Vehicle
  11. interface LandVehicle extends Vehicle {
  12.     int numWheels = 0;
  13.  
  14.     void drive();
  15. }
  16.  
  17. // SeaVehicle interface extending Vehicle
  18. interface SeaVehicle extends Vehicle {
  19.     double displacement = 0;
  20.  
  21.     void launch();
  22. }
  23.  
  24. // Car class implementing LandVehicle
  25. class Car implements LandVehicle {
  26.     String name;
  27.     int maxPassengers;
  28.     double maxSpeed;
  29.     int numWheels;
  30.  
  31.     public Car(String name, int maxPassengers, double maxSpeed, int numWheels) {
  32.         this.name = name;
  33.         this.maxPassengers = maxPassengers;
  34.         this.maxSpeed = maxSpeed;
  35.         this.numWheels = numWheels;
  36.     }
  37.  
  38.     @Override
  39.     public void drive() {
  40.         System.out.println("Car is driving.");
  41.     }
  42.  
  43.     @Override
  44.     public void displayInfo() {
  45.         System.out.println("Car Information - Name: " + name + ", Max Passengers: " + maxPassengers + ", Max Speed: " + maxSpeed + ", Number of Wheels: " + numWheels);
  46.     }
  47. }
  48.  
  49. // HoverCraft class implementing LandVehicle and SeaVehicle
  50. class HoverCraft implements LandVehicle, SeaVehicle {
  51.     String name;
  52.     int maxPassengers;
  53.     double maxSpeed;
  54.     int numWheels;
  55.     double displacement;
  56.  
  57.     public HoverCraft(String name, int maxPassengers, double maxSpeed, int numWheels, double displacement) {
  58.         this.name = name;
  59.         this.maxPassengers = maxPassengers;
  60.         this.maxSpeed = maxSpeed;
  61.         this.numWheels = numWheels;
  62.         this.displacement = displacement;
  63.     }
  64.  
  65.     @Override
  66.     public void drive() {
  67.         System.out.println("HoverCraft is driving on land.");
  68.     }
  69.  
  70.     @Override
  71.     public void launch() {
  72.         System.out.println("HoverCraft is launching on water.");
  73.     }
  74.  
  75.     public void enterLand() {
  76.         System.out.println("HoverCraft enters land.");
  77.     }
  78.  
  79.     public void enterSea() {
  80.         System.out.println("HoverCraft enters sea.");
  81.     }
  82.  
  83.     @Override
  84.     public void displayInfo() {
  85.         System.out.println("HoverCraft Information - Name: " + name + ", Max Passengers: " + maxPassengers + ", Max Speed: " + maxSpeed + ", Number of Wheels: " + numWheels + ", Displacement: " + displacement);
  86.     }
  87. }
  88.  
  89. // Ship class implementing SeaVehicle
  90. class Ship implements SeaVehicle {
  91.     String name;
  92.     int maxPassengers;
  93.     double maxSpeed;
  94.     double displacement;
  95.  
  96.     public Ship(String name, int maxPassengers, double maxSpeed, double displacement) {
  97.         this.name = name;
  98.         this.maxPassengers = maxPassengers;
  99.         this.maxSpeed = maxSpeed;
  100.         this.displacement = displacement;
  101.     }
  102.  
  103.     @Override
  104.     public void launch() {
  105.         System.out.println("Ship is launching on water.");
  106.     }
  107.  
  108.     @Override
  109.     public void displayInfo() {
  110.         System.out.println("Ship Information - Name: " + name + ", Max Passengers: " + maxPassengers + ", Max Speed: " + maxSpeed + ", Displacement: " + displacement);
  111.     }
  112. }
  113.  
  114. public class VehicleDemo {
  115.     public static void main(String[] args) {
  116.         Car car = new Car("Car-1", 4, 120.0, 4);
  117.         HoverCraft hoverCraft = new HoverCraft("HoverCraft-1", 10, 80.0, 4, 250.0);
  118.         Ship ship = new Ship("Ship-1", 100, 40.0, 5000.0);
  119.  
  120.         car.displayInfo();
  121.         car.drive();
  122.  
  123.         hoverCraft.displayInfo();
  124.         hoverCraft.drive();
  125.         hoverCraft.launch();
  126.         hoverCraft.enterLand();
  127.         hoverCraft.enterSea();
  128.  
  129.         ship.displayInfo();
  130.         ship.launch();
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement