1 Commits
Author SHA1 Message Date
faraz_ardeh 7bd320afbf Add completed exercises to develop branch 2026-07-12 17:00:07 +03:30
8 changed files with 217 additions and 138 deletions
+51 -96
View File
@@ -1,126 +1,81 @@
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: // TODO 1: Create vehicle instances
// 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, 1000,
5, 100, 75
);
GasCar toyota = new GasCar(
"Toyota", "Corolla", 2018, 85000,
5, 50, "Petrol"
);
SportsCar ferrari = new SportsCar(
"Ferrari", "F8 Tributo", 2021, 12000,
2, 60, "Premium", 2.9
);
HybridCar prius = new HybridCar(
"Toyota", "Prius", 2023, 15000,
5, 40, 60
);
// ============================================================ // ============================================================
// TODO 2: // TODO 2: Create array of Vehicle (polymorphism)
// 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, toyota, ferrari, prius };
// ============================================================ // ============================================================
// TODO 3: // TODO 3: Loop through vehicles and print basic info
// 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("=== Vehicle Basic Info ===");
for (Vehicle v : vehicles) {
v.basicInfo();
System.out.println();
}
// ============================================================ // ============================================================
// TODO 4: // TODO 4: Inspect each vehicle using VehicleInspector
// Use VehicleInspector to inspect each vehicle:
//
// VehicleInspector.inspect(v);
//
// Purpose:
// • Demonstrate the use of `instanceof`
// • Show subclassspecific inspection messages
// ============================================================ // ============================================================
System.out.println("=== Vehicle Inspection ===");
for (Vehicle v : vehicles) {
VehicleInspector.inspect(v);
System.out.println();
}
// ============================================================ // ============================================================
// TODO 5: // TODO 5: Create array of Serviceable
// 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, toyota, ferrari, prius };
// ============================================================ // ============================================================
// TODO 6: // TODO 6: Loop through serviceables and perform service
// 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 Check ===");
for (Serviceable s : serviceables) {
Vehicle v = (Vehicle) s;
if (s.needsService(v)) {
System.out.println(v.getBrand() + " " + v.getModel() + " needs service:");
s.performService();
} else {
System.out.println(v.getBrand() + " " + v.getModel() + " does not need service.");
}
System.out.println();
}
// ============================================================ // ============================================================
// TODO 7 (Optional): // TODO 7: Final message
// After everything is done, print a final message:
//
// System.out.println("=== Workshop Completed ===");
//
// ============================================================ // ============================================================
System.out.println("=== Workshop Completed ===");
} }
} }
+20 -15
View File
@@ -18,24 +18,29 @@ 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) {
// TODO: Call super constructor super(brand, model, year, mileage);
this.seats = seats; this.seats = seats;
} }
// TODO: Override getEnergyType() → return "Unknown" public int getSeats() { return seats; }
// TODO: Override showDetails() @Override
// Print brand, model, year, mileage, and number of seats public String getEnergyType() { return "Unknown"; }
/* @Override
* Implement Serviceable methods: public void showDetails() {
* System.out.println("Brand: " + getBrand() + " | Model: " + getModel() +
* needsService(Vehicle v): " | Year: " + getYear() + " | Mileage: " + getMileage() +
* - Check the mileage of the current car " | Seats: " + seats);
* - If mileage > 10000 return true }
* - Otherwise return false
* @Override
* performService(): public boolean needsService(Vehicle v) {
* - Print: "General car service completed" return v.getMileage() > 10000;
*/ }
@Override
public void performService() {
System.out.println("General car service completed");
}
} }
+26 -7
View File
@@ -27,13 +27,32 @@ public class ElectricCar extends Car implements Chargeable {
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("Error: charge amount cannot be negative.");
return;
}
chargeLevel = Math.min(100, chargeLevel + amount);
}
@Override
public boolean needsService(Vehicle v) {
return v.getMileage() > 8000;
}
@Override
public void performService() {
System.out.println("Battery system checked");
chargeLevel = 100;
}
} }
+26 -7
View File
@@ -27,13 +27,32 @@ public class GasCar extends Car implements Refuelable {
this.fuelType = fuelType; this.fuelType = fuelType;
} }
// TODO: Override getEnergyType() → return fuelType + " (Gas)" @Override
public String getEnergyType() { return fuelType + " (Gas)"; }
// TODO: Implement refuel(double amount) @Override
// Validate amount public void refuel(double amount) {
// Cap at 100 if (amount < 0) {
System.out.println("Error: refuel amount cannot be negative.");
return;
}
fuelLevel = Math.min(100, fuelLevel + amount);
}
// 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) {
return v.getMileage() > 12000;
}
@Override
public void performService() {
System.out.println("Engine oil changed");
fuelLevel = 100;
}
} }
+57
View File
@@ -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 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"; }
@Override
public void refuel(double amount) {
if (amount < 0) {
System.out.println("Error: refuel amount cannot be negative.");
return;
}
fuelLevel = Math.min(100, fuelLevel + amount);
}
@Override
public void charge(double amount) {
if (amount < 0) {
System.out.println("Error: charge amount cannot be negative.");
return;
}
chargeLevel = Math.min(100, chargeLevel + amount);
}
@Override
public void showDetails() {
super.showDetails();
System.out.println("Fuel Level: " + fuelLevel + "% | Charge Level: " + chargeLevel + "%");
}
@Override
public boolean needsService(Vehicle v) {
return v.getMileage() > 10000;
}
@Override
public void performService() {
System.out.println("Engine oil changed and battery system checked");
fuelLevel = 100;
chargeLevel = 100;
}
}
+15 -3
View File
@@ -24,7 +24,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("0-100 km/h: " + zeroToHundred + "s");
}
@Override
public boolean needsService(Vehicle v) {
return v.getMileage() > 5000;
}
@Override
public void performService() {
System.out.println("High-performance brake system checked");
}
} }
+12 -5
View File
@@ -20,13 +20,20 @@ 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: mileage cannot be negative.");
} else {
mileage += km;
}
}
// TODO: Declare abstract method getEnergyType() public abstract String getEnergyType();
public void basicInfo() { public void basicInfo() {
System.out.println( System.out.println(
+10 -5
View File
@@ -1,6 +1,8 @@
package org.util; package org.util;
import org.*; import org.model.ElectricCar;
import org.model.GasCar;
import org.model.SportsCar;
import org.model.Vehicle; import org.model.Vehicle;
/* /*
@@ -14,9 +16,12 @@ 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" } else if (v instanceof SportsCar) {
// If GasCar → print "Check engine and oil" System.out.println("Check brakes and performance");
} else if (v instanceof GasCar) {
System.out.println("Check engine and oil");
}
} }
} }