2 Commits
11 changed files with 210 additions and 252 deletions
-126
View File
@@ -1,126 +0,0 @@
package org;
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(...);
// ============================================================
// ============================================================
// 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 ===");
//
// ============================================================
}
}
+24 -30
View File
@@ -1,41 +1,35 @@
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;
public abstract class Car extends Vehicle implements Serviceable {
private int seats;
public Car(String brand, String model, int year,
double mileage, int seats) {
// TODO: Call super constructor
public Car(String brand, String model, int year, double mileage, int seats) {
super(brand, model, year, mileage);
this.seats = seats;
}
// TODO: Override getEnergyType() → return "Unknown"
public int getSeats() {
return seats;
}
// TODO: Override showDetails()
// Print brand, model, year, mileage, and number of seats
@Override
public String getEnergyType() {
return "Unknown";
}
/*
* Implement Serviceable methods:
*
* needsService(Vehicle v):
* - Check the mileage of the current car
* - If mileage > 10000 return true
* - Otherwise return false
*
* performService():
* - Print: "General car service completed"
*/
@Override
public void showDetails() {
basicInfo();
System.out.println("Seats: " + seats);
}
@Override
public boolean needsService(Vehicle v) {
return v.getMileage() > 10000;
}
@Override
public void performService() {
System.out.println("General car service completed");
}
}
@@ -1,4 +1,4 @@
package org.behavior;
package org.model;
public interface Chargeable {
void charge(double amount);
+40 -22
View File
@@ -1,39 +1,57 @@
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;
public class ElectricCar extends Car implements Chargeable {
private int batteryCapacity;
private double chargeLevel;
public ElectricCar(String brand, String model, int year,
double mileage, int seats,
int batteryCapacity, double chargeLevel) {
super(brand, model, year, mileage, seats);
this.batteryCapacity = batteryCapacity;
this.chargeLevel = chargeLevel;
}
// TODO: Override getEnergyType() → return "Electric"
public int getBatteryCapacity() {
return batteryCapacity;
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print battery capacity and charge level
public double getChargeLevel() {
return chargeLevel;
}
// TODO: Implement charge(double amount)
// Validate negative
// Cap at 100
@Override
public String getEnergyType() {
return "Electric";
}
@Override
public void charge(double amount) {
if (amount < 0) {
System.out.println("Invalid charge amount.");
return;
}
chargeLevel += amount;
if (chargeLevel > 100) {
chargeLevel = 100;
}
}
@Override
public void showDetails() {
super.showDetails();
System.out.println("Battery Capacity: " + batteryCapacity);
System.out.println("Charge Level: " + chargeLevel);
}
@Override
public boolean needsService(Vehicle v) {
return v.getMileage() > 8000;
}
@Override
public void performService() {
System.out.println("Battery system checked");
chargeLevel = 100;
}
}
+41 -22
View File
@@ -1,39 +1,58 @@
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;
public class GasCar extends Car implements Refuelable {
private double fuelLevel;
private String fuelType;
public GasCar(String brand, String model, int year,
double mileage, int seats,
double fuelLevel, String fuelType) {
super(brand, model, year, mileage, seats);
this.fuelLevel = fuelLevel;
this.fuelType = fuelType;
}
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
public double getFuelLevel() {
return fuelLevel;
}
// TODO: Implement refuel(double amount)
// Validate amount
// Cap at 100
public String getFuelType() {
return fuelType;
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print fuel level and fuel type
@Override
public String getEnergyType() {
return fuelType + " (Gas)";
}
@Override
public void refuel(double amount) {
if (amount < 0) {
System.out.println("Invalid refuel amount.");
return;
}
fuelLevel += amount;
if (fuelLevel > 100) {
fuelLevel = 100;
}
}
@Override
public void showDetails() {
super.showDetails();
System.out.println("Fuel Level: " + fuelLevel);
System.out.println("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;
}
}
+44
View File
@@ -0,0 +1,44 @@
package org.model;
public class Main {
public static void main(String[] args) {
Vehicle[] vehicles = new Vehicle[3];
vehicles[0] = new ElectricCar("Tesla", "Model 3", 2024, 5000, 5, 75, 80);
vehicles[1] = new GasCar("Toyota", "Corolla", 2022, 13000, 5, 60, "Petrol");
vehicles[2] = new SportsCar("BMW", "M4", 2023, 6000, 4, 70, "Petrol", 3.8);
for (Vehicle v : vehicles) {
System.out.println("----- Basic Info -----");
v.basicInfo();
System.out.println();
System.out.println("----- Details -----");
v.showDetails();
System.out.println();
}
System.out.println("----- Inspection -----");
for (Vehicle v : vehicles) {
if (v instanceof SportsCar) {
System.out.println(v.getModel() + " is a SportsCar.");
} else if (v instanceof ElectricCar) {
System.out.println(v.getModel() + " is an ElectricCar.");
} else if (v instanceof GasCar) {
System.out.println(v.getModel() + " is a GasCar.");
} else {
System.out.println(v.getModel() + " is an unknown vehicle type.");
}
}
Serviceable[] serviceables = new Serviceable[3];
serviceables[0] = (Serviceable) vehicles[0];
serviceables[1] = (Serviceable) vehicles[1];
serviceables[2] = (Serviceable) vehicles[2];
System.out.println("----- Service -----");
for (Serviceable s : serviceables) {
s.performService();
}
}
}
@@ -1,4 +1,4 @@
package org.behavior;
package org.model;
public interface Refuelable {
void refuel(double amount);
@@ -1,10 +1,6 @@
package org.behavior;
import org.model.Vehicle;
package org.model;
public interface Serviceable {
void performService();
boolean needsService(Vehicle v);
}
+20 -17
View File
@@ -1,30 +1,33 @@
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 {
private double zeroToHundred;
public SportsCar(String brand, String model, int year,
double mileage, int seats,
double fuelLevel, String fuelType,
double zeroToHundred) {
super(brand, model, year, mileage, seats,
fuelLevel, fuelType);
super(brand, model, year, mileage, seats, fuelLevel, fuelType);
this.zeroToHundred = zeroToHundred;
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print acceleration time
public double getZeroToHundred() {
return zeroToHundred;
}
@Override
public void showDetails() {
super.showDetails();
System.out.println("0-100 km/h: " + zeroToHundred + " seconds");
}
@Override
public boolean needsService(Vehicle v) {
return v.getMileage() > 5000;
}
@Override
public void performService() {
System.out.println("High-performance brake system checked");
}
}
+28 -17
View File
@@ -1,13 +1,6 @@
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 {
private String brand;
private String model;
private int year;
@@ -20,21 +13,39 @@ public abstract class Vehicle {
this.mileage = mileage;
}
// TODO: Create getters for all fields
public String getBrand() {
return brand;
}
// TODO: Implement addMileage(double km)
// If km is negative → print error
// Otherwise increase mileage
public String getModel() {
return model;
}
// TODO: Declare abstract method getEnergyType()
public int getYear() {
return year;
}
public double getMileage() {
return mileage;
}
public void addMileage(double km) {
if (km < 0) {
System.out.println("Mileage cannot be negative.");
return;
}
mileage += km;
}
public void basicInfo() {
System.out.println(
year + " " + brand + " " + model +
" | Mileage: " + mileage +
" | Energy: " + getEnergyType()
);
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
System.out.println("Mileage: " + mileage);
System.out.println("Energy Type: " + getEnergyType());
}
public abstract String getEnergyType();
public abstract void showDetails();
}
+10 -11
View File
@@ -1,22 +1,21 @@
package org.util;
import org.*;
import org.model.Vehicle;
/*
* Utility class to inspect vehicles.
* Demonstrates instanceof and polymorphism.
*/
import org.model.ElectricCar;
import org.model.SportsCar;
import org.model.GasCar;
public class VehicleInspector {
public static void inspect(Vehicle v) {
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");
}
}
}