Fill This Form To Receive Instant Help
Homework answers / question archive / Create a class called "Vehicle" and methods that allow you to set the "Make", "Model", "Year,", and "Weight"
Create a class called "Vehicle" and methods that allow you to set the "Make", "Model", "Year,", and "Weight". The class should also contain a "NeedsMaintenance" boolean that defaults to False, and and "TripsSinceMaintenance" Integer that defaults to 0. Next an inheritance classes from Vehicle called "Cars". The Cars class should contain a method called "Drive" that sets the state of a boolean isDriving to True. It should have another method called "Stop" that sets the value of isDriving to false. Switching isDriving from true to false should increment the "TripsSinceMaintenance" counter. And when TripsSinceMaintenance exceeds 100, then the NeedsMaintenance boolean should be set to true. Add a "Repair" method to either class that resets the TripsSinceMaintenance to zero, and NeedsMaintenance to false. Create 3 different cars, using your Cars class, and drive them all a different number of times. Then print out their values for Make, Model, Year, Weight, NeedsMaintenance, and TripsSinceMaintenance
Answer:
Below are classes implementing VehicleDriver, Vehicle, Cars and Planes. VehicleDriver implements the main method that create and test Cars and Planes methods. Class Vehicle is a parent class inherited by child classes Cars and Planes. Please see example run in the explanation below.
public class VehicleDriver { public static void main(String[] args) { Cars car1 = new Cars("Hyundai", "Accent", 1999, 1200.0); //we drive car1 56 times for (int i = 0; i < 56; i++) { car1.Drive(); car1.Stop(); } //we print car1 values System.out.println(car1.toString()); //car2 Cars car2 = new Cars("Toyota", "Sedan", 2010, 5000.0); //we drive carr 102 times for (int i = 0; i < 102; i++) { car2.Drive(); car2.Stop(); } //we print car2 values System.out.println(car2.toString()); //car3 Cars car3 = new Cars("Kia", "Picano", 2019, 6000.0); //we drive car3 103 times for (int i = 0; i < 103; i++) { car3.Drive(); car3.Stop(); } //we repair it car3.repair(); //we print car3 System.out.println(car3.toString()); //we create plane1 Planes plane1 = new Planes("Airbus", "A300", 2002, 120000.0); //we fly 23 times for (int i = 0; i < 23; i++) { plane1.Flying(); plane1.Landing(); } //we print plane1 values System.out.println(plane1.toString()); //we create plane2 Planes plane2 = new Planes("Boeing", "747", 2009, 150000.0); //we fly 103 times. We check plane2 if it can fly //if it is not then it is in repair for (int i = 0; i < 103; i++) { plane2.Flying(); if(!plane2.isIsFlying()){ System.out.println("the plane can't fly until it's repaired"); break; } plane2.Landing(); } //we print plane2 System.out.println(plane2.toString()); } }
public class Vehicle { private String make; private String model; private int year; private double weight; private boolean needsMaintenance; private int TripsSinceMaintenance; public Vehicle() { needsMaintenance = false; TripsSinceMaintenance = 0; } public Vehicle(String make, String model, int year, double weight) { this.make = make; this.model = model; this.year = year; this.weight = weight; needsMaintenance = false; TripsSinceMaintenance = 0; } 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 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 void setNeedsMaintenance(boolean needsMaintenance) { this.needsMaintenance = needsMaintenance; } public void setTripsSinceMaintenance(int TripsSinceMaintenance) { this.TripsSinceMaintenance = TripsSinceMaintenance; } @Override public String toString() { return "Vehicle{" + "make=" + make + ", model=" + model + ", year=" + year + ", weight=" + weight + ", needsMaintenance=" + needsMaintenance + ", TripsSinceMaintenance=" + TripsSinceMaintenance + '}'; } }
public class Cars extends Vehicle{ private boolean isDriving; public Cars() { isDriving = false; } public Cars(String make, String model, int year, double weight) { super(make, model, year, weight); isDriving = false; } public void Drive(){ isDriving = true; } public void Stop(){ isDriving = false; setTripsSinceMaintenance(getTripsSinceMaintenance()+1); if(getTripsSinceMaintenance()==100){ setNeedsMaintenance(true); } } public void repair(){ setTripsSinceMaintenance(0); setNeedsMaintenance(false); } public boolean isIsDriving() { return isDriving; } }
public class Planes extends Vehicle { private boolean isFlying; public Planes() { isFlying = false; } public Planes(String make, String model, int year, double weight) { super(make, model, year, weight); isFlying = false; } public void Flying() { if (isNeedsMaintenance() == false) { isFlying = true; } } public void Landing() { isFlying = false; setTripsSinceMaintenance(getTripsSinceMaintenance() + 1); if (getTripsSinceMaintenance() == 100) { setNeedsMaintenance(true); } } public boolean isIsFlying() { return isFlying; } public void repair(){ setTripsSinceMaintenance(0); setNeedsMaintenance(false); } }
Step-by-step explanation
run: Vehicle{make=Hyundai, model=Accent, year=1999, weight=1200.0, needsMaintenance=false, TripsSinceMaintenance=56} Vehicle{make=Toyota, model=Sedan, year=2010, weight=5000.0, needsMaintenance=true, TripsSinceMaintenance=102} Vehicle{make=Kia, model=Picano, year=2019, weight=6000.0, needsMaintenance=false, TripsSinceMaintenance=0} Vehicle{make=Airbus, model=A300, year=2002, weight=120000.0, needsMaintenance=false, TripsSinceMaintenance=23} the plane can't fly until it's repaired Vehicle{make=Boeing, model=747, year=2009, weight=150000.0, needsMaintenance=true, TripsSinceMaintenance=100} BUILD SUCCESSFUL (total time: 0 seconds)