Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba6435df81 | ||
|
|
22726ea54d |
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>
|
||||
+41
-103
@@ -1,124 +1,62 @@
|
||||
package org;
|
||||
|
||||
import org.model.Vehicle;
|
||||
import org.model.ElectricCar;
|
||||
import org.model.GasCar;
|
||||
import org.model.SportsCar;
|
||||
import org.util.VehicleInspector;
|
||||
import org.behavior.Serviceable;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// ============================================================
|
||||
// TODO 1:
|
||||
// Create one instance of each vehicle type:
|
||||
// - ElectricCar
|
||||
// - 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(...);
|
||||
// ============================================================
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
Vehicle[] vehicles = {tesla , toyota , ferrari};
|
||||
|
||||
// ============================================================
|
||||
// TODO 2:
|
||||
// Create an array of Vehicle:
|
||||
// Vehicle[] vehicles = { ... };
|
||||
//
|
||||
// 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).
|
||||
// ============================================================
|
||||
for (Vehicle v : vehicles) {
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
for (Vehicle v : vehicles) {
|
||||
VehicleInspector.inspect(v);
|
||||
}
|
||||
|
||||
Serviceable[] serviceables = {tesla, toyota, ferrari};
|
||||
|
||||
for (Serviceable s : serviceables) {
|
||||
|
||||
Vehicle v = (Vehicle) s;
|
||||
|
||||
// ============================================================
|
||||
// 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.
|
||||
// ============================================================
|
||||
v.basicInfo();
|
||||
|
||||
if (s.needsService(v)) {
|
||||
System.out.println(v.getBrand() + " " + v.getModel());
|
||||
System.out.print("Performing service: ");
|
||||
s.performService();
|
||||
} else {
|
||||
System.out.println(v.getBrand() + " " + v.getModel() + " 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 ===");
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -11,31 +11,44 @@ package org.model;
|
||||
|
||||
import org.behavior.Serviceable;
|
||||
|
||||
import java.lang.classfile.Superclass;
|
||||
|
||||
public abstract class Car extends Vehicle implements Serviceable {
|
||||
|
||||
private int seats;
|
||||
|
||||
public Car(String brand, String model, int year,
|
||||
double mileage, int seats) {
|
||||
|
||||
// TODO: Call super constructor
|
||||
public Car(String brand, String model, int year, double mileage, int seats) {
|
||||
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.print(
|
||||
getYear() + " " + getBrand() + " " + getModel() +
|
||||
" | Mileage: " + getMileage() +
|
||||
" | Energy: " + getEnergyType() +
|
||||
" | Seats: " + seats
|
||||
);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public boolean needsService(Vehicle v) {
|
||||
if(getMileage() > 10000) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void performService() {
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
|
||||
/*
|
||||
* Implement Serviceable methods:
|
||||
*
|
||||
* needsService(Vehicle v):
|
||||
* - Check the mileage of the current car
|
||||
* - If mileage > 10000 return true
|
||||
* - Otherwise return false
|
||||
*
|
||||
* performService():
|
||||
* - Print: "General car service completed"
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -26,14 +26,49 @@ public class ElectricCar extends Car implements Chargeable {
|
||||
this.batteryCapacity = batteryCapacity;
|
||||
this.chargeLevel = chargeLevel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEnergyType() {
|
||||
return "Electric";
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return "Electric"
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.print(
|
||||
" | battery Capacity: " + batteryCapacity +
|
||||
" | charge Level: " + chargeLevel
|
||||
);
|
||||
System.out.println();
|
||||
}
|
||||
@Override
|
||||
public void charge(double amount) {
|
||||
if (amount <= 0) {
|
||||
System.out.println("Error");
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print battery capacity and charge level
|
||||
if (this.chargeLevel + amount > 100) {
|
||||
this.chargeLevel = 100;
|
||||
}
|
||||
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
else {
|
||||
this.chargeLevel += amount;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if(getMileage() > 8000) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Battery system checked");
|
||||
this.chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,47 @@ public class GasCar extends Car implements Refuelable {
|
||||
this.fuelType = fuelType;
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||
@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("Error");
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
if (this.fuelLevel + amount > 100) {
|
||||
this.fuelLevel = 100;
|
||||
} else {
|
||||
this.fuelLevel += amount;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.print(
|
||||
" | fuel type: " + fuelType +
|
||||
" | fuel level: " + fuelLevel
|
||||
);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if(getMileage() > 12000) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Engine oil changed");
|
||||
this.fuelLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +23,27 @@ public class SportsCar extends GasCar {
|
||||
|
||||
this.zeroToHundred = zeroToHundred;
|
||||
}
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.print(
|
||||
" | acceleration time: " + this.zeroToHundred
|
||||
);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print acceleration time
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
if(getMileage() > 5000) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("High-performance brake system checked");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,13 +20,36 @@ public abstract class Vehicle {
|
||||
this.mileage = mileage;
|
||||
}
|
||||
|
||||
// TODO: Create getters for all fields
|
||||
public String getBrand() {
|
||||
|
||||
// TODO: Implement addMileage(double km)
|
||||
// If km is negative → print error
|
||||
// Otherwise increase mileage
|
||||
return brand;
|
||||
}
|
||||
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
public String getModel() {
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
|
||||
return year;
|
||||
}
|
||||
|
||||
public double getMileage() {
|
||||
|
||||
return mileage;
|
||||
}
|
||||
|
||||
|
||||
public void addMileage(double km) {
|
||||
if ( km < 0 ) {
|
||||
System.out.println("Error: Mileage cannot be negative!");
|
||||
return;
|
||||
}
|
||||
this.mileage += km;
|
||||
}
|
||||
|
||||
public abstract String getEnergyType();
|
||||
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
|
||||
@@ -2,7 +2,9 @@ package org.util;
|
||||
|
||||
import org.*;
|
||||
import org.model.Vehicle;
|
||||
|
||||
import org.model.ElectricCar;
|
||||
import org.model.SportsCar;
|
||||
import org.model.GasCar;
|
||||
/*
|
||||
* Utility class to inspect vehicles.
|
||||
* Demonstrates instanceof and polymorphism.
|
||||
@@ -14,9 +16,14 @@ public class VehicleInspector {
|
||||
|
||||
v.basicInfo();
|
||||
|
||||
// TODO:
|
||||
// If ElectricCar → print "Check battery system"
|
||||
// If SportsCar → print "Check brakes and performance"
|
||||
// If GasCar → print "Check engine and oil"
|
||||
if (v instanceof SportsCar) {
|
||||
System.out.println("→ Check brakes and performance");
|
||||
}
|
||||
else if (v instanceof ElectricCar) {
|
||||
System.out.println("→ Check battery system");
|
||||
}
|
||||
else if (v instanceof GasCar) {
|
||||
System.out.println("→ Check engine and oil");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user