1 Commits
8 changed files with 314 additions and 209 deletions
+51 -111
View File
@@ -1,126 +1,66 @@
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) {
// ============================================================ ElectricCar tesla = new ElectricCar(
// TODO 1: "Tesla", "Model S", 2022, 1000,
// Create one instance of each vehicle type: 100, 550, 50 // batteryCapacity, range
// - ElectricCar );
// - GasCar
// - SportsCar GasCar toyota = new GasCar(
// "Toyota", "Corolla", 2018, 85000,
// Requirements: 5, 50, "Petrol" // seats, fuelCapacity, fuelType
// • Use the constructors you created in each class. );
// • Provide realistic values (brand, model, year, mileage, etc.).
// • Store them in variables such as: SportsCar ferrari = new SportsCar(
// ElectricCar tesla = new ElectricCar(...); "Ferrari", "F8 Tributo", 2021, 12000,
// ============================================================ 2, 3.9, "oil", 710 // seats, engineSize, horsepower
);
HybridCar hybridCar = new HybridCar(
"Toyota", "Prius", 2026, 23000,
5, 67, "Petrol", 500, 34.5
);
Vehicle[] vehicles = {tesla, toyota, ferrari, hybridCar};
System.out.println("the provided vehicles' info is as follows:\n");
for (Vehicle v : vehicles) {
v.basicInfo();
System.out.println();
}
System.out.println("Inspecting the vehicles");
for (Vehicle v : vehicles){
VehicleInspector.inspect(v);
System.out.println();
}
Serviceable[] serviceables = {tesla, toyota, ferrari, hybridCar};
// ============================================================
// 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 ===");
//
// ============================================================
for (Serviceable car : serviceables){
Vehicle v = (Vehicle) car;
if (car.needsService(v)) {
System.out.println(v.getBrand() + " " + v.getModel());
car.performService();
}
else {
System.out.println("this car: " + v.getBrand() + " " + v.getModel() + "\ndoes not require to be serviced");
}
System.out.println();
}
System.out.println("=== Workshop Completed ===");
} }
} }
+28 -25
View File
@@ -1,41 +1,44 @@
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 {
private int seats; private final int seats;
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
// Print brand, model, year, mileage, and number of seats public void showDetails()
{
basicInfo();
/* System.out.println(
* Implement Serviceable methods: "Number of seats: " + 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) {
* double carMileage = getMileage();
* performService():
* - Print: "General car service completed" return carMileage > 10000;
*/ }
@Override
public void performService() {
System.out.println("General car service completed");
}
} }
+42 -19
View File
@@ -1,21 +1,10 @@
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 {
private int batteryCapacity; private final int batteryCapacity;
private double chargeLevel; private double chargeLevel;
public ElectricCar(String brand, String model, int year, public ElectricCar(String brand, String model, int year,
@@ -27,13 +16,47 @@ 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();
// TODO: Implement charge(double amount) System.out.println(
// Validate negative "Battery capacity: " + batteryCapacity +
// Cap at 100 "| Charge level: " + chargeLevel
);
}
@Override
public void charge(double amount) {
if (amount <= 0) {
System.out.println("An error occurred while charging the vehicle, value must be bigger than zero, " + amount + " is not");
}
if (chargeLevel + amount > 100) {
System.out.println("An error occurred while charging the vehicle, by adding this amount: " + amount + " vehicle's charge level exceeds battery capacity");
}
chargeLevel += amount;
}
@Override
public void performService() {
super.performService();
System.out.println("Battery system checked");
chargeLevel = 100;
}
@Override
public boolean needsService(Vehicle v) {
return getMileage() > 8000;
}
} }
+42 -19
View File
@@ -1,22 +1,11 @@
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 {
private double fuelLevel; private double fuelLevel;
private String fuelType; private final String fuelType;
public GasCar(String brand, String model, int year, public GasCar(String brand, String model, int year,
double mileage, int seats, double mileage, int seats,
@@ -27,13 +16,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("An error occurred while refueling the vehicle, value must be bigger than zero, " + amount + " is not");
}
// TODO: Override showDetails() if (fuelLevel + amount > 100) {
// Call super.showDetails() System.out.println("An error occurred while refueling the vehicle, by adding this amount: " + amount + " vehicle's charge level exceeds battery capacity");
// Print fuel level and fuel type }
fuelLevel += amount;
}
@Override
public void showDetails() {
super.showDetails();
System.out.println(
"Fuel level: " + fuelLevel +
"| Fuel type: " + fuelType
);
}
@Override
public boolean needsService(Vehicle v) {
double mileage = getMileage();
return mileage > 12000;
}
@Override
public void performService() {
super.performService();
System.out.println("Engine oil checked");
fuelLevel = 100;
}
} }
+85
View File
@@ -0,0 +1,85 @@
package org.model;
import org.behavior.Chargeable;
import org.behavior.Refuelable;
public class HybridCar extends Car implements Chargeable, Refuelable {
private double fuelLevel;
private final String fuelType;
private final int batteryCapacity;
private double chargeLevel;
public HybridCar(String brand, String model, int year,
double mileage, int seats,
double fuelLevel, String fuelType,
int batteryCapacity, double chargeLevel) {
super(brand, model, year, mileage, seats);
this.fuelLevel = fuelLevel;
this.fuelType = fuelType;
this.batteryCapacity = batteryCapacity;
this.chargeLevel = chargeLevel;
}
@Override
public String getEnergyType() {
return "Hybrid (Electric + " + fuelType + ")";
}
@Override
public void showDetails() {
super.showDetails();
System.out.println(
"Fuel level: " + fuelLevel +
"| Fuel type: " + fuelType
);
System.out.println(
"Battery capacity: " + batteryCapacity +
"| Charge level: " + chargeLevel
);
}
@Override
public void charge(double amount) {
if (amount <= 0) {
System.out.println("An error occurred while charging the vehicle, value must be bigger than zero, " + amount + " is not");
}
if (chargeLevel + amount > 100) {
System.out.println("An error occurred while charging the vehicle, by adding this amount: " + amount + " vehicle's charge level exceeds battery capacity");
}
chargeLevel += amount;
}
@Override
public void refuel(double amount) {
if (amount <= 0) {
System.out.println("An error occurred while refueling the vehicle, value must be bigger than zero, " + amount + " is not");
}
if (fuelLevel + amount > 100) {
System.out.println("An error occurred while refueling the vehicle, by adding this amount: " + amount + " vehicle's charge level exceeds battery capacity");
}
fuelLevel += amount;
}
@Override
public void performService() {
super.performService();
System.out.println("Engine oil changed and battery system checked");
fuelLevel = 100;
chargeLevel = 100;
}
@Override
public boolean needsService(Vehicle v) {
return getMileage() > 10000;
}
}
+20 -12
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,7 +15,24 @@ 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(
"Acceleration time: " + zeroToHundred
);
}
@Override
public boolean needsService(Vehicle v) {
return getMileage() > 5000;
}
@Override
public void performService() {
super.performService();
System.out.println("High-performance brake system checked");
}
} }
+29 -13
View File
@@ -1,16 +1,10 @@
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 final String brand;
private String model; private final String model;
private int year; private final int year;
private double mileage; private double mileage;
public Vehicle(String brand, String model, int year, double mileage) { public Vehicle(String brand, String model, int year, double mileage) {
@@ -21,12 +15,34 @@ public abstract class Vehicle {
} }
// TODO: Create getters for all fields // TODO: Create getters for all fields
public String getBrand()
{
return brand;
}
// TODO: Implement addMileage(double km) public double getMileage() {
// If km is negative → print error return mileage;
// Otherwise increase mileage }
// TODO: Declare abstract method getEnergyType() public int getYear() {
return year;
}
public String getModel() {
return model;
}
public void addMileage(double km)
{
if (km <= 0)
{
System.out.println("An error occurred while adding mileage to the vehicle, value must be bigger than zero, " + km + " is not");
}
mileage += km;
}
public abstract String getEnergyType();
public void basicInfo() { public void basicInfo() {
System.out.println( System.out.println(
+17 -10
View File
@@ -1,12 +1,8 @@
package org.util; package org.util;
import org.*; import org.*;
import org.model.Vehicle; import org.behavior.Serviceable;
import org.model.*;
/*
* Utility class to inspect vehicles.
* Demonstrates instanceof and polymorphism.
*/
public class VehicleInspector { public class VehicleInspector {
@@ -14,9 +10,20 @@ 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" }
// If GasCar → print "Check engine and oil" else if (v instanceof SportsCar){
System.out.println("Check brakes and performance");
}
else if (v instanceof GasCar){
System.out.println("Check engine and oil");
}
else if (v instanceof HybridCar){
System.out.println("Check battery system, engine and oil");
}
else {
System.out.println("This type of vehicle is not supported");
}
} }
} }