Implemented all of the TODO sections, Added HybridCar class and its methods, removed comments.

This commit is contained in:
2026-04-27 00:04:49 +03:30
parent e8aedb8354
commit 33a11d54c7
8 changed files with 214 additions and 193 deletions
+32 -110
View File
@@ -1,125 +1,47 @@
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) {
// ============================================================ ElectricCar tesla = new ElectricCar("Tesla","Model Y",2020,120.8,4,42000,25);
// TODO 1: GasCar bmw = new GasCar("BMW","M5",2025,2120,4,40,"Gasoline");
// Create one instance of each vehicle type: SportsCar koenigsegg = new SportsCar("Koenigsegg","Jesko",2023,12,2,50,"Gasoline",2.3);
// - 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(...);
// ============================================================
Vehicle[] vehicles = {tesla,bmw,koenigsegg};
for (Vehicle v : vehicles){
v.basicInfo();
System.out.println("-------------------------------");
}
for (Vehicle v : vehicles){
VehicleInspector.inspect(v);
}
Serviceable[] serviceables = {tesla,bmw,koenigsegg};
// ============================================================ for (Serviceable s : serviceables){
// TODO 2: Vehicle v = (Vehicle) s;
// 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 ===");
//
// ============================================================
if (s.needsService(v)){
System.out.println("Brand : " + v.getBrand());
System.out.println("Model : " + v.getModel() );
s.performService();
}
else {
System.out.println("The car does not need service");
}
System.out.println("-------------------------------");
}
System.out.println("=== Workshop Completed ===");
} }
+23 -23
View File
@@ -1,14 +1,5 @@
package org.model; package org.model;
/*
* Car extends Vehicle.
* This class SHOULD implement Serviceable.
*
* Service logic for Car:
* - needsService(): return true if mileage > 10,000
* - performService(): print "General car service completed"
*/
import org.behavior.Serviceable; import org.behavior.Serviceable;
public abstract class Car extends Vehicle implements Serviceable { public abstract class Car extends Vehicle implements Serviceable {
@@ -18,23 +9,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
* Implement Serviceable methods: public void showDetails() {
* System.out.println("Seats Count : " + this.seats);
* needsService(Vehicle v): }
* - Check the mileage of the current car
* - If mileage > 10000 return true @Override
* - Otherwise return false public boolean needsService(Vehicle v) {
* if (v.getMileage() > 10000){
* performService(): return true;
* - Print: "General car service completed" }
*/ return false;
}
@Override
public void performService() {
System.out.println("General car service completed");
}
} }
+33 -16
View File
@@ -1,16 +1,5 @@
package org.model; package org.model;
/*
* ElectricCar extends Car.
* It already inherits Serviceable from Car.
*
* Service logic for ElectricCar:
* - needsService(): return true if mileage > 8000
* - performService():
* Print "Battery system checked"
* Reset chargeLevel to 100
*/
import org.behavior.Chargeable; import org.behavior.Chargeable;
public class ElectricCar extends Car implements Chargeable { public class ElectricCar extends Car implements Chargeable {
@@ -27,11 +16,39 @@ 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
public void showDetails() {
System.out.println("Battery capacity : " + this.batteryCapacity);
System.out.println("Charge level : " + this.chargeLevel);
}
// TODO: Implement charge(double amount) public void charge(double amount){
// Validate negative if (amount <= 0){
// Cap at 100 System.out.println("Charging amount should be positive.");
return;
}
if (chargeLevel + amount > 100){
System.out.println("Battery Full!");
return;
}
}
@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;
}
} }
+33 -16
View File
@@ -1,16 +1,5 @@
package org.model; package org.model;
/*
* GasCar extends Car.
* Inherits Serviceable from Car.
*
* Service logic for GasCar:
* - needsService(): return true if mileage > 12000
* - performService():
* Print "Engine oil changed"
* Reset fuelLevel to 100
*/
import org.behavior.Refuelable; import org.behavior.Refuelable;
public class GasCar extends Car implements Refuelable { public class GasCar extends Car implements Refuelable {
@@ -27,11 +16,39 @@ public class GasCar extends Car implements Refuelable {
this.fuelType = fuelType; this.fuelType = fuelType;
} }
// TODO: Override getEnergyType() → return fuelType + " (Gas)" @Override
public String getEnergyType() {
return this.fuelType + " (Gas)";
}
// TODO: Implement refuel(double amount) public void refuel(double amount){
// Validate amount if (amount <= 0){
// Cap at 100 System.out.println("Amount of fuel should be positive");
return;
}
if (this.fuelLevel + amount > 100){
System.out.println("Out of storage!");
return;
}
}
// TODO: Override showDetails() @Override
public void showDetails() {
System.out.println("Fuel Level : " + this.fuelLevel);
System.out.println("Fuel Type : " + this.fuelType);
}
@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;
}
} }
+43
View File
@@ -0,0 +1,43 @@
package org.model;
import org.behavior.Chargeable;
import org.behavior.Refuelable;
public class HybridCar extends Car implements Refuelable, Chargeable{
private int batteryCapacity;
private double chargeLevel;
private double fuelLevel;
public HybridCar(String brand, String model, int year,
double mileage, int seats,
int batteryCapacity, double chargeLevel, double fuelLevel){
super(brand, model, year, mileage, seats);
this.batteryCapacity = batteryCapacity;
this.chargeLevel = chargeLevel;
this.fuelLevel = fuelLevel;
}
@Override
public void charge(double amount){
if (amount <= 0){
System.out.println("Charging amount should be positive.");
return;
}
if (chargeLevel + amount > 100){
System.out.println("Battery Full!");
return;
}
}
@Override
public void refuel(double amount){
if (amount <= 0){
System.out.println("Amount of fuel should be positive");
return;
}
if (this.fuelLevel + amount > 100){
System.out.println("Out of storage!");
return;
}
}
}
+19 -13
View File
@@ -1,14 +1,5 @@
package org.model; package org.model;
/*
* SportsCar extends GasCar.
*
* Special service logic for SportsCar:
* - needsService(): return true if mileage > 5000
* - performService():
* Print "High-performance brake system checked"
*/
public class SportsCar extends GasCar { public class SportsCar extends GasCar {
private double zeroToHundred; private double zeroToHundred;
@@ -24,8 +15,23 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred; this.zeroToHundred = zeroToHundred;
} }
// TODO: Override showDetails() @Override
// Print header public void showDetails() {
// Call super.showDetails() System.out.println("===SportsCar===");
// Print acceleration time super.showDetails();
System.out.println("0-100 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");
}
} }
+21 -11
View File
@@ -1,11 +1,5 @@
package org.model; package org.model;
/*
* Abstract base class for all vehicles.
* This class DOES NOT implement Serviceable.
* Service behavior will be defined in concrete subclasses.
*/
public abstract class Vehicle { public abstract class Vehicle {
private String brand; private String brand;
@@ -20,13 +14,29 @@ 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 can't be negative");
return;
}
this.mileage += km;
}
// TODO: Declare abstract method getEnergyType()
public abstract String getEnergyType();
public void basicInfo() { public void basicInfo() {
System.out.println( System.out.println(
+10 -4
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;
/* /*
@@ -14,9 +17,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 GasCar) {
// If GasCar → print "Check engine and oil" System.out.println("Check engine and oil");
} else if (v instanceof SportsCar) {
System.out.println("Check brakes and performance");
}
} }
} }