Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Vehicle interface
- interface Vehicle {
- String name = "Unknown";
- int maxPassengers = 0;
- double maxSpeed = 0;
- void displayInfo();
- }
- // LandVehicle interface extending Vehicle
- interface LandVehicle extends Vehicle {
- int numWheels = 0;
- void drive();
- }
- // SeaVehicle interface extending Vehicle
- interface SeaVehicle extends Vehicle {
- double displacement = 0;
- void launch();
- }
- // Car class implementing LandVehicle
- class Car implements LandVehicle {
- String name;
- int maxPassengers;
- double maxSpeed;
- int numWheels;
- public Car(String name, int maxPassengers, double maxSpeed, int numWheels) {
- this.name = name;
- this.maxPassengers = maxPassengers;
- this.maxSpeed = maxSpeed;
- this.numWheels = numWheels;
- }
- @Override
- public void drive() {
- System.out.println("Car is driving.");
- }
- @Override
- public void displayInfo() {
- System.out.println("Car Information - Name: " + name + ", Max Passengers: " + maxPassengers + ", Max Speed: " + maxSpeed + ", Number of Wheels: " + numWheels);
- }
- }
- // HoverCraft class implementing LandVehicle and SeaVehicle
- class HoverCraft implements LandVehicle, SeaVehicle {
- String name;
- int maxPassengers;
- double maxSpeed;
- int numWheels;
- double displacement;
- public HoverCraft(String name, int maxPassengers, double maxSpeed, int numWheels, double displacement) {
- this.name = name;
- this.maxPassengers = maxPassengers;
- this.maxSpeed = maxSpeed;
- this.numWheels = numWheels;
- this.displacement = displacement;
- }
- @Override
- public void drive() {
- System.out.println("HoverCraft is driving on land.");
- }
- @Override
- public void launch() {
- System.out.println("HoverCraft is launching on water.");
- }
- public void enterLand() {
- System.out.println("HoverCraft enters land.");
- }
- public void enterSea() {
- System.out.println("HoverCraft enters sea.");
- }
- @Override
- public void displayInfo() {
- System.out.println("HoverCraft Information - Name: " + name + ", Max Passengers: " + maxPassengers + ", Max Speed: " + maxSpeed + ", Number of Wheels: " + numWheels + ", Displacement: " + displacement);
- }
- }
- // Ship class implementing SeaVehicle
- class Ship implements SeaVehicle {
- String name;
- int maxPassengers;
- double maxSpeed;
- double displacement;
- public Ship(String name, int maxPassengers, double maxSpeed, double displacement) {
- this.name = name;
- this.maxPassengers = maxPassengers;
- this.maxSpeed = maxSpeed;
- this.displacement = displacement;
- }
- @Override
- public void launch() {
- System.out.println("Ship is launching on water.");
- }
- @Override
- public void displayInfo() {
- System.out.println("Ship Information - Name: " + name + ", Max Passengers: " + maxPassengers + ", Max Speed: " + maxSpeed + ", Displacement: " + displacement);
- }
- }
- public class VehicleDemo {
- public static void main(String[] args) {
- Car car = new Car("Car-1", 4, 120.0, 4);
- HoverCraft hoverCraft = new HoverCraft("HoverCraft-1", 10, 80.0, 4, 250.0);
- Ship ship = new Ship("Ship-1", 100, 40.0, 5000.0);
- car.displayInfo();
- car.drive();
- hoverCraft.displayInfo();
- hoverCraft.drive();
- hoverCraft.launch();
- hoverCraft.enterLand();
- hoverCraft.enterSea();
- ship.displayInfo();
- ship.launch();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement