class Vehicle { private String make; private String model; private int year; private double weight; private boolean needsMaintenance; private int tripsSinceMaintenance; // Constructors public Vehicle(String make, String model, int year, double weight) { this.make = make; this.model = model; this.year = year; this.weight = weight; this.needsMaintenance = false; this.tripsSinceMaintenance = 0; } public Vehicle() { this.needsMaintenance = false; this.tripsSinceMaintenance = 0; } // Writing set methods for setting make, model, year & weight public void setMake(String make) { this.make = make; } public void setModel(String model) { this.model = model; } public void setYear(int year) { this.year = year; } public void setWeight(double weight) { this.weight = weight; } public String getMake() { return make; } public String getModel() { return model; } public int getYear() { return year; } public double getWeight() { return weight; } public boolean isNeedsMaintenance() { return needsMaintenance; } public int getTripsSinceMaintenance() { return tripsSinceMaintenance; } public void setNeedsMaintenance(boolean needsMaintenance) { this.needsMaintenance = needsMaintenance; } public void setTripsSinceMaintenance(int tripsSinceMaintenance) { this.tripsSinceMaintenance = tripsSinceMaintenance; } @Override public String toString() { return "{make=" + make + ", model=" + model + ", year=" + year + ", weight=" + weight + ", needsMaintenance=" + needsMaintenance + ", TripsSinceMaintenance=" + tripsSinceMaintenance + "}"; } } // Car class class Cars extends Vehicle { private boolean isDriving; // Constructors public Cars(String make, String model, int year, double weight) { super(make, model, year, weight); this.isDriving = false; } public Cars() { super(); this.isDriving = false; } // Drive method public void Drive() { this.isDriving = true; } // Stop method public void Stop() { this.isDriving = false; // When switching from true to false, increment trip since maintenance setTripsSinceMaintenance(getTripsSinceMaintenance()+1); // If it exceeds 100, set needsMaintenance to boolean if (getTripsSinceMaintenance() == 100) { setNeedsMaintenance(true); } } // Repair method public void Repair() { setTripsSinceMaintenance(0); setNeedsMaintenance(false); } public boolean isDriving() { return isDriving; } @Override public String toString() { return "Cars: " + super.toString(); } } class Planes extends Vehicle { private boolean isFlying; // Constructors public Planes() { super(); isFlying = false; } public Planes(String make, String model, int year, double weight) { super(make, model, year, weight); this.isFlying = false; } // Flying & Landing methods public void Flying() { if (!isNeedsMaintenance()) { this.isFlying = true; } } public void Landing() { this.isFlying = false; setTripsSinceMaintenance(getTripsSinceMaintenance()+1); if (getTripsSinceMaintenance() == 100) { setNeedsMaintenance(true); } } public boolean isFlying() { return isFlying; } // Repair method public void Repair() { setTripsSinceMaintenance(0); setNeedsMaintenance(false); } @Override public String toString() { return "Planes: " + super.toString(); } } public class VehicleDriver { public static void main(String[] args) { // Car1 Cars car1 = new Cars("Honda", "Accord", 2000, 1000); // Driving car1 for 50 times for (int i = 0; i < 50; i++) { car1.Drive(); car1.Stop(); } // Printing car1 System.out.println(car1); // Car2 Cars car2 = new Cars("Toyota", "Grande", 2005, 1500); // Driving car2 for 102 times for (int i = 0; i < 102; i++) { car2.Drive(); car2.Stop(); } // Printing car2 System.out.println(car2); // Car3 Cars car3 = new Cars("Kia", "Picano", 2000, 1000); // Driving car3 for 103 times for (int i = 0; i < 103; i++) { car3.Drive(); car3.Stop(); } // Repairing car 3 car3.Repair(); System.out.println(car3); /* Same process doing with planes */ Planes plane1 = new Planes("Airbus", "A300", 2001, 120000.0); Planes plane2 = new Planes("Boeing", "747", 2009, 150000.0); for (int i = 0; i < 23; i++) { plane1.Flying(); plane1.Landing(); } System.out.println(plane1); for (int i = 0; i < 103; i++) { plane2.Flying(); if (!plane2.isFlying()) { System.out.println("Plane can't fly until it's repaired"); break; } plane2.Landing(); } System.out.println(plane2); } }