Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d7e9de943 | ||
|
|
aca8cb51bf | ||
|
|
570e1cd97f | ||
|
|
fe69260c63 | ||
|
|
fb21770776 | ||
|
|
1251fd6b76 | ||
|
|
f96194275e | ||
|
|
c3abc9ed05 | ||
|
|
4eb20505a2 | ||
|
|
29c31f5845 |
+47
-110
@@ -1,126 +1,63 @@
|
|||||||
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) {
|
||||||
|
|
||||||
// ============================================================
|
//Todo 1
|
||||||
// TODO 1:
|
ElectricCar tesla = new ElectricCar(
|
||||||
// Create one instance of each vehicle type:
|
"Tesla", "Model S", 2022, 1000,
|
||||||
// - ElectricCar
|
100, 550, 50
|
||||||
// - 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(...);
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
GasCar toyota = new GasCar(
|
||||||
|
"Toyota", "Corolla", 2018, 85000,
|
||||||
|
5, 50, "Petrol"
|
||||||
|
);
|
||||||
|
|
||||||
|
SportsCar ferrari = new SportsCar(
|
||||||
|
"Ferrari", "F8 Tributo", 2021, 12000,
|
||||||
|
2, 3.9, "oil", 710
|
||||||
|
);
|
||||||
|
|
||||||
|
HybridCar fun = new HybridCar(
|
||||||
|
"Aodi", "fun", 2026, 1000,
|
||||||
|
5, 600, 40, 60,
|
||||||
|
"oil"
|
||||||
|
);
|
||||||
|
|
||||||
|
//TODO 2
|
||||||
|
Vehicle[] vehicles = {tesla, toyota, ferrari, fun};
|
||||||
|
|
||||||
// ============================================================
|
//TODO 3
|
||||||
// TODO 2:
|
for(int i = 0; i<4; i++) {
|
||||||
// Create an array of Vehicle:
|
vehicles[i].basicInfo();
|
||||||
// Vehicle[] vehicles = { ... };
|
System.out.println(" ");
|
||||||
//
|
}
|
||||||
// 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 ===");
|
|
||||||
//
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
//TODO 4
|
||||||
|
for(int i = 0; i<4; i++) {
|
||||||
|
VehicleInspector.inspect(vehicles[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO 5
|
||||||
|
Serviceable[] serviceables = {tesla, toyota, ferrari, fun};
|
||||||
|
|
||||||
|
//TODO 6
|
||||||
|
for(int i = 0; i<4; i++) {
|
||||||
|
Vehicle v = (Vehicle) serviceables[i];
|
||||||
|
if (serviceables[i].needsService(v)) {
|
||||||
|
System.out.println(((Vehicle)serviceables[i]).getBrand() + " " + ((Vehicle)serviceables[i]).getModel());
|
||||||
|
serviceables[i].performService();
|
||||||
|
}
|
||||||
|
else {System.out.println("The car doesn't need service!");}
|
||||||
|
System.out.println(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO 7
|
||||||
|
System.out.println("=== Workshop Completed ===");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,25 +17,30 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Unknown"
|
@Override
|
||||||
|
abstract String getEnergyType();
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Print brand, model, year, mileage, and number of seats
|
public void showDetails() {
|
||||||
|
System.out.println(getBrand());
|
||||||
|
System.out.println(getModel());
|
||||||
|
System.out.println(getYear());
|
||||||
|
System.out.println(getMileage());
|
||||||
|
System.out.println(seats);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Implement Serviceable methods:
|
//Implements:
|
||||||
*
|
public boolean needsService(Vehicle v) {
|
||||||
* needsService(Vehicle v):
|
double mileage = v.getMileage();
|
||||||
* - Check the mileage of the current car
|
return (mileage > 10000);
|
||||||
* - If mileage > 10000 return true
|
}
|
||||||
* - Otherwise return false
|
|
||||||
*
|
public void performService() {
|
||||||
* performService():
|
System.out.println("General car service completed");
|
||||||
* - Print: "General car service completed"
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,13 +27,35 @@ 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()
|
//Implement Chargeable
|
||||||
// Call super.showDetails()
|
public void charge(double amount) {
|
||||||
// Print battery capacity and charge level
|
if (amount <= 0) {
|
||||||
|
System.out.println("Invalid amount!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (chargeLevel + amount > 100) {System.out.println("Invalid amount!");}
|
||||||
|
else {System.out.println("Refuelable!");}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement charge(double amount)
|
@Override
|
||||||
// Validate negative
|
public void showDetails() {
|
||||||
// Cap at 100
|
super.showDetails();
|
||||||
|
System.out.println(batteryCapacity);
|
||||||
|
System.out.println(chargeLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
double mileage = v.getMileage();
|
||||||
|
return (mileage > 8000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Battery system checked");
|
||||||
|
chargeLevel = 100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,13 +27,37 @@ 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)
|
//Implement Refuelable
|
||||||
// Validate amount
|
public void refuel(double amount) {
|
||||||
// Cap at 100
|
if (amount <= 0) {
|
||||||
|
System.out.println("Invalid amount!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (fuelLevel + amount > 100) {System.out.println("Invalid amount!");}
|
||||||
|
else {System.out.println("Refuelable!");}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Call super.showDetails()
|
public void showDetails() {
|
||||||
// Print fuel level and fuel type
|
super.showDetails();
|
||||||
|
System.out.println(fuelLevel);
|
||||||
|
System.out.println(fuelType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
double mileage = v.getMileage();
|
||||||
|
return (mileage > 12000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed");
|
||||||
|
fuelLevel = 100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package org.model;
|
||||||
|
|
||||||
|
import org.behavior.Chargeable;
|
||||||
|
import org.behavior.Refuelable;
|
||||||
|
|
||||||
|
public class HybridCar extends Car implements Chargeable, Refuelable {
|
||||||
|
|
||||||
|
private int batteryCapacity;
|
||||||
|
private double chargeLevel;
|
||||||
|
private double fuelLevel;
|
||||||
|
private String fuelType;
|
||||||
|
|
||||||
|
public HybridCar(String brand, String model, int year,
|
||||||
|
double mileage, int seats,
|
||||||
|
int batteryCapacity, double chargeLevel,
|
||||||
|
double fuelLevel, String fuelType)
|
||||||
|
{
|
||||||
|
super(brand, model, year, mileage, seats);
|
||||||
|
this.batteryCapacity = batteryCapacity;
|
||||||
|
this.chargeLevel = chargeLevel;
|
||||||
|
this.fuelLevel = fuelLevel;
|
||||||
|
this.fuelType = fuelType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getEnergyType() {
|
||||||
|
return ("Hybrid (Gas and Electric)");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Implement Refuelable
|
||||||
|
public void refuel(double amount) {
|
||||||
|
if (amount <= 0) {
|
||||||
|
System.out.println("Invalid amount!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (fuelLevel + amount > 100) {System.out.println("Invalid amount!");}
|
||||||
|
else {System.out.println("Refuelable!");}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Implement Chargeable
|
||||||
|
public void charge(double amount) {
|
||||||
|
if (amount <= 0) {
|
||||||
|
System.out.println("Invalid amount!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (chargeLevel + amount > 100) {System.out.println("Invalid amount!");}
|
||||||
|
else {System.out.println("Refuelable!");}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println(fuelLevel);
|
||||||
|
System.out.println(fuelType);
|
||||||
|
System.out.println(batteryCapacity);
|
||||||
|
System.out.println(chargeLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
double mileage = v.getMileage();
|
||||||
|
return (mileage > 10000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed and battery system checked");
|
||||||
|
chargeLevel = 100;
|
||||||
|
fuelLevel = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,20 @@ 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(zeroToHundred);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
double mileage = v.getMileage();
|
||||||
|
return (mileage > 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("High-performance brake system checked");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,13 +20,32 @@ public abstract class Vehicle {
|
|||||||
this.mileage = mileage;
|
this.mileage = mileage;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create getters for all fields
|
public String getBrand() {
|
||||||
|
return brand;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement addMileage(double km)
|
public String getModel() {
|
||||||
// If km is negative → print error
|
return model;
|
||||||
// Otherwise increase mileage
|
}
|
||||||
|
|
||||||
// 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("The mileage can't be negative!");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mileage += km;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract String getEnergyType();
|
||||||
|
|
||||||
public void basicInfo() {
|
public void basicInfo() {
|
||||||
System.out.println(
|
System.out.println(
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package org.util;
|
package org.util;
|
||||||
|
|
||||||
import org.*;
|
import org.model.Car;
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.GasCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
import org.model.Vehicle;
|
import org.model.Vehicle;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -11,12 +14,10 @@ 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();
|
||||||
|
|
||||||
// TODO:
|
if (v instanceof SportsCar) {System.out.println("Check brakes and performance");}
|
||||||
// If ElectricCar → print "Check battery system"
|
else if (v instanceof ElectricCar) {System.out.println("Check battery system");}
|
||||||
// If SportsCar → print "Check brakes and performance"
|
else if (v instanceof GasCar) {System.out.println("Check engine and oil");}
|
||||||
// If GasCar → print "Check engine and oil"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user