complete code

This commit is contained in:
2026-04-28 00:53:37 +03:30
parent 6cb8f9a8d8
commit c65d263b45
7 changed files with 167 additions and 141 deletions
+38 -111
View File
@@ -1,126 +1,53 @@
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) {
// ============================================================ ElectricCar tesla = new ElectricCar(
// TODO 1: "Tesla", "Model S", 2022, 1000,
// Create one instance of each vehicle type: 100, 550, 50 // batteryCapacity, range
// - 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(...);
// ============================================================
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
);
// ============================================================ // ============================================================
// TODO 2: Vehicle[] vehicles = {tesla, toyota, ferrari};
// Create an array of Vehicle: for (Vehicle v : vehicles) {
// Vehicle[] vehicles = { ... }; v.basicInfo();
// 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 subclassspecific inspection messages
// ============================================================
// ============================================================
// TODO 5:
// Create an array of Serviceable:
// Serviceable[] serviceables = { ... };
//
// This demonstrates **interfacebased 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 ===");
//
// ============================================================
for (Vehicle v : vehicles) {
VehicleInspector.inspect(v);
System.out.println();
}
Serviceable[] serviceList = {tesla, toyota, ferrari};
for (Serviceable s : serviceList) {
Vehicle v = (Vehicle) s;
if (s.needsService(v)) {
s.performService();
} else {
System.out.println("The car does not need service.");
}
} }
System.out.println("=== Workshop Completed ===");
}
} }
+18 -3
View File
@@ -18,13 +18,19 @@ 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" public String getEnergyType(){
return "UnKnown";
}
// TODO: Override showDetails() public void showDetails(){
System.out.println("Brand: " + getBrand() + ", Model: " + getModel() +
", Year: " + getYear() + ", Mileage: " + getMileage() +
", Seats: " + seats);
}
// Print brand, model, year, mileage, and number of seats // Print brand, model, year, mileage, and number of seats
/* /*
@@ -38,4 +44,13 @@ public abstract class Car extends Vehicle implements Serviceable {
* performService(): * performService():
* - Print: "General car service completed" * - Print: "General car service completed"
*/ */
public boolean needsService(Vehicle v) {
return v.getMileage() > 10000;
}
public void performService() {
System.out.println("General car service completed");
}
} }
+29 -7
View File
@@ -27,13 +27,35 @@ public class ElectricCar extends Car implements Chargeable {
this.chargeLevel = chargeLevel; this.chargeLevel = chargeLevel;
} }
// TODO: Override getEnergyType() → return "Electric" public String getEnergyType() {
return "Electric";
}
// TODO: Override showDetails() public void showDetails() {
// Call super.showDetails() super.showDetails();
// Print battery capacity and charge level System.out.println("Battery Capacity: " + batteryCapacity + " , Charge Level: " + chargeLevel + "%");
}
// TODO: Implement charge(double amount) public void charge(double amount) {
// Validate negative if (amount < 0) {
// Cap at 100 System.out.println("invalid amount");
} else {
this.chargeLevel = Math.min(100, this.chargeLevel + amount);
}
}
public boolean needsService() {
return getMileage() > 8000;
}
public void performService() {
System.out.println("Battery system checked");
this.chargeLevel = 100;
System.out.println("Charge level reset to 100%.");
}
} }
+25 -7
View File
@@ -27,13 +27,31 @@ public class GasCar extends Car implements Refuelable {
this.fuelType = fuelType; this.fuelType = fuelType;
} }
// TODO: Override getEnergyType() → return fuelType + " (Gas)" public String getEnergyType() {
return fuelType + " (Gas)";
}
// TODO: Implement refuel(double amount) public void refuel(double amount) {
// Validate amount if (amount > 0) {
// Cap at 100 this.fuelLevel = Math.min(100, this.fuelLevel + amount);
} else {
System.out.println("Invalid fuel amount.");
}
}
// TODO: Override showDetails() public void showDetails() {
// Call super.showDetails() super.showDetails();
// Print fuel level and fuel type System.out.println("Fuel Level: " + fuelLevel + "%, Fuel Type: " + fuelType);
}
public boolean needsService() {
return getMileage() > 12000;
}
public void performService() {
System.out.println("Engine oil changed");
this.fuelLevel = 100;
}
} }
+15 -3
View File
@@ -24,7 +24,19 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred; this.zeroToHundred = zeroToHundred;
} }
// TODO: Override showDetails() public void showDetails() {
// Call super.showDetails() super.showDetails(); // نمایش جزئیات پایه از کلاس GasCar
// Print acceleration time System.out.println("0 to 100 km/h: " + zeroToHundred + " seconds");
}
public boolean needsService() {
return getMileage() > 5000;
}
public void performService() {
System.out.println("High-performance brake system checked");
}
} }
+28 -4
View File
@@ -20,11 +20,33 @@ 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 }
public int getYear(){
return year;
}
public double getMileage(){
return mileage;
}
public void addMileage(double km){
if( km < 0 ){
System.out.println("Error");
}
else{
this.mileage += km;
}
}
public abstract String getEnergyType();
// TODO: Declare abstract method getEnergyType() // TODO: Declare abstract method getEnergyType()
@@ -36,5 +58,7 @@ public abstract class Vehicle {
); );
} }
public abstract void showDetails(); public abstract void showDetails();
} }
+12 -4
View File
@@ -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,14 @@ 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");
}
} }
} }