Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c21faaff29 | ||
|
|
8f252041e1 | ||
|
|
3805024923 | ||
|
|
faffb3c801 | ||
|
|
7769382421 | ||
|
|
ae505f04be | ||
|
|
a419bf1858 | ||
|
|
989c2fe43b | ||
|
|
7799febfd6 | ||
|
|
89b43d8a0d |
+50
-106
@@ -1,126 +1,70 @@
|
|||||||
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) {
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// TODO 1:
|
ElectricCar tesla = new ElectricCar(
|
||||||
// Create one instance of each vehicle type:
|
"Tesla", "Model S", 2022, 1000,
|
||||||
// - ElectricCar
|
100, 550, 50 // batteryCapacity, range
|
||||||
// - GasCar
|
);
|
||||||
// - SportsCar
|
|
||||||
//
|
GasCar toyota = new GasCar(
|
||||||
// Requirements:
|
"Toyota", "Corolla", 2018, 85000,
|
||||||
// • Use the constructors you created in each class.
|
5, 50, "Petrol" // seats, fuelCapacity, fuelType
|
||||||
// • Provide realistic values (brand, model, year, mileage, etc.).
|
);
|
||||||
// • Store them in variables such as:
|
|
||||||
// ElectricCar tesla = new ElectricCar(...);
|
SportsCar ferrari = new SportsCar(
|
||||||
// ============================================================
|
"Ferrari", "F8 Tributo", 2021, 12000,
|
||||||
|
2, 3.9, "oil", 710 // seats, engineSize, horsepower
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
Vehicle[] vehicles = {tesla, toyota, ferrari};
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("\n--------------BASIC INFO--------------\n");
|
||||||
|
for(int i=0 ; i<3 ; i++) {
|
||||||
|
vehicles[i].basicInfo();
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================
|
System.out.println("--------------INSPECT--------------\n");
|
||||||
// TODO 2:
|
for(int i=0 ; i<3 ; i++) {
|
||||||
// Create an array of Vehicle:
|
VehicleInspector.inspect(vehicles[i]);
|
||||||
// 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).
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
Serviceable[] serviceables = {tesla, toyota, ferrari};
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("--------------SERVISES--------------\n");
|
||||||
|
for(int i=0 ; i<3 ; i++) {
|
||||||
|
Vehicle temp = (Vehicle) serviceables[i];
|
||||||
|
|
||||||
// ============================================================
|
if(serviceables[i].needsService(temp)) {
|
||||||
// TODO 3:
|
|
||||||
// Loop through the Vehicle[] array and:
|
System.out.println(temp.getBrand() + " " + temp.getModel());
|
||||||
// • Call v.basicInfo();
|
serviceables[i].performService();
|
||||||
// • Print a blank line after each.
|
|
||||||
//
|
}
|
||||||
// Purpose:
|
else {
|
||||||
// Demonstrate that all subclasses share the same Vehicle
|
System.out.println(temp.getBrand() + " " + temp.getModel() +
|
||||||
// behavior and can call inherited methods.
|
" does not need service");
|
||||||
// ============================================================
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// 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 ===");
|
|
||||||
//
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("========= Workshop Completed =========");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,24 +18,34 @@ 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) {
|
||||||
|
|
||||||
// TODO: Call super constructor
|
super(brand, model, year, mileage);
|
||||||
this.seats = seats;
|
this.seats = seats;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Unknown"
|
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Print brand, model, year, mileage, and number of seats
|
public String getEnergyType() {
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Implement Serviceable methods:
|
@Override
|
||||||
*
|
public void showDetails() {
|
||||||
* needsService(Vehicle v):
|
super.basicInfo();
|
||||||
* - Check the mileage of the current car
|
System.out.print("seats: " + this.seats);
|
||||||
* - If mileage > 10000 return true
|
}
|
||||||
* - Otherwise return false
|
|
||||||
*
|
|
||||||
* performService():
|
@Override
|
||||||
* - Print: "General car service completed"
|
public boolean needsService(Vehicle v) {
|
||||||
*/
|
if(v.getMileage()>10000) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("General car service completed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,13 +27,44 @@ public class ElectricCar extends Car implements Chargeable {
|
|||||||
this.chargeLevel = chargeLevel;
|
this.chargeLevel = chargeLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Electric"
|
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
@Override
|
||||||
// Call super.showDetails()
|
public String getEnergyType() {
|
||||||
// Print battery capacity and charge level
|
return "Electric";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement charge(double amount)
|
|
||||||
// Validate negative
|
@Override
|
||||||
// Cap at 100
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println(" | battery capacity: " + batteryCapacity +
|
||||||
|
" | charge level: " + chargeLevel
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void charge(double amount) {
|
||||||
|
if(amount<0)
|
||||||
|
System.out.println("amont cannot be negative");
|
||||||
|
else
|
||||||
|
chargeLevel += amount;
|
||||||
|
|
||||||
|
if(chargeLevel>100)
|
||||||
|
chargeLevel = 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if(v.getMileage()>8000)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Battery system checked");
|
||||||
|
chargeLevel = 100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,13 +27,47 @@ public class GasCar extends Car implements Refuelable {
|
|||||||
this.fuelType = fuelType;
|
this.fuelType = fuelType;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
|
||||||
|
|
||||||
// TODO: Implement refuel(double amount)
|
@Override
|
||||||
// Validate amount
|
public String getEnergyType() {
|
||||||
// Cap at 100
|
return (fuelType + " (Gas)");
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
|
||||||
// Call super.showDetails()
|
@Override
|
||||||
// Print fuel level and fuel type
|
public void refuel(double amount) {
|
||||||
|
if(amount<0) {
|
||||||
|
System.out.println("amount cannot be negative");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fuelLevel += amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(fuelLevel>100) {
|
||||||
|
fuelLevel = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println(" | fuel level: " + fuelLevel +
|
||||||
|
" | fuel type: " + fuelType
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if(v.getMileage()>12000)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed");
|
||||||
|
this.fuelLevel = 100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package org.model;
|
||||||
|
|
||||||
|
import org.behavior.Chargeable;
|
||||||
|
import org.behavior.Refuelable;
|
||||||
|
|
||||||
|
public class HybirdCar extends Car implements Refuelable, Chargeable {
|
||||||
|
|
||||||
|
private double batterLevel;
|
||||||
|
private double fuelLevel;
|
||||||
|
|
||||||
|
public HybirdCar(String brand, String model, int year,
|
||||||
|
double mileage,int seats, double batterLevel, double fuelLevel) {
|
||||||
|
|
||||||
|
super(brand, model, year, mileage, seats);
|
||||||
|
this.batterLevel = batterLevel;
|
||||||
|
this.fuelLevel = fuelLevel;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void refuel(double amount) {
|
||||||
|
if(amount<0) {
|
||||||
|
System.out.println("amount cannot be negative");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fuelLevel += amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(fuelLevel>100) {
|
||||||
|
fuelLevel = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void charge(double amount) {
|
||||||
|
if(amount<0)
|
||||||
|
System.out.println("amount cannot be negative");
|
||||||
|
else
|
||||||
|
batterLevel += amount;
|
||||||
|
|
||||||
|
if(batterLevel>100)
|
||||||
|
batterLevel = 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if(v.getMileage()>10000)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed and battery system checked");
|
||||||
|
batterLevel = 100;
|
||||||
|
fuelLevel = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,22 @@ public class SportsCar extends GasCar {
|
|||||||
this.zeroToHundred = zeroToHundred;
|
this.zeroToHundred = zeroToHundred;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
|
||||||
// Call super.showDetails()
|
@Override
|
||||||
// Print acceleration time
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println("accelaration time: " + zeroToHundred);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if(v.getMileage()>5000)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("High-performance brake system checked");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,13 +20,38 @@ public abstract class Vehicle {
|
|||||||
this.mileage = mileage;
|
this.mileage = mileage;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create getters for all fields
|
|
||||||
|
|
||||||
// TODO: Implement addMileage(double km)
|
public String getBrand() {
|
||||||
|
return this.brand;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModel() {
|
||||||
|
return this.model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getYear() {
|
||||||
|
return this.year;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMileage() {
|
||||||
|
return this.mileage;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// If km is negative → print error
|
// If km is negative → print error
|
||||||
// Otherwise increase mileage
|
// Otherwise increase mileage
|
||||||
|
public void addMileage(double km) {
|
||||||
|
if(km<0) {
|
||||||
|
System.out.print("km cannot be negative");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.mileage += km;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public abstract String getEnergyType();
|
||||||
|
|
||||||
// TODO: Declare abstract method 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.*;
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.GasCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
import org.model.Vehicle;
|
import org.model.Vehicle;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -14,9 +17,13 @@ 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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user