implement Main.java
This commit is contained in:
+50
-106
@@ -1,126 +1,70 @@
|
|||||||
package org;
|
package org;
|
||||||
|
|
||||||
|
import org.behavior.Serviceable;
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.GasCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
|
import org.model.Vehicle;
|
||||||
|
import org.util.VehicleInspector;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 1:
|
ElectricCar tesla = new ElectricCar(
|
||||||
// Create one instance of each vehicle type:
|
"Tesla", "Model S", 2022, 1000,
|
||||||
// - ElectricCar
|
100, 550, 50 // batteryCapacity, range
|
||||||
// - GasCar
|
);
|
||||||
// - SportsCar
|
|
||||||
//
|
GasCar toyota = new GasCar(
|
||||||
// Requirements:
|
"Toyota", "Corolla", 2018, 85000,
|
||||||
// • Use the constructors you created in each class.
|
5, 50, "Petrol" // seats, fuelCapacity, fuelType
|
||||||
// • Provide realistic values (brand, model, year, mileage, etc.).
|
);
|
||||||
// • Store them in variables such as:
|
|
||||||
// ElectricCar tesla = new ElectricCar(...);
|
SportsCar ferrari = new SportsCar(
|
||||||
// ============================================================
|
"Ferrari", "F8 Tributo", 2021, 12000,
|
||||||
|
2, 3.9, "oil", 710 // seats, engineSize, horsepower
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
Vehicle[] vehicles = {tesla, toyota, ferrari};
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("\n--------------BASIC INFO--------------\n");
|
||||||
|
for(int i=0 ; i<3 ; i++) {
|
||||||
|
vehicles[i].basicInfo();
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================
|
System.out.println("--------------INSPECT--------------\n");
|
||||||
// TODO 2:
|
for(int i=0 ; i<3 ; i++) {
|
||||||
// Create an array of Vehicle:
|
VehicleInspector.inspect(vehicles[i]);
|
||||||
// Vehicle[] vehicles = { ... };
|
System.out.println();
|
||||||
//
|
}
|
||||||
// This array MUST contain the objects created above.
|
|
||||||
// This demonstrates **polymorphism** because all different
|
|
||||||
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
|
||||||
// as their parent type (Vehicle).
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
Serviceable[] serviceables = {tesla, toyota, ferrari};
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("--------------SERVISES--------------\n");
|
||||||
|
for(int i=0 ; i<3 ; i++) {
|
||||||
|
Vehicle temp = (Vehicle) serviceables[i];
|
||||||
|
|
||||||
// ============================================================
|
if(serviceables[i].needsService(temp)) {
|
||||||
// TODO 3:
|
|
||||||
// Loop through the Vehicle[] array and:
|
System.out.println(temp.getBrand() + " " + temp.getModel());
|
||||||
// • Call v.basicInfo();
|
serviceables[i].performService();
|
||||||
// • Print a blank line after each.
|
|
||||||
//
|
}
|
||||||
// Purpose:
|
else {
|
||||||
// Demonstrate that all subclasses share the same Vehicle
|
System.out.println(temp.getBrand() + " " + temp.getModel() +
|
||||||
// behavior and can call inherited methods.
|
" does not need service");
|
||||||
// ============================================================
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 4:
|
|
||||||
// Use VehicleInspector to inspect each vehicle:
|
|
||||||
//
|
|
||||||
// VehicleInspector.inspect(v);
|
|
||||||
//
|
|
||||||
// Purpose:
|
|
||||||
// • Demonstrate the use of `instanceof`
|
|
||||||
// • Show subclass‑specific inspection messages
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 5:
|
|
||||||
// Create an array of Serviceable:
|
|
||||||
// Serviceable[] serviceables = { ... };
|
|
||||||
//
|
|
||||||
// This demonstrates **interface‑based polymorphism**.
|
|
||||||
// Every Car subtype implements Serviceable.
|
|
||||||
//
|
|
||||||
// IMPORTANT:
|
|
||||||
// • You must cast to Vehicle when needed:
|
|
||||||
// Vehicle v = (Vehicle) s;
|
|
||||||
//
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 6:
|
|
||||||
// Loop through the Serviceable[] array:
|
|
||||||
//
|
|
||||||
// Steps inside loop:
|
|
||||||
//
|
|
||||||
// 1. Cast the current object to Vehicle:
|
|
||||||
// Vehicle v = (Vehicle) s;
|
|
||||||
//
|
|
||||||
// 2. Check:
|
|
||||||
// if (s.needsService(v)) { ... }
|
|
||||||
//
|
|
||||||
// 3. If true:
|
|
||||||
// - Print a message showing brand + model
|
|
||||||
// - Call s.performService();
|
|
||||||
//
|
|
||||||
// 4. Otherwise:
|
|
||||||
// - Print that the car does not need service
|
|
||||||
//
|
|
||||||
// Add blank lines between each item for readability.
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 7 (Optional):
|
|
||||||
// After everything is done, print a final message:
|
|
||||||
//
|
|
||||||
// System.out.println("=== Workshop Completed ===");
|
|
||||||
//
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("========= Workshop Completed =========");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user