Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cb8f9a8d8 | ||
|
|
4e9b7eb7ea | ||
|
|
7eefa807a3 | ||
|
|
22fc28130e |
@@ -41,9 +41,9 @@ The project consists of the following classes and interfaces:
|
|||||||
| GasCar | | ElectricCar |
|
| GasCar | | ElectricCar |
|
||||||
| (Class) | | (Class) |
|
| (Class) | | (Class) |
|
||||||
+-------------------+ +-------------------+
|
+-------------------+ +-------------------+
|
||||||
^ ^ ^
|
^ | |
|
||||||
| extends | implements | implements
|
| extends | implements | implements
|
||||||
| | |
|
| ⌄ ⌄
|
||||||
+-------------------+ +-------------------+ +-------------------+
|
+-------------------+ +-------------------+ +-------------------+
|
||||||
| SportsCar | | Refuelable | | Chargeable |
|
| SportsCar | | Refuelable | | Chargeable |
|
||||||
| (Class) | | (Interface) | | (Interface) |
|
| (Class) | | (Interface) | | (Interface) |
|
||||||
@@ -118,7 +118,14 @@ Add a `HybridCar` class
|
|||||||
- Create a `HybridCar` class that extends `Car`.
|
- Create a `HybridCar` class that extends `Car`.
|
||||||
- It will need variables for both battery level and fuel level
|
- It will need variables for both battery level and fuel level
|
||||||
- Implement `Refuelable` and `Chargeable` together
|
- Implement `Refuelable` and `Chargeable` together
|
||||||
- (Optional) Override methods to manage both power sources (driving drains battery first, then gas)
|
- 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
|
||||||
|
```
|
||||||
|
|
||||||
## Workflow
|
## Workflow
|
||||||
1. Fork the repository to your own Git account
|
1. Fork the repository to your own Git account
|
||||||
@@ -133,6 +140,6 @@ git checkout -b develop
|
|||||||
```
|
```
|
||||||
git push origin develop
|
git push origin develop
|
||||||
```
|
```
|
||||||
7. Create a Pull Request from `develop` to `main`
|
7. Create a Pull Request from `develop` to `main` or `master`
|
||||||
|
|
||||||
# GOOD LUCK! :)
|
# GOOD LUCK! :)
|
||||||
|
|||||||
+115
-67
@@ -1,78 +1,126 @@
|
|||||||
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) {
|
||||||
{
|
|
||||||
// Stage 1
|
|
||||||
|
|
||||||
ElectricCar tesla = new ElectricCar("Tesla", "Model 3", 2023, 5000, 5, 75, 90.5);
|
// ============================================================
|
||||||
GasCar toyota = new GasCar("Toyota", "Camry", 2022, 15000, 5, 70, "Gasoline");
|
// TODO 1:
|
||||||
SportsCar porsche = new SportsCar("Porsche", "911", 2023, 7000, 2, 50, "Premium Gasoline", 3.2);
|
// Create one instance of each vehicle type:
|
||||||
|
// - ElectricCar
|
||||||
System.out.println("--- Vehicle Instances Created ---");
|
// - GasCar
|
||||||
System.out.println();
|
// - SportsCar
|
||||||
|
//
|
||||||
// Stage 2
|
// Requirements:
|
||||||
|
// • Use the constructors you created in each class.
|
||||||
Vehicle[] vehicles = {tesla, toyota, porsche};
|
// • Provide realistic values (brand, model, year, mileage, etc.).
|
||||||
System.out.println("--- Vehicle Array Created ---");
|
// • Store them in variables such as:
|
||||||
System.out.println();
|
// ElectricCar tesla = new ElectricCar(...);
|
||||||
|
// ============================================================
|
||||||
// Stage 3
|
|
||||||
|
|
||||||
System.out.println("--- Displaying Basic Vehicle Info ---");
|
|
||||||
for (Vehicle v : vehicles)
|
|
||||||
{
|
|
||||||
v.basicInfo();
|
// ============================================================
|
||||||
System.out.println();
|
// TODO 2:
|
||||||
}
|
// Create an array of Vehicle:
|
||||||
System.out.println();
|
// Vehicle[] vehicles = { ... };
|
||||||
|
//
|
||||||
// Stage 4
|
// This array MUST contain the objects created above.
|
||||||
|
// This demonstrates **polymorphism** because all different
|
||||||
System.out.println("--- Inspecting Vehicles ---");
|
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
||||||
for (Vehicle v : vehicles)
|
// as their parent type (Vehicle).
|
||||||
{
|
// ============================================================
|
||||||
VehicleInspector.inspect(v);
|
|
||||||
System.out.println(); // Blank line for readability
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// 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 ===");
|
||||||
|
//
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
// 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,32 +18,24 @@ 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) {
|
||||||
|
|
||||||
super(brand, model, year, mileage);
|
// TODO: Call super constructor
|
||||||
this.seats = seats;
|
this.seats = seats;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// TODO: Override getEnergyType() → return "Unknown"
|
||||||
public String getEnergyType()
|
|
||||||
{
|
|
||||||
return "Unknown";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Override showDetails()
|
// TODO: Override showDetails()
|
||||||
@Override
|
// Print brand, model, year, mileage, and number of seats
|
||||||
public void showDetails()
|
|
||||||
{
|
|
||||||
System.out.println("Seats: " + seats);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
/*
|
||||||
public boolean needsService(Vehicle v)
|
* Implement Serviceable methods:
|
||||||
{
|
*
|
||||||
return v.getMileage() > 10000;
|
* needsService(Vehicle v):
|
||||||
}
|
* - Check the mileage of the current car
|
||||||
|
* - If mileage > 10000 return true
|
||||||
@Override
|
* - Otherwise return false
|
||||||
public void performService()
|
*
|
||||||
{
|
* performService():
|
||||||
System.out.println("General car service completed");
|
* - Print: "General car service completed"
|
||||||
}
|
*/
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,49 +27,13 @@ public class ElectricCar extends Car implements Chargeable {
|
|||||||
this.chargeLevel = chargeLevel;
|
this.chargeLevel = chargeLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// TODO: Override getEnergyType() → return "Electric"
|
||||||
public String getEnergyType()
|
|
||||||
{
|
|
||||||
return "Electric";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// TODO: Override showDetails()
|
||||||
|
// Call super.showDetails()
|
||||||
|
// Print battery capacity and charge level
|
||||||
|
|
||||||
@Override
|
// TODO: Implement charge(double amount)
|
||||||
public void showDetails()
|
// Validate negative
|
||||||
{
|
// Cap at 100
|
||||||
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,47 +27,13 @@ public class GasCar extends Car implements Refuelable {
|
|||||||
this.fuelType = fuelType;
|
this.fuelType = fuelType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||||
public String getEnergyType()
|
|
||||||
{
|
|
||||||
return fuelType + " (Gas)";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
// TODO: Implement refuel(double amount)
|
||||||
public void refuel(double amount)
|
// Validate amount
|
||||||
{
|
// Cap at 100
|
||||||
if (amount < 0)
|
|
||||||
System.out.println("Error: Cannot refuel with a negative amount.");
|
|
||||||
else
|
|
||||||
{
|
|
||||||
this.fuelLevel += amount;
|
|
||||||
|
|
||||||
if (this.fuelLevel > 100)
|
// TODO: Override showDetails()
|
||||||
this.fuelLevel = 100;
|
// Call super.showDetails()
|
||||||
|
// Print fuel level and fuel type
|
||||||
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,22 +24,7 @@ public class SportsCar extends GasCar {
|
|||||||
this.zeroToHundred = zeroToHundred;
|
this.zeroToHundred = zeroToHundred;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// TODO: Override showDetails()
|
||||||
public void showDetails()
|
// Call super.showDetails()
|
||||||
{
|
// Print acceleration time
|
||||||
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,27 +20,15 @@ public abstract class Vehicle {
|
|||||||
this.mileage = mileage;
|
this.mileage = mileage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBrand() {return brand;}
|
// TODO: Create getters for all fields
|
||||||
|
|
||||||
public String getModel() {return model;}
|
// TODO: Implement addMileage(double km)
|
||||||
|
// If km is negative → print error
|
||||||
|
// Otherwise increase mileage
|
||||||
|
|
||||||
public int getYear() {return year;}
|
// TODO: Declare abstract method getEnergyType()
|
||||||
|
|
||||||
public double getMileage() {return mileage;}
|
public void basicInfo() {
|
||||||
|
|
||||||
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(
|
System.out.println(
|
||||||
year + " " + brand + " " + model +
|
year + " " + brand + " " + model +
|
||||||
" | Mileage: " + mileage +
|
" | Mileage: " + mileage +
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
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;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -13,16 +10,13 @@ import org.model.Vehicle;
|
|||||||
|
|
||||||
public class VehicleInspector {
|
public class VehicleInspector {
|
||||||
|
|
||||||
public static void inspect(Vehicle v)
|
public static void inspect(Vehicle v) {
|
||||||
{
|
|
||||||
v.basicInfo();
|
v.basicInfo();
|
||||||
|
|
||||||
switch (v)
|
// TODO:
|
||||||
{
|
// If ElectricCar → print "Check battery system"
|
||||||
case ElectricCar electricCar -> System.out.println("Check battery system");
|
// If SportsCar → print "Check brakes and performance"
|
||||||
case SportsCar sportsCar -> System.out.println("Check brakes and performance");
|
// If GasCar → print "Check engine and oil"
|
||||||
case GasCar gasCar -> System.out.println("Check engine and oil");
|
|
||||||
default -> {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user