Merge pull request 'develop' (#1) from develop into master

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-05-18 15:56:17 +00:00
8 changed files with 276 additions and 142 deletions
+42 -112
View File
@@ -1,126 +1,56 @@
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 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, 1000,
5, 100, 20);
GasCar toyota = new GasCar("Toyota", "Corolla", 2018, 13000,
5, 50, "Petrol");
SportsCar ferrari = new SportsCar("Ferrari", "F8 Tributo", 2021, 8000,
2, 100, "Petrol", 2.9);
Vehicle[] vehicles = {tesla, toyota, ferrari};
for (Vehicle v : vehicles)
{
v.basicInfo();
System.out.println();
}
for (Vehicle v : vehicles)
{
VehicleInspector.inspect(v);
}
// ============================================================
// 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 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 ===");
//
// ============================================================
Serviceable[] serviceables = {tesla, toyota, ferrari};
for (Serviceable s : serviceables)
{
Vehicle v = (Vehicle) s;
if (s.needsService(v))
{
System.out.println(v.getBrand() + " " + v.getModel());
s.performService();
}
else
{
System.out.println(v.getBrand() + " " + v.getModel() + " does not need service.");
}
System.out.println();
}
System.out.println("=== Workshop Completed ===");
}
}
+25 -4
View File
@@ -18,15 +18,36 @@ public abstract class Car extends Vehicle implements Serviceable {
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()
{
basicInfo();
System.out.println("number of seats: " + this.seats);
}
public boolean needsService(Vehicle v)
{
if (v.getMileage() > 10000)
{
return true;
}
return false;
}
public void performService()
{
System.out.println("General car service completed");
}
/*
* Implement Serviceable methods:
*
+38 -7
View File
@@ -27,13 +27,44 @@ public class ElectricCar extends Car implements Chargeable {
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: " + this.batteryCapacity + " | charge level: " + this.chargeLevel);
}
public void charge(double amount) {
if (amount < 0) {
System.out.println("Cannot charge negative amount!");
return;
}
this.chargeLevel += amount;
if (this.chargeLevel > 100) {
this.chargeLevel = 100;
}
}
@Override
public boolean needsService(Vehicle v)
{
if (v.getMileage() > 8000)
{
return true;
}
return false;
}
@Override
public void performService()
{
System.out.println("Battery system checked");
this.chargeLevel = 100;
}
// TODO: Implement charge(double amount)
// Validate negative
// Cap at 100
}
+43 -7
View File
@@ -27,13 +27,49 @@ public class GasCar extends Car implements Refuelable {
this.fuelType = fuelType;
}
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
@Override
public String getEnergyType()
{
return fuelType + "(Gas)";
}
// TODO: Implement refuel(double amount)
// Validate amount
// Cap at 100
public void refuel(double amount)
{
if (amount < 0)
{
System.out.println("Cannot refuel negative amount!");
return;
}
this.fuelLevel += amount;
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: " + this.fuelType + " | fuel level: " + this.fuelLevel);
}
@Override
public boolean needsService(Vehicle v)
{
if (v.getMileage() > 12000)
{
return true;
}
return false;
}
@Override
public void performService()
{
System.out.println("Engine oil changed");
this.fuelLevel = 100;
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print fuel level and fuel type
}
+62
View File
@@ -0,0 +1,62 @@
package org.model;
import org.behavior.Chargeable;
import org.behavior.Refuelable;
public class HybridCar extends Car implements Chargeable, Refuelable{
private double chargeLevel;
private double fuelLevel;
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 boolean needsService(Vehicle v)
{
if (v.getMileage() > 10000)
{
return true;
}
return false;
}
@Override
public void performService()
{
System.out.println("Engine oil changed and battery system checked");
this.chargeLevel = 100;
this.fuelLevel = 100;
}
public void charge(double amount) {
if (amount < 0) {
System.out.println("Cannot charge negative amount!");
return;
}
this.chargeLevel += amount;
if (this.chargeLevel > 100) {
this.chargeLevel = 100;
}
}
public void refuel(double amount)
{
if (amount < 0)
{
System.out.println("Cannot refuel negative amount!");
return;
}
this.fuelLevel += amount;
if (this.fuelLevel > 100)
{
this.fuelLevel = 100;
}
System.out.println("Refueled. Current fuel level: " + this.fuelLevel);
}
}
+23 -3
View File
@@ -24,7 +24,27 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred;
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print acceleration time
@Override
public void showDetails()
{
super.showDetails();
System.out.println("acceleration time: " + this.zeroToHundred);
}
@Override
public boolean needsService(Vehicle v)
{
if (v.getMileage() > 5000)
{
return true;
}
return false;
}
@Override
public void performService()
{
System.out.println("High-performance brake system checked");
}
}
+28 -5
View File
@@ -20,13 +20,36 @@ 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)
{
throw new IllegalArgumentException("Mileage cannot be negative!");
}
else
{
mileage += km;
}
}
// TODO: Declare abstract method getEnergyType()
public abstract String getEnergyType();
public void basicInfo() {
System.out.println(
+15 -4
View File
@@ -1,6 +1,9 @@
package org.util;
import org.*;
import org.model.ElectricCar;
import org.model.GasCar;
import org.model.SportsCar;
import org.model.Vehicle;
/*
@@ -14,9 +17,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 and oil");
}
}
}