Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f99877eb6d | ||
|
|
35a0a04f7f |
@@ -1,5 +1,12 @@
|
||||
package org;
|
||||
|
||||
import org.behavior.Serviceable;
|
||||
import org.model.*;
|
||||
import org.util.VehicleInspector;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -16,8 +23,23 @@ public class Main {
|
||||
// • Store them in variables such as:
|
||||
// ElectricCar tesla = new ElectricCar(...);
|
||||
// ============================================================
|
||||
ElectricCar tesla = new ElectricCar(
|
||||
"Tesla", "Model S", 2022, 1000,
|
||||
100, 550, 50 // batteryCapacity, range
|
||||
);
|
||||
|
||||
GasCar toyota = new GasCar(
|
||||
"Toyota", "Corolla", 2018, 85000,
|
||||
5, 50, "Petrol" // seats, fuelCapacity, fuelType
|
||||
);
|
||||
|
||||
SportsCar ferrari = new SportsCar(
|
||||
"Ferrari", "F8 Tributo", 2021, 12000,
|
||||
2, 3.9, "oil", 710 // seats, engineSize, horsepower
|
||||
);
|
||||
|
||||
HybridCar tara = new HybridCar("Tara", "Model s", 2020,
|
||||
1000, 4, 45 ,80);
|
||||
|
||||
|
||||
|
||||
@@ -31,7 +53,7 @@ public class Main {
|
||||
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
|
||||
Vehicle[] vehicles = {tesla, toyota, ferrari, tara};
|
||||
|
||||
|
||||
|
||||
@@ -47,6 +69,12 @@ public class Main {
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
|
||||
for(Vehicle v: vehicles)
|
||||
{
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -61,7 +89,11 @@ public class Main {
|
||||
// • Demonstrate the use of `instanceof`
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
for(Vehicle v: vehicles)
|
||||
{
|
||||
VehicleInspector.inspect(v);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -79,7 +111,7 @@ public class Main {
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
Serviceable[] serviceables = {tesla, toyota, ferrari, tara};
|
||||
|
||||
|
||||
|
||||
@@ -105,6 +137,21 @@ public class Main {
|
||||
//
|
||||
// Add blank lines between each item for readability.
|
||||
// ============================================================
|
||||
for (Serviceable s: serviceables)
|
||||
{
|
||||
Vehicle v = (Vehicle) s;
|
||||
if (s.needsService(v))
|
||||
{
|
||||
System.out.println(v.getBrand() + " " + v.getModel());
|
||||
s.performService();
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("The " + v.getBrand() + " " + v.getModel() + " "
|
||||
+ "does not need service");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -118,7 +165,7 @@ public class Main {
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -19,14 +19,27 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
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
|
||||
public void showDetails()
|
||||
{
|
||||
System.out.println("Brand: " + getBrand());
|
||||
System.out.println("Model: " + getModel());
|
||||
System.out.println("Year: " + getYear());
|
||||
System.out.println("Mileage: " + getMileage());
|
||||
System.out.println("Number of seats: " + seats);
|
||||
}
|
||||
|
||||
/*
|
||||
* Implement Serviceable methods:
|
||||
*
|
||||
@@ -38,4 +51,16 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
* 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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,62 @@ public class ElectricCar extends Car implements Chargeable {
|
||||
|
||||
// TODO: Override getEnergyType() → return "Electric"
|
||||
|
||||
@Override
|
||||
public String getEnergyType()
|
||||
{
|
||||
return "Electric";
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print battery capacity and charge level
|
||||
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
super.showDetails();
|
||||
System.out.println("Battery capacity: " + batteryCapacity);
|
||||
System.out.println("Charge level: " + chargeLevel);
|
||||
}
|
||||
|
||||
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
@Override
|
||||
public void charge(double amount)
|
||||
{
|
||||
if (chargeLevel == 100)
|
||||
{
|
||||
System.out.println("The car is full!");
|
||||
}
|
||||
else if(amount <= 0)
|
||||
{
|
||||
System.out.println("charge amount must be positive number!");
|
||||
return;
|
||||
}
|
||||
else if(amount > (100 - chargeLevel))
|
||||
{
|
||||
System.out.printf("The amount can be from %d to %f", 0, 100 - chargeLevel);
|
||||
return;
|
||||
}
|
||||
chargeLevel += amount;
|
||||
|
||||
}
|
||||
|
||||
//Serviceable methods
|
||||
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v)
|
||||
{
|
||||
return v.getMileage() > 8000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("Battery system checked");
|
||||
chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,63 @@ public class GasCar extends Car implements Refuelable {
|
||||
|
||||
// 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 (fuelLevel == 100)
|
||||
{
|
||||
System.out.println("The car is full!");
|
||||
return;
|
||||
}
|
||||
else if(amount <= 0 )
|
||||
{
|
||||
System.out.println("error: fuel must have positive amount!");
|
||||
return;
|
||||
}
|
||||
else if (amount > (100 - fuelLevel))
|
||||
{
|
||||
System.out.printf("The amount can be from %d to %f", 0, 100 - fuelLevel);
|
||||
return;
|
||||
}
|
||||
|
||||
fuelLevel += amount;
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
super.showDetails();
|
||||
System.out.println("Fuel type: " + fuelType);
|
||||
System.out.println("Fuel level: " + fuelLevel);
|
||||
}
|
||||
|
||||
|
||||
//Serviceable methods
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v)
|
||||
{
|
||||
return v.getMileage() > 12000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("Engine oil changed");
|
||||
fuelLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package org.model;
|
||||
|
||||
import org.behavior.Chargeable;
|
||||
import org.behavior.Refuelable;
|
||||
|
||||
public class HybridCar extends Car implements Refuelable, Chargeable
|
||||
{
|
||||
private double batteryLevel;
|
||||
private double fuelLevel;
|
||||
|
||||
|
||||
public HybridCar(String brand, String model, int year,
|
||||
double mileage, int seats,
|
||||
double batteryLevel, double fuelLevel) {
|
||||
super(brand, model, year, mileage, seats);
|
||||
this.batteryLevel = batteryLevel;
|
||||
this.fuelLevel = fuelLevel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEnergyType()
|
||||
{
|
||||
return "gas and electric";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
super.showDetails();
|
||||
System.out.println("Battery level: " + batteryLevel);
|
||||
System.out.println("Fuel level: " + fuelLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refuel(double amount)
|
||||
{
|
||||
if (fuelLevel == 100)
|
||||
{
|
||||
System.out.println("The car is full!");
|
||||
return;
|
||||
}
|
||||
else if(amount <= 0 )
|
||||
{
|
||||
System.out.println("error: fuel must have positive amount!");
|
||||
return;
|
||||
}
|
||||
else if (amount > (100 - fuelLevel))
|
||||
{
|
||||
System.out.printf("The amount can be from %d to %f", 0, 100 - fuelLevel);
|
||||
return;
|
||||
}
|
||||
|
||||
fuelLevel += amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void charge(double amount)
|
||||
{
|
||||
if (batteryLevel == 100)
|
||||
{
|
||||
System.out.println("The car is full!");
|
||||
}
|
||||
else if(amount <= 0)
|
||||
{
|
||||
System.out.println("charge amount must be positive number!");
|
||||
return;
|
||||
}
|
||||
else if(amount > (100 - batteryLevel))
|
||||
{
|
||||
System.out.printf("The amount can be from %d to %f", 0, 100 - batteryLevel);
|
||||
return;
|
||||
}
|
||||
batteryLevel += amount;
|
||||
}
|
||||
|
||||
//Serviceable methods
|
||||
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v)
|
||||
{
|
||||
return v.getMileage() > 10000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("Engine oil changed and battery system checked");
|
||||
batteryLevel = 100;
|
||||
fuelLevel = 100;
|
||||
}
|
||||
}
|
||||
@@ -27,4 +27,26 @@ public class SportsCar extends GasCar {
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print acceleration time
|
||||
|
||||
@Override
|
||||
public void showDetails()
|
||||
{
|
||||
super.showDetails();
|
||||
System.out.println("Acceleration time: " + zeroToHundred);
|
||||
}
|
||||
|
||||
//Serviceable methods
|
||||
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v)
|
||||
{
|
||||
return v.getMileage() > 5000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService()
|
||||
{
|
||||
System.out.println("High-performance brake system checked");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,12 +21,24 @@ public abstract class Vehicle {
|
||||
}
|
||||
|
||||
// TODO: Create getters for all fields
|
||||
|
||||
public String getBrand() {return brand;};
|
||||
public String getModel() {return model;};
|
||||
public int getYear() {return year;};
|
||||
public double getMileage() {return mileage;}
|
||||
// TODO: Implement addMileage(double km)
|
||||
// If km is negative → print error
|
||||
// Otherwise increase mileage
|
||||
|
||||
public void addMileage(double km)
|
||||
{
|
||||
if(km < 0)
|
||||
{
|
||||
System.out.println("error: km can not be negative!");
|
||||
return;
|
||||
}
|
||||
this.mileage += km;
|
||||
}
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
public abstract String getEnergyType();
|
||||
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.util;
|
||||
|
||||
import org.*;
|
||||
import org.model.Vehicle;
|
||||
import org.model.*;
|
||||
|
||||
/*
|
||||
* Utility class to inspect vehicles.
|
||||
@@ -18,5 +18,21 @@ public class VehicleInspector {
|
||||
// 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");
|
||||
}
|
||||
else if(v instanceof HybridCar)
|
||||
{
|
||||
System.out.println("Check Engine, oil and battery system");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user