1 Commits
Author SHA1 Message Date
amirM.t 1a78323743 complete workshop assignments 2026-04-28 09:12:10 +03:30
7 changed files with 222 additions and 155 deletions
+67 -115
View File
@@ -1,126 +1,78 @@
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)
{
// Stage 1
// ============================================================ ElectricCar tesla = new ElectricCar("Tesla", "Model 3", 2023, 5000, 5, 75, 90.5);
// TODO 1: GasCar toyota = new GasCar("Toyota", "Camry", 2022, 15000, 5, 70, "Gasoline");
// Create one instance of each vehicle type: SportsCar porsche = new SportsCar("Porsche", "911", 2023, 7000, 2, 50, "Premium Gasoline", 3.2);
// - ElectricCar
// - GasCar System.out.println("--- Vehicle Instances Created ---");
// - SportsCar System.out.println();
//
// Requirements: // Stage 2
// • Use the constructors you created in each class.
// • Provide realistic values (brand, model, year, mileage, etc.). Vehicle[] vehicles = {tesla, toyota, porsche};
// • Store them in variables such as: System.out.println("--- Vehicle Array Created ---");
// ElectricCar tesla = new ElectricCar(...); System.out.println();
// ============================================================
// Stage 3
System.out.println("--- Displaying Basic Vehicle Info ---");
for (Vehicle v : vehicles)
{
// ============================================================ v.basicInfo();
// TODO 2: System.out.println();
// Create an array of Vehicle: }
// Vehicle[] vehicles = { ... }; System.out.println();
//
// This array MUST contain the objects created above. // Stage 4
// This demonstrates **polymorphism** because all different
// subclasses (ElectricCar, GasCar, SportsCar) are stored System.out.println("--- Inspecting Vehicles ---");
// as their parent type (Vehicle). for (Vehicle v : vehicles)
// ============================================================ {
VehicleInspector.inspect(v);
System.out.println(); // Blank line for readability
}
System.out.println();
// ============================================================
// 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 subclassspecific inspection messages
// ============================================================
// ============================================================
// 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;
//
// ============================================================
// ============================================================
// 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 ===");
//
// ============================================================
// Stage 5
Serviceable[] serviceables = {tesla, toyota, porsche};
System.out.println("--- Serviceable Array Created ---");
System.out.println();
// Stage 6
System.out.println("--- Performing Service Checks ---");
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(); // Blank line for readability
}
// Stage 7
System.out.println("=== Workshop Completed ===");
} }
} }
+23 -14
View File
@@ -18,23 +18,32 @@ 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" @Override
public String getEnergyType()
{
return "Unknown";
}
// TODO: Override showDetails() // Override showDetails()
@Override
public void showDetails()
{
System.out.println("Seats: " + seats);
}
/* @Override
* Implement Serviceable methods: public boolean needsService(Vehicle v)
* {
* needsService(Vehicle v): return v.getMileage() > 10000;
* - Check the mileage of the current car }
* - If mileage > 10000 return true
* - Otherwise return false @Override
* public void performService()
* performService(): {
* - Print: "General car service completed" System.out.println("General car service completed");
*/ }
} }
+43 -5
View File
@@ -27,11 +27,49 @@ 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()
// TODO: Implement charge(double amount) @Override
// Validate negative public void showDetails()
// Cap at 100 {
super.showDetails();
System.out.println("Battery Capacity: " + batteryCapacity + " kWh");
System.out.println("Charge Level: " + chargeLevel + "%");
}
@Override
public void charge(double amount)
{
if (amount < 0)
System.out.println("Error: Cannot charge with a negative amount.");
else
{
this.chargeLevel += amount;
if (this.chargeLevel > 100)
this.chargeLevel = 100;
System.out.println("Charged. Current charge level: " + this.chargeLevel + "%");
}
}
@Override
public boolean needsService(Vehicle v)
{
return v.getMileage() > 8000;
}
@Override
public void performService()
{
System.out.println("Battery system checked");
this.chargeLevel = 100; // Reset charge level
System.out.println("Charge level reset to 100%");
}
} }
+41 -5
View File
@@ -27,11 +27,47 @@ 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: Cannot refuel with a negative amount.");
else
{
this.fuelLevel += amount;
// TODO: Override showDetails() if (this.fuelLevel > 100)
this.fuelLevel = 100;
System.out.println("Refueled. Current fuel level: " + this.fuelLevel + "%");
}
}
@Override
public void showDetails()
{
super.showDetails();
System.out.println("Fuel Type: " + fuelType);
System.out.println("Fuel Level: " + fuelLevel + "%");
}
@Override
public boolean needsService(Vehicle v)
{
return v.getMileage() > 12000;
}
@Override
public void performService()
{
System.out.println("Engine oil changed");
this.fuelLevel = 100;
System.out.println("Fuel level reset to 100%");
}
} }
+18 -4
View File
@@ -24,8 +24,22 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred; this.zeroToHundred = zeroToHundred;
} }
// TODO: Override showDetails() @Override
// Print header public void showDetails()
// Call super.showDetails() {
// Print acceleration time super.showDetails();
System.out.println("0-100 km/h Acceleration: " + zeroToHundred + "s");
}
@Override
public boolean needsService(Vehicle v)
{
return v.getMileage() > 5000;
}
@Override
public void performService()
{
System.out.println("High-performance brake system checked");
}
} }
+18 -6
View File
@@ -20,15 +20,27 @@ public abstract class Vehicle {
this.mileage = mileage; this.mileage = mileage;
} }
// TODO: Create getters for all fields public String getBrand() {return brand;}
// TODO: Implement addMileage(double km) public String getModel() {return model;}
// If km is negative → print error
// Otherwise increase mileage
// TODO: Declare abstract method getEnergyType() public int getYear() {return year;}
public void basicInfo() { public double getMileage() {return mileage;}
public void addMileage(double km)
{
if (km < 0)
System.out.println("Error: Mileage cannot be negative.");
else
this.mileage += km;
}
public abstract String getEnergyType();
public void basicInfo()
{
System.out.println( System.out.println(
year + " " + brand + " " + model + year + " " + brand + " " + model +
" | Mileage: " + mileage + " | Mileage: " + mileage +
+12 -6
View File
@@ -1,6 +1,9 @@
package org.util; package org.util;
import org.*; import org.*;
import org.model.ElectricCar;
import org.model.GasCar;
import org.model.SportsCar;
import org.model.Vehicle; import org.model.Vehicle;
/* /*
@@ -10,13 +13,16 @@ import org.model.Vehicle;
public class VehicleInspector { public class VehicleInspector {
public static void inspect(Vehicle v) { public static void inspect(Vehicle v)
{
v.basicInfo(); v.basicInfo();
// TODO: switch (v)
// If ElectricCar → print "Check battery system" {
// If SportsCar → print "Check brakes and performance" case ElectricCar electricCar -> System.out.println("Check battery system");
// If GasCar → print "Check engine and oil" case SportsCar sportsCar -> System.out.println("Check brakes and performance");
case GasCar gasCar -> System.out.println("Check engine and oil");
default -> {}
}
} }
} }