Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e3d94d0b19 | ||
|
|
4cff34b20a | ||
|
|
13e41f5f21 | ||
|
|
46939f6f38 | ||
|
|
26e6d9b82d | ||
|
|
eef8eb4933 | ||
|
|
2267f37d0d | ||
|
|
3a7e3ebea5 |
+40
-113
@@ -1,124 +1,51 @@
|
|||||||
package org;
|
package org;
|
||||||
|
|
||||||
|
import org.behavior.Serviceable;
|
||||||
|
import org.model.*;
|
||||||
|
import org.util.VehicleInspector;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// ============================================================
|
ElectricCar tesla = new ElectricCar("Tesla","3",2020,7000,5,100,60);
|
||||||
// TODO 1:
|
GasCar pride = new GasCar("Saipa","132",2002,200000,5,50,"Gasoline");
|
||||||
// Create one instance of each vehicle type:
|
HybridCar prius = new HybridCar("Toyota","Prius",2025,3000,5,70,90);
|
||||||
// - ElectricCar
|
SportsCar ferrari = new SportsCar("Ferrari","488 GTB",2015,80000,2,20,"Gasoline",2.9);
|
||||||
// - GasCar
|
|
||||||
// - SportsCar
|
|
||||||
//
|
Vehicle[] vehicles = {tesla,pride,prius,ferrari};
|
||||||
// Requirements:
|
System.out.println("=".repeat(20) + " Show Vehicles " + "=".repeat(20));
|
||||||
// • Use the constructors you created in each class.
|
for (Vehicle vehicle : vehicles) {
|
||||||
// • Provide realistic values (brand, model, year, mileage, etc.).
|
vehicle.basicInfo();
|
||||||
// • Store them in variables such as:
|
System.out.println();
|
||||||
// ElectricCar tesla = new ElectricCar(...);
|
}
|
||||||
// ============================================================
|
|
||||||
|
System.out.println("=".repeat(20) + " Inspect Vehicles " + "=".repeat(20));
|
||||||
|
for (Vehicle vehicle : vehicles) {
|
||||||
|
VehicleInspector.inspect(vehicle);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Serviceable[] serviceables = {prius,ferrari,tesla,pride};
|
||||||
|
|
||||||
|
System.out.println("=".repeat(20) + " Check Services " + "=".repeat(20));
|
||||||
|
for (Serviceable serviceable : serviceables) {
|
||||||
|
Vehicle v = (Vehicle) serviceable;
|
||||||
|
if (serviceable.needsService(v)) {
|
||||||
|
System.out.println("brand: " + v.getBrand() + " | model: " + v.getModel());
|
||||||
|
serviceable.performService();
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("brand: " + v.getBrand() + " | model: " + v.getModel() + " doesn't needs service");
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("=".repeat(20) + " Workshop Completed " + "=".repeat(20));
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 2:
|
|
||||||
// Create an array of Vehicle:
|
|
||||||
// Vehicle[] vehicles = { ... };
|
|
||||||
//
|
|
||||||
// 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).
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 3:
|
|
||||||
// Loop through the Vehicle[] array and:
|
|
||||||
// • Call v.basicInfo();
|
|
||||||
// • Print a blank line after each.
|
|
||||||
//
|
|
||||||
// Purpose:
|
|
||||||
// Demonstrate that all subclasses share the same Vehicle
|
|
||||||
// behavior and can call inherited methods.
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// 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 ===");
|
|
||||||
//
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,5 @@
|
|||||||
package org.model;
|
package org.model;
|
||||||
|
|
||||||
/*
|
|
||||||
* Car extends Vehicle.
|
|
||||||
* This class SHOULD implement Serviceable.
|
|
||||||
*
|
|
||||||
* Service logic for Car:
|
|
||||||
* - needsService(): return true if mileage > 10,000
|
|
||||||
* - performService(): print "General car service completed"
|
|
||||||
*/
|
|
||||||
|
|
||||||
import org.behavior.Serviceable;
|
import org.behavior.Serviceable;
|
||||||
|
|
||||||
@@ -17,25 +9,36 @@ public abstract class Car extends Vehicle implements Serviceable {
|
|||||||
|
|
||||||
public Car(String brand, String model, int year,
|
public Car(String brand, String model, int year,
|
||||||
double mileage, int seats) {
|
double mileage, int seats) {
|
||||||
|
super(brand, model, year, mileage);
|
||||||
// TODO: Call super constructor
|
|
||||||
this.seats = seats;
|
this.seats = seats;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Unknown"
|
public int getSeats() { return this.seats; }
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
|
||||||
// Print brand, model, year, mileage, and number of seats
|
|
||||||
|
|
||||||
/*
|
@Override
|
||||||
* Implement Serviceable methods:
|
public String getEnergyType() {
|
||||||
*
|
return "Unknown";
|
||||||
* needsService(Vehicle v):
|
}
|
||||||
* - Check the mileage of the current car
|
|
||||||
* - If mileage > 10000 return true
|
@Override
|
||||||
* - Otherwise return false
|
public void showDetails() {
|
||||||
*
|
System.out.println(
|
||||||
* performService():
|
"brand: " + getBrand() + "\n"
|
||||||
* - Print: "General car service completed"
|
+ " model: " + getModel() + "\n"
|
||||||
*/
|
+ " year: " + getYear() + "\n"
|
||||||
|
+ " mileage: " + getMileage() + "\n"
|
||||||
|
+ " seats: " + getSeats()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
return getMileage() > 10000;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("General car service completed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,5 @@
|
|||||||
package org.model;
|
package org.model;
|
||||||
|
|
||||||
/*
|
|
||||||
* ElectricCar extends Car.
|
|
||||||
* It already inherits Serviceable from Car.
|
|
||||||
*
|
|
||||||
* Service logic for ElectricCar:
|
|
||||||
* - needsService(): return true if mileage > 8000
|
|
||||||
* - performService():
|
|
||||||
* Print "Battery system checked"
|
|
||||||
* Reset chargeLevel to 100
|
|
||||||
*/
|
|
||||||
|
|
||||||
import org.behavior.Chargeable;
|
import org.behavior.Chargeable;
|
||||||
|
|
||||||
@@ -27,13 +17,44 @@ public class ElectricCar extends Car implements Chargeable {
|
|||||||
this.chargeLevel = chargeLevel;
|
this.chargeLevel = chargeLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Electric"
|
public int getBatteryCapacity() { return this.batteryCapacity; }
|
||||||
|
public void setBatteryCapacity(int batteryCapacity) { this.batteryCapacity = batteryCapacity; }
|
||||||
|
public double getChargeLevel() { return this.chargeLevel; }
|
||||||
|
public void setChargeLevel(double chargeLevel) { this.chargeLevel = chargeLevel; }
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
|
||||||
// Call super.showDetails()
|
|
||||||
// Print battery capacity and charge level
|
|
||||||
|
|
||||||
// TODO: Implement charge(double amount)
|
@Override
|
||||||
// Validate negative
|
public String getEnergyType() {
|
||||||
// Cap at 100
|
return "Electric";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println(
|
||||||
|
"battery capacity: " + getBatteryCapacity() + "\n"
|
||||||
|
+ " charge level: " + getChargeLevel());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void charge(double amount) {
|
||||||
|
if (amount < 0) {
|
||||||
|
throw new IllegalArgumentException("Amount must be positive");
|
||||||
|
}
|
||||||
|
if ( getChargeLevel() + amount > 100 ) {
|
||||||
|
setChargeLevel(100);
|
||||||
|
}
|
||||||
|
setChargeLevel(getChargeLevel() + amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
return getMileage() > 8000;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Battery system checked");
|
||||||
|
setChargeLevel(100);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,5 @@
|
|||||||
package org.model;
|
package org.model;
|
||||||
|
|
||||||
/*
|
|
||||||
* GasCar extends Car.
|
|
||||||
* Inherits Serviceable from Car.
|
|
||||||
*
|
|
||||||
* Service logic for GasCar:
|
|
||||||
* - needsService(): return true if mileage > 12000
|
|
||||||
* - performService():
|
|
||||||
* Print "Engine oil changed"
|
|
||||||
* Reset fuelLevel to 100
|
|
||||||
*/
|
|
||||||
|
|
||||||
import org.behavior.Refuelable;
|
import org.behavior.Refuelable;
|
||||||
|
|
||||||
@@ -27,13 +17,42 @@ public class GasCar extends Car implements Refuelable {
|
|||||||
this.fuelType = fuelType;
|
this.fuelType = fuelType;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
public double getFuelLevel() { return this.fuelLevel; }
|
||||||
|
public String getFuelType() { return this.fuelType; }
|
||||||
|
public void setFuelLevel(double fuelLevel) { this.fuelLevel = fuelLevel; }
|
||||||
|
|
||||||
// TODO: Implement refuel(double amount)
|
@Override
|
||||||
// Validate amount
|
public String getEnergyType() {
|
||||||
// Cap at 100
|
return getFuelType() + " (Gas)";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Call super.showDetails()
|
public void refuel(double amount) {
|
||||||
// Print fuel level and fuel type
|
if (amount < 0) {
|
||||||
|
throw new IllegalArgumentException("Amount must be positive");
|
||||||
|
}
|
||||||
|
if ( getFuelLevel() + amount > 100 ) {
|
||||||
|
setFuelLevel(100);
|
||||||
|
}
|
||||||
|
setFuelLevel(getFuelLevel() + amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println(
|
||||||
|
"fuel level: " + getFuelLevel() + "\n"
|
||||||
|
+ " fuel type: " + getFuelType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
return getMileage() > 12000;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed");
|
||||||
|
setFuelLevel(100);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package org.model;
|
||||||
|
|
||||||
|
import org.behavior.Chargeable;
|
||||||
|
import org.behavior.Refuelable;
|
||||||
|
|
||||||
|
public class HybridCar extends Car implements Refuelable, Chargeable {
|
||||||
|
private double batteryLevel;
|
||||||
|
private double fuelLevel;
|
||||||
|
|
||||||
|
public HybridCar(String brand, String model, int year,
|
||||||
|
double mileage, int seats, double batteryLevel, double fuelLevel) {
|
||||||
|
super(brand, model, year, mileage, seats);
|
||||||
|
this.batteryLevel = batteryLevel;
|
||||||
|
this.fuelLevel = fuelLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getEnergyType() {
|
||||||
|
return "Hybrid";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println(
|
||||||
|
"Battery Level: " + this.batteryLevel + "\n"
|
||||||
|
+ "Fuel Level: " + this.fuelLevel
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void charge(double amount) {
|
||||||
|
if (amount < 0) {
|
||||||
|
throw new IllegalArgumentException("Amount must be positive");
|
||||||
|
}
|
||||||
|
if (this.batteryLevel + amount > 100) {this.batteryLevel = 100;}
|
||||||
|
this.batteryLevel += amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void refuel(double amount) {
|
||||||
|
if (amount < 0) {
|
||||||
|
throw new IllegalArgumentException("Amount must be positive");
|
||||||
|
}
|
||||||
|
if (this.fuelLevel + amount > 100) {this.fuelLevel = 100;}
|
||||||
|
this.fuelLevel += amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed and batter system checked");
|
||||||
|
this.batteryLevel = 100;
|
||||||
|
this.fuelLevel = 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,13 +1,5 @@
|
|||||||
package org.model;
|
package org.model;
|
||||||
|
|
||||||
/*
|
|
||||||
* SportsCar extends GasCar.
|
|
||||||
*
|
|
||||||
* Special service logic for SportsCar:
|
|
||||||
* - needsService(): return true if mileage > 5000
|
|
||||||
* - performService():
|
|
||||||
* Print "High-performance brake system checked"
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class SportsCar extends GasCar {
|
public class SportsCar extends GasCar {
|
||||||
|
|
||||||
@@ -24,7 +16,19 @@ public class SportsCar extends GasCar {
|
|||||||
this.zeroToHundred = zeroToHundred;
|
this.zeroToHundred = zeroToHundred;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Call super.showDetails()
|
public void showDetails() {
|
||||||
// Print acceleration time
|
super.showDetails();
|
||||||
|
System.out.println("acceleration time: " + this.zeroToHundred);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
return getMileage() > 5000;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("High-performance brake system checked");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
package org.model;
|
package org.model;
|
||||||
|
|
||||||
/*
|
|
||||||
* Abstract base class for all vehicles.
|
|
||||||
* This class DOES NOT implement Serviceable.
|
|
||||||
* Service behavior will be defined in concrete subclasses.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public abstract class Vehicle {
|
public abstract class Vehicle {
|
||||||
|
|
||||||
@@ -20,13 +15,18 @@ public abstract class Vehicle {
|
|||||||
this.mileage = mileage;
|
this.mileage = mileage;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create getters for all fields
|
public String getBrand() { return this.brand; }
|
||||||
|
public String getModel() { return this.model; }
|
||||||
|
public int getYear() { return this.year; }
|
||||||
|
public double getMileage() { return this.mileage; }
|
||||||
|
|
||||||
// TODO: Implement addMileage(double km)
|
public void addMileage(double mileage) {
|
||||||
// If km is negative → print error
|
if (mileage < 0) {
|
||||||
// Otherwise increase mileage
|
throw new IllegalArgumentException("Mileage cannot be negative");
|
||||||
|
}
|
||||||
|
this.mileage += mileage;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Declare abstract method getEnergyType()
|
|
||||||
|
|
||||||
public void basicInfo() {
|
public void basicInfo() {
|
||||||
System.out.println(
|
System.out.println(
|
||||||
@@ -37,4 +37,5 @@ public abstract class Vehicle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public abstract void showDetails();
|
public abstract void showDetails();
|
||||||
|
public abstract String getEnergyType();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package org.util;
|
package org.util;
|
||||||
|
|
||||||
import org.*;
|
import org.*;
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.GasCar;
|
||||||
|
import org.model.HybridCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
import org.model.Vehicle;
|
import org.model.Vehicle;
|
||||||
|
|
||||||
/*
|
|
||||||
* Utility class to inspect vehicles.
|
|
||||||
* Demonstrates instanceof and polymorphism.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class VehicleInspector {
|
public class VehicleInspector {
|
||||||
|
|
||||||
@@ -14,9 +15,17 @@ public class VehicleInspector {
|
|||||||
|
|
||||||
v.basicInfo();
|
v.basicInfo();
|
||||||
|
|
||||||
// TODO:
|
if (v instanceof ElectricCar) {
|
||||||
// If ElectricCar → print "Check battery system"
|
System.out.println("Check battery service");
|
||||||
// If SportsCar → print "Check brakes and performance"
|
}
|
||||||
// If GasCar → print "Check engine and oil"
|
else if (v instanceof SportsCar) {
|
||||||
|
System.out.println("Check brakes and performance");
|
||||||
|
}
|
||||||
|
else if (v instanceof GasCar) {
|
||||||
|
System.out.println("Check engine and oil");
|
||||||
|
}
|
||||||
|
else if (v instanceof HybridCar) {
|
||||||
|
System.out.println("Check engine and oil and battery service");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user