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;
import org.behavior.Serviceable;
import org.model.*;
import org.util.VehicleInspector;
public class Main {
public static void main(String[] args) {
// ============================================================
// TODO 1:
// Create one instance of each vehicle type:
// - ElectricCar
// - GasCar
// - SportsCar
//
// Requirements:
// • 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(...);
ElectricCar tesla = new ElectricCar("Tesla", "Model S", 2022, 85000, 5, 550, 50);
GasCar hyundai = new GasCar("Hyundai", "Sonata", 2014, 18000, 5, 80, "Super Petrol");
SportsCar bugatti = new SportsCar("Bugatti", "Chiron", 2026, 1000, 2, 60, "Premium", 2.4);
HybridCar bmw = new HybridCar("BMW", "I8", 2018, 12000, 5, 40, 80);
// ============================================================
@@ -22,14 +22,7 @@ public class Main {
// ============================================================
// 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).
Vehicle[] vehicles = {tesla, hyundai, bugatti, bmw};
// ============================================================
@@ -37,14 +30,13 @@ public class Main {
// ============================================================
// 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.
System.out.println();
System.out.println("--- Basic Vehicle Information ---");
for (int i = 0; i < vehicles.length; i++) {
Vehicle v = vehicles[i];
v.basicInfo();
System.out.println();
}
// ============================================================
@@ -52,14 +44,12 @@ public class Main {
// ============================================================
// TODO 4:
// Use VehicleInspector to inspect each vehicle:
//
// VehicleInspector.inspect(v);
//
// Purpose:
// • Demonstrate the use of `instanceof`
// • Show subclassspecific inspection messages
System.out.println("--- Detailed Inspection ---");
for (int i = 0; i < vehicles.length; i++) {
Vehicle v = vehicles[i];
VehicleInspector.inspect(v);
System.out.println();
}
// ============================================================
@@ -67,17 +57,7 @@ public class Main {
// ============================================================
// TODO 5:
// 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;
//
Serviceable[] serviceables = {tesla, hyundai, bugatti, bmw};
// ============================================================
@@ -85,25 +65,19 @@ public class Main {
// ============================================================
// 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.
System.out.println("--- Service Result ---");
for (int i = 0; i < serviceables.length; i++) {
Serviceable s = serviceables[i];
Vehicle v = (Vehicle) s;
if (s.needsService(v)) {
System.out.println("SERVICE REQUIRED for: " + v.getBrand() + " " + v.getModel());
s.performService();
} else {
System.out.println("No service needed for: " + v.getBrand() + " " + v.getModel());
}
System.out.println();
}
// ============================================================
@@ -111,11 +85,7 @@ public class Main {
// ============================================================
// TODO 7 (Optional):
// After everything is done, print a final message:
//
// System.out.println("=== Workshop Completed ===");
//
System.out.println("=== Workshop Completed ===");
// ============================================================
+21 -6
View File
@@ -15,17 +15,21 @@ public abstract class Car extends Vehicle implements Serviceable {
private int seats;
public Car(String brand, String model, int year,
double mileage, int seats) {
public Car(String brand, String model, int year, double mileage, int seats) {
// TODO: Call super constructor
super(brand, model, year, mileage);
this.seats = seats;
}
// TODO: Override getEnergyType() → return "Unknown"
@Override
public String getEnergyType() {
return "Unknown";
}
// TODO: Override showDetails()
// Print brand, model, year, mileage, and number of seats
@Override
public void showDetails() {
System.out.println("Brand: " + getBrand() + ", Model: " + getModel() + ", Year: " + getYear() + ", Mileage: " + getMileage() + ", Seats: " + seats);
}
/*
* Implement Serviceable methods:
@@ -38,4 +42,15 @@ public abstract class Car extends Vehicle implements Serviceable {
* performService():
* - 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 double chargeLevel;
public ElectricCar(String brand, String model, int year,
double mileage, int seats,
int batteryCapacity, double chargeLevel) {
public ElectricCar(String brand, String model, int year, double mileage, int seats, int batteryCapacity, double chargeLevel) {
super(brand, model, year, mileage, seats);
this.batteryCapacity = batteryCapacity;
this.chargeLevel = chargeLevel;
}
// TODO: Override getEnergyType() → return "Electric"
@Override
public String getEnergyType() {
return "Electric";
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print battery capacity and charge level
@Override
public void showDetails() {
super.showDetails();
System.out.println("Battery Capacity: " + batteryCapacity + " kWh, Charge Level: " + chargeLevel + "%");
}
// TODO: Implement charge(double amount)
// Validate negative
// Cap at 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 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 String fuelType;
public GasCar(String brand, String model, int year,
double mileage, int seats,
double fuelLevel, String fuelType) {
public GasCar(String brand, String model, int year, double mileage, int seats, double fuelLevel, String fuelType) {
super(brand, model, year, mileage, seats);
this.fuelLevel = fuelLevel;
this.fuelType = fuelType;
}
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
@Override
public String getEnergyType() {
return fuelType;
}
// TODO: Implement refuel(double amount)
// Validate amount
// Cap at 100
@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;
}
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print fuel level and fuel type
@Override
public void showDetails() {
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;
public SportsCar(String brand, String model, int year,
double mileage, int seats,
double fuelLevel, String fuelType,
double zeroToHundred) {
public SportsCar(String brand, String model, int year, double mileage, int seats, double fuelLevel, String fuelType, double zeroToHundred) {
super(brand, model, year, mileage, seats,
fuelLevel, fuelType);
super(brand, model, year, mileage, seats, fuelLevel, fuelType);
this.zeroToHundred = zeroToHundred;
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print acceleration time
@Override
public void showDetails() {
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;
}
// 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)
// If km is negative → print error
// Otherwise increase mileage
public void addMileage(double km) {
if (km < 0) {
System.out.println("Error: Cannot add negative mileage.");
} else {
this.mileage += km;
}
}
// TODO: Declare abstract method getEnergyType()
public abstract String getEnergyType();
public void basicInfo() {
System.out.println(
year + " " + brand + " " + model +
" | Mileage: " + mileage +
" | Energy: " + getEnergyType()
);
System.out.println(brand + " " + model + " " + year + " | Mileage: " + mileage + " | Energy: " + getEnergyType());
}
public abstract void showDetails();
+13 -5
View File
@@ -1,6 +1,6 @@
package org.util;
import org.*;
import org.model.*;
import org.model.Vehicle;
/*
@@ -14,9 +14,17 @@ public class VehicleInspector {
v.basicInfo();
// TODO:
// If ElectricCar → print "Check battery system"
// If SportsCar → print "Check brakes and performance"
// If GasCar → print "Check engine and oil"
if (v instanceof ElectricCar) {
System.out.println("Check battery system");
}
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");
}
}
}