4 Commits
Author SHA1 Message Date
MehrdadShirvani 3a9aadfd16 Merge pull request 'develop' (#1) from develop into master - Full Mark 100/100 (No Bonus)
Full Mark 100/100 (No Bonus)
2026-05-15 12:56:12 +00:00
bitahajati a96e2ada01 Complete other classes 2026-04-28 15:46:24 +03:30
bitahajati d6cae281d9 Complete the class Car 2026-04-27 16:36:23 +03:30
bitahajati 2de716271c Complete the class Vehicle 2026-04-27 15:35:51 +03:30
7 changed files with 210 additions and 165 deletions
+51 -114
View File
@@ -1,126 +1,63 @@
package org; package org;
public class Main { import org.behavior.Serviceable;
public static void main(String[] args) { import org.model.ElectricCar;
import org.model.GasCar;
import org.model.SportsCar;
import org.model.Vehicle;
import org.util.VehicleInspector;
// ============================================================ public class Main
// TODO 1: {
// Create one instance of each vehicle type: public static void main(String[] args)
// - ElectricCar {
// - GasCar ElectricCar tesla = new ElectricCar("Tesla", "Model S", 2022, 1000,
// - SportsCar 100, 550, 50);
//
// Requirements: GasCar toyota = new GasCar("Toyota", "Corolla", 2018, 85000,
// • Use the constructors you created in each class. 5, 50, "Petrol");
// • Provide realistic values (brand, model, year, mileage, etc.).
// • Store them in variables such as: SportsCar ferrari = new SportsCar("Ferrari", "F8 Tributo", 2021, 12000,
// ElectricCar tesla = new ElectricCar(...); 2, 3.9, "oil", 710);
// ============================================================
Vehicle[] vehicles = {tesla, toyota, ferrari};
System.out.println();
for (Vehicle v : vehicles)
{
v.basicInfo();
System.out.println("----------------------------------------------------------------");
}
System.out.println();
System.out.println("==================================================================");
System.out.println();
for (Vehicle v : vehicles)
{
VehicleInspector.inspect(v);
System.out.println("----------------------------------------------------------------");
}
System.out.println();
System.out.println("==================================================================");
// ============================================================ Serviceable[] serviceables = {(Serviceable) tesla, (Serviceable) toyota, (Serviceable) 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).
// ============================================================
// ============================================================
// 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 (Serviceable s : serviceables)
{
Vehicle v = (Vehicle) s;
if (s.needsService(v))
{
System.out.println(v.getBrand() + " " + v.getModel());
s.performService();
}
else
{
System.out.println(v.getBrand() + " " + v.getModel() + " does not need service");
}
System.out.println("----------------------------------------------------------------");
}
System.out.println("=== Workshop Completed ===");
} }
} }
+30 -9
View File
@@ -11,22 +11,31 @@ package org.model;
import org.behavior.Serviceable; import org.behavior.Serviceable;
public abstract class Car extends Vehicle implements Serviceable { public abstract class Car extends Vehicle implements Serviceable
{
private int seats; private int seats;
public Car(String brand, String model, int year, public Car(String brand, String model, int year, double mileage, int seats)
double mileage, int seats) { {
super(brand, model, year, mileage);
// TODO: Call super constructor
this.seats = seats; 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("==========================");
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);
System.out.println("==========================");
}
/* /*
* Implement Serviceable methods: * Implement Serviceable methods:
* *
@@ -38,4 +47,16 @@ public abstract class Car extends Vehicle implements Serviceable {
* performService(): * performService():
* - Print: "General car service completed" * - Print: "General car service completed"
*/ */
@Override
public boolean needsService(Vehicle v)
{
if (getMileage() > 1000 ) return true;
return false;
}
@Override
public void performService()
{
System.out.println("Gneral car service completed");
}
} }
+45 -13
View File
@@ -13,27 +13,59 @@ package org.model;
import org.behavior.Chargeable; import org.behavior.Chargeable;
public class ElectricCar extends Car implements Chargeable { public class ElectricCar extends Car implements Chargeable
{
private int batteryCapacity; private int batteryCapacity;
private double chargeLevel; private double chargeLevel;
public ElectricCar(String brand, String model, int year, public ElectricCar(String brand, String model, int year, double mileage, int seats, int batteryCapacity, double chargeLevel)
double mileage, int seats, {
int batteryCapacity, double chargeLevel) {
super(brand, model, year, mileage, seats); super(brand, model, year, mileage, seats);
this.batteryCapacity = batteryCapacity; this.batteryCapacity = batteryCapacity;
this.chargeLevel = chargeLevel; this.chargeLevel = chargeLevel;
} }
// TODO: Override getEnergyType() → return "Electric" @Override
public String getEnergyType() { return "Electric";}
// TODO: Override showDetails() @Override
// Call super.showDetails() public void showDetails()
// Print battery capacity and charge level {
super.showDetails();
System.out.println("The Capacity of the battery : " + batteryCapacity);
System.out.println("The charge level : " + chargeLevel);
}
// TODO: Implement charge(double amount) @Override
// Validate negative public void charge(double amount)
// Cap at 100 {
if (amount < 0)
{
System.out.println("Error !");
return;
}
chargeLevel += amount;
if (chargeLevel > 100)
{
chargeLevel = 100;
System.out.println("Full !");
}
}
@Override
public boolean needsService(Vehicle v)
{
if ( getMileage() > 8000) return true;
return false;
}
@Override
public void performService()
{
System.out.println("Battery system checked");
this.chargeLevel = 100;
System.out.println("Charge level reset to 100%");
}
} }
+45 -13
View File
@@ -13,27 +13,59 @@ package org.model;
import org.behavior.Refuelable; import org.behavior.Refuelable;
public class GasCar extends Car implements Refuelable { public class GasCar extends Car implements Refuelable
{
private double fuelLevel; private double fuelLevel;
private String fuelType; private String fuelType;
public GasCar(String brand, String model, int year, public GasCar(String brand, String model, int year, double mileage, int seats, double fuelLevel, String fuelType)
double mileage, int seats, {
double fuelLevel, String fuelType) {
super(brand, model, year, mileage, seats); super(brand, model, year, mileage, seats);
this.fuelLevel = fuelLevel; this.fuelLevel = fuelLevel;
this.fuelType = fuelType; this.fuelType = fuelType;
} }
// TODO: Override getEnergyType() → return fuelType + " (Gas)" @Override
public String getEnergyType() { return fuelType + "Gas"; }
// TODO: Implement refuel(double amount) @Override
// Validate amount public void refuel(double amount)
// Cap at 100 {
if (amount < 0)
{
System.out.println("Error");
return;
}
// TODO: Override showDetails() fuelLevel += amount;
// Call super.showDetails() if (fuelLevel > 100)
// Print fuel level and fuel type {
fuelLevel = 100;
System.out.println("Full !");
}
}
@Override
public void showDetails()
{
super.showDetails();
System.out.println("Fuel level : " + fuelLevel);
System.out.println("Fuel type : " + fuelType);
}
@Override
public boolean needsService(Vehicle v)
{
if (getMileage() > 12000) return true;
return false;
}
@Override
public void performService()
{
System.out.println("Engine oil changed");
this.fuelLevel = 100;
System.out.println("Fuel level reset to 100");
}
} }
+16 -3
View File
@@ -24,7 +24,20 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred; this.zeroToHundred = zeroToHundred;
} }
// TODO: Override showDetails() @Override
// Call super.showDetails() public void showDetails()
// Print acceleration time {
super.showDetails();
System.out.println("Acceleration time : " + zeroToHundred);
}
@Override
public boolean needsService(Vehicle v)
{
if (getMileage() > 5000) return true;
return false;
}
@Override
public void performService() { System.out.println("High-performance brake system checked"); }
} }
+13 -5
View File
@@ -20,13 +20,21 @@ 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() { return model; }
// If km is negative → print error
// Otherwise increase mileage
// TODO: Declare abstract method getEnergyType() 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();
public void basicInfo() { public void basicInfo() {
System.out.println( System.out.println(
+10 -8
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;
/* /*
@@ -8,15 +11,14 @@ import org.model.Vehicle;
* Demonstrates instanceof and polymorphism. * Demonstrates instanceof and polymorphism.
*/ */
public class VehicleInspector { public class VehicleInspector
{
public static void inspect(Vehicle v) { public static void inspect(Vehicle v)
{
v.basicInfo(); v.basicInfo();
// TODO: if (v instanceof ElectricCar) System.out.println("Check battery system");
// If ElectricCar → print "Check battery system" else if (v instanceof SportsCar) System.out.println("Check brakes and performance");
// If SportsCar → print "Check brakes and performance" else if (v instanceof GasCar) System.out.println("Check engine and oil");
// If GasCar → print "Check engine and oil"
} }
} }