Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a78323743 |
@@ -41,9 +41,9 @@ The project consists of the following classes and interfaces:
|
||||
| GasCar | | ElectricCar |
|
||||
| (Class) | | (Class) |
|
||||
+-------------------+ +-------------------+
|
||||
^ | |
|
||||
^ ^ ^
|
||||
| extends | implements | implements
|
||||
| ⌄ ⌄
|
||||
| | |
|
||||
+-------------------+ +-------------------+ +-------------------+
|
||||
| SportsCar | | Refuelable | | Chargeable |
|
||||
| (Class) | | (Interface) | | (Interface) |
|
||||
@@ -118,14 +118,7 @@ Add a `HybridCar` class
|
||||
- Create a `HybridCar` class that extends `Car`.
|
||||
- It will need variables for both battery level and fuel level
|
||||
- Implement `Refuelable` and `Chargeable` together
|
||||
- Service logic for HybridCar:
|
||||
```
|
||||
needsService(): return true if mileage > 10000
|
||||
performService():
|
||||
Print "Engine oil changed and battery system checked"
|
||||
Reset fuelLevel to 100
|
||||
Reset chargeLevel to 100
|
||||
```
|
||||
- (Optional) Override methods to manage both power sources (driving drains battery first, then gas)
|
||||
|
||||
## Workflow
|
||||
1. Fork the repository to your own Git account
|
||||
@@ -140,6 +133,6 @@ git checkout -b develop
|
||||
```
|
||||
git push origin develop
|
||||
```
|
||||
7. Create a Pull Request from `develop` to `main` or `master`
|
||||
7. Create a Pull Request from `develop` to `main`
|
||||
|
||||
# GOOD LUCK! :)
|
||||
|
||||
+67
-115
@@ -1,126 +1,78 @@
|
||||
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) {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// Stage 1
|
||||
|
||||
// ============================================================
|
||||
// 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 subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 5:
|
||||
// Create an array of Serviceable:
|
||||
// Serviceable[] serviceables = { ... };
|
||||
//
|
||||
// This demonstrates **interface‑based 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 ===");
|
||||
//
|
||||
// ============================================================
|
||||
ElectricCar tesla = new ElectricCar("Tesla", "Model 3", 2023, 5000, 5, 75, 90.5);
|
||||
GasCar toyota = new GasCar("Toyota", "Camry", 2022, 15000, 5, 70, "Gasoline");
|
||||
SportsCar porsche = new SportsCar("Porsche", "911", 2023, 7000, 2, 50, "Premium Gasoline", 3.2);
|
||||
|
||||
System.out.println("--- Vehicle Instances Created ---");
|
||||
System.out.println();
|
||||
|
||||
// Stage 2
|
||||
|
||||
Vehicle[] vehicles = {tesla, toyota, porsche};
|
||||
System.out.println("--- Vehicle Array Created ---");
|
||||
System.out.println();
|
||||
|
||||
// Stage 3
|
||||
|
||||
System.out.println("--- Displaying Basic Vehicle Info ---");
|
||||
for (Vehicle v : vehicles)
|
||||
{
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// Stage 4
|
||||
|
||||
System.out.println("--- Inspecting Vehicles ---");
|
||||
for (Vehicle v : vehicles)
|
||||
{
|
||||
VehicleInspector.inspect(v);
|
||||
System.out.println(); // Blank line for readability
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
|
||||
// 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 ===");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,24 +18,32 @@ 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 showDetails()
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
System.out.println("Seats: " + seats);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 boolean needsService(Vehicle v)
|
||||
{
|
||||
return v.getMileage() > 10000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,49 @@ 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
|
||||
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
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%");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,47 @@ 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
|
||||
@Override
|
||||
public void refuel(double amount)
|
||||
{
|
||||
if (amount < 0)
|
||||
System.out.println("Error: Cannot refuel with a negative amount.");
|
||||
else
|
||||
{
|
||||
this.fuelLevel += amount;
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
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%");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,22 @@ 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("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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,15 +20,27 @@ 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 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(
|
||||
year + " " + brand + " " + model +
|
||||
" | Mileage: " + mileage +
|
||||
|
||||
@@ -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;
|
||||
|
||||
/*
|
||||
@@ -10,13 +13,16 @@ import org.model.Vehicle;
|
||||
|
||||
public class VehicleInspector {
|
||||
|
||||
public static void inspect(Vehicle v) {
|
||||
|
||||
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"
|
||||
switch (v)
|
||||
{
|
||||
case ElectricCar electricCar -> System.out.println("Check battery system");
|
||||
case SportsCar sportsCar -> System.out.println("Check brakes and performance");
|
||||
case GasCar gasCar -> System.out.println("Check engine and oil");
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user