1 Commits
Author SHA1 Message Date
Soroush ff9237f507 Workshop-3 2026-04-28 23:17:44 +03:30
8 changed files with 234 additions and 120 deletions
+40 -70
View File
@@ -1,20 +1,20 @@
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) {
// ============================================================ // ============================================================
// TODO 1: ElectricCar tesla = new ElectricCar("Tesla", "Model S", 2022, 85000, 5, 550, 50);
// Create one instance of each vehicle type:
// - ElectricCar GasCar hyundai = new GasCar("Hyundai", "Sonata", 2014, 18000, 5, 80, "Super Petrol");
// - GasCar
// - SportsCar SportsCar bugatti = new SportsCar("Bugatti", "Chiron", 2026, 1000, 2, 60, "Premium", 2.4);
//
// Requirements: HybridCar bmw = new HybridCar("BMW", "I8", 2018, 12000, 5, 40, 80);
// • Use the constructors you created in each class.
// • Provide realistic values (brand, model, year, mileage, etc.).
// • Store them in variables such as:
// ElectricCar tesla = new ElectricCar(...);
// ============================================================ // ============================================================
@@ -22,14 +22,7 @@ public class Main {
// ============================================================ // ============================================================
// TODO 2: Vehicle[] vehicles = {tesla, hyundai, bugatti, bmw};
// 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).
// ============================================================ // ============================================================
@@ -37,14 +30,13 @@ public class Main {
// ============================================================ // ============================================================
// TODO 3: System.out.println();
// Loop through the Vehicle[] array and: System.out.println("--- Basic Vehicle Information ---");
// • Call v.basicInfo(); for (int i = 0; i < vehicles.length; i++) {
// • Print a blank line after each. Vehicle v = vehicles[i];
// v.basicInfo();
// Purpose: System.out.println();
// Demonstrate that all subclasses share the same Vehicle }
// behavior and can call inherited methods.
// ============================================================ // ============================================================
@@ -52,14 +44,12 @@ public class Main {
// ============================================================ // ============================================================
// TODO 4: System.out.println("--- Detailed Inspection ---");
// Use VehicleInspector to inspect each vehicle: for (int i = 0; i < vehicles.length; i++) {
// Vehicle v = vehicles[i];
// VehicleInspector.inspect(v); VehicleInspector.inspect(v);
// System.out.println();
// Purpose: }
// • Demonstrate the use of `instanceof`
// • Show subclassspecific inspection messages
// ============================================================ // ============================================================
@@ -67,17 +57,7 @@ public class Main {
// ============================================================ // ============================================================
// TODO 5: Serviceable[] serviceables = {tesla, hyundai, bugatti, bmw};
// Create an array of Serviceable:
// Serviceable[] serviceables = { ... };
//
// This demonstrates **interfacebased polymorphism**.
// Every Car subtype implements Serviceable.
//
// IMPORTANT:
// • You must cast to Vehicle when needed:
// Vehicle v = (Vehicle) s;
//
// ============================================================ // ============================================================
@@ -85,25 +65,19 @@ public class Main {
// ============================================================ // ============================================================
// TODO 6: System.out.println("--- Service Result ---");
// Loop through the Serviceable[] array: for (int i = 0; i < serviceables.length; i++) {
// Serviceable s = serviceables[i];
// Steps inside loop: Vehicle v = (Vehicle) s;
//
// 1. Cast the current object to Vehicle: if (s.needsService(v)) {
// Vehicle v = (Vehicle) s; System.out.println("SERVICE REQUIRED for: " + v.getBrand() + " " + v.getModel());
// s.performService();
// 2. Check: } else {
// if (s.needsService(v)) { ... } System.out.println("No service needed for: " + v.getBrand() + " " + v.getModel());
// }
// 3. If true: System.out.println();
// - 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.
// ============================================================ // ============================================================
@@ -111,11 +85,7 @@ public class Main {
// ============================================================ // ============================================================
// TODO 7 (Optional): System.out.println("=== Workshop Completed ===");
// After everything is done, print a final message:
//
// System.out.println("=== Workshop Completed ===");
//
// ============================================================ // ============================================================
+21 -6
View File
@@ -15,17 +15,21 @@ public abstract class Car extends Vehicle implements Serviceable {
private int seats; private int seats;
public Car(String brand, String model, int year, public Car(String brand, String model, int year, double mileage, int seats) {
double mileage, int seats) {
// TODO: Call super constructor super(brand, model, year, mileage);
this.seats = seats; this.seats = seats;
} }
// TODO: Override getEnergyType() → return "Unknown" @Override
public String getEnergyType() {
return "Unknown";
}
// TODO: Override showDetails() @Override
// Print brand, model, year, mileage, and number of seats public void showDetails() {
System.out.println("Brand: " + getBrand() + ", Model: " + getModel() + ", Year: " + getYear() + ", Mileage: " + getMileage() + ", Seats: " + seats);
}
/* /*
* Implement Serviceable methods: * Implement Serviceable methods:
@@ -38,4 +42,15 @@ public abstract class Car extends Vehicle implements Serviceable {
* performService(): * performService():
* - Print: "General car service completed" * - Print: "General car service completed"
*/ */
@Override
public boolean needsService(Vehicle v) {
if (this.getMileage() > 10000) {return true;}
else {return false;}
}
@Override
public void performService() {
System.out.println("General car service completed");
}
} }
+32 -10
View File
@@ -18,22 +18,44 @@ public class ElectricCar extends Car implements Chargeable {
private int batteryCapacity; private int batteryCapacity;
private double chargeLevel; private double chargeLevel;
public ElectricCar(String brand, String model, int year, public ElectricCar(String brand, String model, int year, double mileage, int seats, int batteryCapacity, double chargeLevel) {
double mileage, int seats,
int batteryCapacity, double chargeLevel) {
super(brand, model, year, mileage, seats); super(brand, model, year, mileage, seats);
this.batteryCapacity = batteryCapacity; this.batteryCapacity = batteryCapacity;
this.chargeLevel = chargeLevel; this.chargeLevel = chargeLevel;
} }
// TODO: Override getEnergyType() → return "Electric" @Override
public String getEnergyType() {
return "Electric";
}
// TODO: Override showDetails() @Override
// Call super.showDetails() public void showDetails() {
// Print battery capacity and charge level super.showDetails();
System.out.println("Battery Capacity: " + batteryCapacity + " kWh, Charge Level: " + chargeLevel + "%");
}
// TODO: Implement charge(double amount) @Override
// Validate negative public void charge(double amount) {
// Cap at 100 if (amount < 0) {
System.out.println("Invalid charge amount.");
return;
}
this.chargeLevel += amount;
if (this.chargeLevel > 100) {
this.chargeLevel = 100;
}
}
@Override
public boolean needsService(Vehicle v) {
if (this.getMileage() > 8000) {return true;}
else {return false;}
}
@Override
public void performService() {
System.out.println("Battery system checked");
this.chargeLevel = 100;
}
} }
+32 -10
View File
@@ -18,22 +18,44 @@ public class GasCar extends Car implements Refuelable {
private double fuelLevel; private double fuelLevel;
private String fuelType; private String fuelType;
public GasCar(String brand, String model, int year, public GasCar(String brand, String model, int year, double mileage, int seats, double fuelLevel, String fuelType) {
double mileage, int seats,
double fuelLevel, String fuelType) {
super(brand, model, year, mileage, seats); super(brand, model, year, mileage, seats);
this.fuelLevel = fuelLevel; this.fuelLevel = fuelLevel;
this.fuelType = fuelType; this.fuelType = fuelType;
} }
// TODO: Override getEnergyType() → return fuelType + " (Gas)" @Override
public String getEnergyType() {
return fuelType;
}
// TODO: Implement refuel(double amount) @Override
// Validate amount public void refuel(double amount) {
// Cap at 100 if (amount <= 0) {
System.out.println("Invalid fuel amount.");
return;
}
this.fuelLevel += amount;
if (this.fuelLevel > 100) {
this.fuelLevel = 100;
}
}
// TODO: Override showDetails() @Override
// Call super.showDetails() public void showDetails() {
// Print fuel level and fuel type super.showDetails();
System.out.println("Fuel Level: " + fuelLevel + "%, Fuel Type: " + fuelType);
}
@Override
public boolean needsService(Vehicle v) {
if (this.getMileage() > 12000) {return true;}
else {return false;}
}
@Override
public void performService() {
System.out.println("Engine oil changed");
this.fuelLevel = 100;
}
} }
+65
View File
@@ -0,0 +1,65 @@
package org.model;
import org.behavior.Chargeable;
import org.behavior.Refuelable;
public class HybridCar extends Car implements Refuelable, Chargeable {
private double fuelLevel;
private double chargeLevel;
public HybridCar(String brand, String model, int year, double mileage, int seats, double fuelLevel, double chargeLevel) {
super(brand, model, year, mileage, seats);
this.fuelLevel = fuelLevel;
this.chargeLevel = chargeLevel;
}
@Override
public String getEnergyType() {
return "Hybrid (Gas/Electric)";
}
@Override
public void refuel(double amount) {
if (amount <= 0) {
System.out.println("Invalid fuel amount.");
return;
}
this.fuelLevel += amount;
if (this.fuelLevel > 100) {
this.fuelLevel = 100;
}
}
@Override
public void charge(double amount) {
if (amount <= 0) {
System.out.println("Invalid charge amount.");
return;
}
this.chargeLevel += amount;
if (this.chargeLevel > 100) {
this.chargeLevel = 100;
}
}
@Override
public void showDetails() {
super.showDetails();
System.out.println("Fuel Level: " + fuelLevel + "%, Charge Level: " + chargeLevel + "%");
}
@Override
public boolean needsService(Vehicle v) {
if (this.getMileage() > 10000) {return true;}
else {return false;}
}
@Override
public void performService() {
System.out.println("Engine oil changed and battery system checked");
this.fuelLevel = 100;
this.chargeLevel = 100;
}
}
+18 -9
View File
@@ -13,18 +13,27 @@ public class SportsCar extends GasCar {
private double zeroToHundred; private double zeroToHundred;
public SportsCar(String brand, String model, int year, public SportsCar(String brand, String model, int year, double mileage, int seats, double fuelLevel, String fuelType, double zeroToHundred) {
double mileage, int seats,
double fuelLevel, String fuelType,
double zeroToHundred) {
super(brand, model, year, mileage, seats, super(brand, model, year, mileage, seats, fuelLevel, fuelType);
fuelLevel, fuelType);
this.zeroToHundred = zeroToHundred; this.zeroToHundred = zeroToHundred;
} }
// TODO: Override showDetails() @Override
// Call super.showDetails() public void showDetails() {
// Print acceleration time super.showDetails();
System.out.println("0-100 km/h Acceleration: " + zeroToHundred + "s");
}
@Override
public boolean needsService(Vehicle v) {
if (this.getMileage() > 5000) {return true;}
else {return false;}
}
@Override
public void performService() {
System.out.println("High-performance brake system checked");
}
} }
+13 -10
View File
@@ -20,20 +20,23 @@ public abstract class Vehicle {
this.mileage = mileage; this.mileage = mileage;
} }
// TODO: Create getters for all fields public String getBrand() {return brand;}
public String getModel() {return model;}
public int getYear() {return year;}
public double getMileage() {return mileage;}
// TODO: Implement addMileage(double km) public void addMileage(double km) {
// If km is negative → print error if (km < 0) {
// Otherwise increase mileage System.out.println("Error: Cannot add negative mileage.");
} else {
this.mileage += km;
}
}
// TODO: Declare abstract method getEnergyType() public abstract String getEnergyType();
public void basicInfo() { public void basicInfo() {
System.out.println( System.out.println(brand + " " + model + " " + year + " | Mileage: " + mileage + " | Energy: " + getEnergyType());
year + " " + brand + " " + model +
" | Mileage: " + mileage +
" | Energy: " + getEnergyType()
);
} }
public abstract void showDetails(); public abstract void showDetails();
+13 -5
View File
@@ -1,6 +1,6 @@
package org.util; package org.util;
import org.*; import org.model.*;
import org.model.Vehicle; import org.model.Vehicle;
/* /*
@@ -14,9 +14,17 @@ public class VehicleInspector {
v.basicInfo(); v.basicInfo();
// TODO: if (v instanceof ElectricCar) {
// If ElectricCar → print "Check battery system" System.out.println("Check battery system");
// 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 oil");
}
else if (v instanceof HybridCar) {
System.out.println("Check engine oil and battery system");
}
} }
} }