Implement WS-03
This commit is contained in:
Generated
+1
-1
@@ -8,7 +8,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
+36
-11
@@ -1,10 +1,17 @@
|
||||
package org;
|
||||
|
||||
import org.behavior.Serviceable;
|
||||
import org.model.Vehicle;
|
||||
import org.util.VehicleInspector;
|
||||
import org.model.GasCar;
|
||||
import org.model.ElectricCar;
|
||||
import org.model.SportsCar;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// ============================================================
|
||||
// TODO 1:
|
||||
// TODO 1: .
|
||||
// Create one instance of each vehicle type:
|
||||
// - ElectricCar
|
||||
// - GasCar
|
||||
@@ -16,13 +23,17 @@ 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);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 2:
|
||||
// TODO 2: .
|
||||
// Create an array of Vehicle:
|
||||
// Vehicle[] vehicles = { ... };
|
||||
//
|
||||
@@ -31,13 +42,13 @@ public class Main {
|
||||
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
|
||||
Vehicle[] vehicles = {tesla, ferrari, toyota};
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 3:
|
||||
// TODO 3: .
|
||||
// Loop through the Vehicle[] array and:
|
||||
// • Call v.basicInfo();
|
||||
// • Print a blank line after each.
|
||||
@@ -46,13 +57,16 @@ public class Main {
|
||||
// Demonstrate that all subclasses share the same Vehicle
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
|
||||
for (Vehicle v : vehicles) {
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 4:
|
||||
// TODO 4: .
|
||||
// Use VehicleInspector to inspect each vehicle:
|
||||
//
|
||||
// VehicleInspector.inspect(v);
|
||||
@@ -61,13 +75,15 @@ public class Main {
|
||||
// • Demonstrate the use of `instanceof`
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
for (Vehicle v : vehicles) {
|
||||
VehicleInspector.inspect(v);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 5:
|
||||
// TODO 5: .
|
||||
// Create an array of Serviceable:
|
||||
// Serviceable[] serviceables = { ... };
|
||||
//
|
||||
@@ -79,6 +95,7 @@ public class Main {
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// ============================================================
|
||||
Serviceable[] serviceables = {tesla, ferrari, toyota};
|
||||
|
||||
|
||||
|
||||
@@ -105,19 +122,27 @@ 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("Brand: " + v.getBrand() + " Model: " + v.getModel());
|
||||
s.performService();
|
||||
}
|
||||
else System.out.println("The car does not need service");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 7 (Optional):
|
||||
// TODO 7 (Optional): .
|
||||
// After everything is done, print a final message:
|
||||
//
|
||||
// System.out.println("=== Workshop Completed ===");
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -18,24 +18,46 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
public Car(String brand, String model, int year,
|
||||
double mileage, int seats) {
|
||||
|
||||
// TODO: Call super constructor
|
||||
// TODO: Call super constructor .
|
||||
super(brand, model, year, mileage);
|
||||
this.seats = seats;
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return "Unknown"
|
||||
// TODO: Override getEnergyType() → return "Unknown" .
|
||||
|
||||
// TODO: Override showDetails()
|
||||
@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() + " Model: " + getModel() + " Year: " + getYear() + " Mileage: " + getMileage() + "Number of seats: " + seats);
|
||||
}
|
||||
|
||||
/*
|
||||
* Implement Serviceable methods:
|
||||
* Implement Serviceable methods: .
|
||||
*
|
||||
* needsService(Vehicle v):
|
||||
* needsService(Vehicle v): .
|
||||
* - Check the mileage of the current car
|
||||
* - If mileage > 10000 return true
|
||||
* - Otherwise return false
|
||||
*
|
||||
* performService():
|
||||
* performService(): .
|
||||
* - Print: "General car service completed"
|
||||
*/
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if(v.getMileage() > 10000) return true;
|
||||
else return false;
|
||||
}
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,38 @@ public class ElectricCar extends Car implements Chargeable {
|
||||
this.chargeLevel = chargeLevel;
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return "Electric"
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// 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("Capacity: " + batteryCapacity + "Charge level: " + chargeLevel);
|
||||
}
|
||||
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
public void charge(double amount) {
|
||||
if (amount < 0) System.out.println("invalid amount");
|
||||
else chargeLevel += amount;
|
||||
|
||||
if (chargeLevel > 100) chargeLevel = 100;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if(v.getMileage() > 8000) return true;
|
||||
else return false;
|
||||
}
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Battery system checked");
|
||||
chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,40 @@ public class GasCar extends Car implements Refuelable {
|
||||
this.fuelType = fuelType;
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)" .
|
||||
|
||||
// TODO: Implement refuel(double amount)
|
||||
@Override
|
||||
public String getEnergyType() {
|
||||
return (fuelType + " (Gas)");
|
||||
}
|
||||
|
||||
// TODO: Implement refuel(double amount) .
|
||||
// Validate amount
|
||||
// Cap at 100
|
||||
public void refuel(double amount) {
|
||||
if (amount < 0) System.out.println("invalid amount");
|
||||
else fuelLevel += amount;
|
||||
|
||||
// TODO: Override showDetails()
|
||||
if (fuelLevel > 100) fuelLevel = 100;
|
||||
}
|
||||
|
||||
// TODO: Override showDetails() .
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
|
||||
@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;
|
||||
else return false;
|
||||
}
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Engine oil changed");
|
||||
fuelLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.model;
|
||||
|
||||
import org.behavior.Chargeable;
|
||||
import org.behavior.Refuelable;
|
||||
|
||||
public class HybridCar extends Car implements Chargeable, Refuelable {
|
||||
private double fuelLevel;
|
||||
private double chargeLevel;
|
||||
public HybridCar(String brand, String model, int year,
|
||||
double mileage, int seats,
|
||||
double fuelLevel, double chargeLevel) {
|
||||
super(brand, model, year, mileage, seats);
|
||||
this.chargeLevel = chargeLevel;
|
||||
this.fuelLevel = fuelLevel;
|
||||
}
|
||||
|
||||
public void refuel(double amount) {
|
||||
if (amount < 0) System.out.println("invalid amount");
|
||||
else fuelLevel += amount;
|
||||
|
||||
if (fuelLevel > 100) fuelLevel = 100;
|
||||
}
|
||||
public void charge(double amount) {
|
||||
if (amount < 0) System.out.println("invalid amount");
|
||||
else chargeLevel += amount;
|
||||
|
||||
if (chargeLevel > 100) chargeLevel = 100;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println("Fuel level: " + fuelLevel + "Charge level: " + chargeLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEnergyType() {
|
||||
return "Hybrid";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if(v.getMileage() > 10000) return true;
|
||||
else return false;
|
||||
}
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Engine oil changed and battery system checked");
|
||||
fuelLevel = 100;
|
||||
chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,23 @@ public class SportsCar extends GasCar {
|
||||
this.zeroToHundred = zeroToHundred;
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// TODO: Override showDetails() .
|
||||
// Call super.showDetails()
|
||||
// Print acceleration time
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println("Acceleration time: " + zeroToHundred);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if(v.getMileage() > 5000) return true;
|
||||
else return false;
|
||||
}
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("High-performance brake system checked");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,14 +20,33 @@ public abstract class Vehicle {
|
||||
this.mileage = mileage;
|
||||
}
|
||||
|
||||
// TODO: Create getters for all fields
|
||||
// TODO: Create getters for all fields .
|
||||
|
||||
// TODO: Implement addMileage(double km)
|
||||
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
|
||||
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
|
||||
public void addMileage(double km) {
|
||||
if (km < 0) System.out.println("invalid number");
|
||||
else mileage += km;
|
||||
}
|
||||
// TODO: Declare abstract method getEnergyType() .
|
||||
public abstract String getEnergyType();
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
year + " " + brand + " " + model +
|
||||
|
||||
@@ -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;
|
||||
|
||||
/*
|
||||
@@ -14,9 +17,13 @@ public class VehicleInspector {
|
||||
|
||||
v.basicInfo();
|
||||
|
||||
// TODO:
|
||||
// TODO: .
|
||||
// If ElectricCar → print "Check battery system"
|
||||
// If SportsCar → print "Check brakes and performance"
|
||||
// If GasCar → print "Check engine and oil"
|
||||
if (v instanceof GasCar) System.out.println("Check engine and oil");
|
||||
else if (v instanceof ElectricCar) System.out.println("Check battery system");
|
||||
else if (v instanceof SportsCar) System.out.println("Check brakes and performance");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user