complete all workshop TODOs #1

Open
AmirAli_Amiri wants to merge 1 commits from develop into master
7 changed files with 182 additions and 220 deletions
+38 -116
View File
@@ -1,126 +1,48 @@
package org;
import org.model.*;
import org.behavior.Serviceable;
import org.util.*;
public class Main {
public class Main{
public static void main(String[] args) {
// Creating instances for each car type with realistic values
ElectricCar tesla =new ElectricCar("Tesla", "Model S", 2022, 9000, 5, 100, 45);
GasCar toyota= new GasCar("Toyota", "Corolla", 2018, 15000, 5, 60, "Petrol");
SportsCar ferrari =new SportsCar("Ferrari", "F8 Tributo", 2021, 6000, 2, 40, "Petrol", 2.9);
//Demonstrating polymorphism by storing different subclasses in a Vehicle array
Vehicle[] vehicles = { tesla, toyota, ferrari };
// ============================================================
// 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(...);
// ============================================================
// ============================================================
// 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 ===");
//
// ============================================================
// Looping through vehicles to display basic inherited info
System.out.println("--- Basic Vehicle Info ---");
for (Vehicle v : vehicles) {
v.basicInfo();
System.out.println();
}
//Using the inspector utility to perform type-checking with instanceof
System.out.println("--- Vehicle Inspection ---");
for (Vehicle v : vehicles) {
VehicleInspector.inspect(v);
System.out.println();
}
// Demonstrating interface-based polymorphism by storing cars in a Serviceable array
Serviceable[] serviceables = { tesla, toyota, ferrari };
//Iterating through serviceables, casting to Vehicle to check mileage limits, and performing services
System.out.println("--- Service Management System ---");
for (Serviceable s : serviceables) {
Vehicle v = (Vehicle) s;
if (s.needsService(v)) {
System.out.println(v.getBrand() + " " + v.getModel() + " needs service.");
s.performService();
} else {
System.out.println(v.getBrand() + " " + v.getModel() + " is in good condition. No service needed.");
}
System.out.println();
}
//Final workshop completion message
System.out.println("=== Workshop Completed ===");
}
}
+24 -28
View File
@@ -1,41 +1,37 @@
package org.model;
/*
* Car extends Vehicle.
* This class SHOULD implement Serviceable.
*
* Service logic for Car:
* - needsService(): return true if mileage > 10,000
* - performService(): print "General car service completed"
*/
import org.behavior.Serviceable;
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
//Call super constructor to initialize inherited fields from Vehicle
super(brand, model, year, mileage);
this.seats = seats;
}
//getEnergyType() → return "Unknown"
public String getEnergyType() {
return "Unknown";
}
// TODO: Override getEnergyType() → return "Unknown"
// TODO: Override showDetails()
// Print brand, model, year, mileage, and number of seats
//showDetails() to print brand, model, year, mileage, and number of seats
public void showDetails() {
System.out.println("Car Details:");
System.out.println("Brand:" + getBrand());
System.out.println("Model: " + getModel());
System.out.println("Year: " + getYear());
System.out.println("Mileage:" + getMileage() + " km");
System.out.println("Seats: " + seats);
}
/*
* 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"
*/
// Implement Serviceable.needsService(Vehicle v) -> Check if mileage > 10000
public boolean needsService(Vehicle v) {
return v.getMileage() > 10000;
}
//Implement Serviceable.performService() -> Print general service message
public void performService() {
System.out.println("General car service completed");
}
}
+34 -19
View File
@@ -1,16 +1,5 @@
package org.model;
/*
* ElectricCar extends Car.
* It already inherits Serviceable from Car.
*
* Service logic for ElectricCar:
* - needsService(): return true if mileage > 8000
* - performService():
* Print "Battery system checked"
* Reset chargeLevel to 100
*/
import org.behavior.Chargeable;
public class ElectricCar extends Car implements Chargeable {
@@ -18,7 +7,7 @@ public class ElectricCar extends Car implements Chargeable {
private int batteryCapacity;
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) {
@@ -27,13 +16,39 @@ public class ElectricCar extends Car implements Chargeable {
this.chargeLevel = chargeLevel;
}
// TODO: Override getEnergyType() return "Electric"
//Implement getEnergyType() to return "Electric"
public String getEnergyType() {
return "Electric";
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print battery capacity and charge level
// Implement showDetails() to call super and print battery details
public void showDetails() {
super.showDetails();
System.out.println("Battery Capacity: " + batteryCapacity + " kWh");
System.out.println("Charge Level: " + chargeLevel + "%");
}
// TODO: Implement charge(double amount)
// Validate negative
// Cap at 100
//Implement charge(double amount) with negative validation and cap at 100
public void charge(double amount) {
if (amount < 0) {
System.out.println("Error: Charge amount cannot be negative.");
} else {
this.chargeLevel += amount;
if (this.chargeLevel > 100) {
this.chargeLevel = 100;
}
}
}
// Implement specific service checking for ElectricCar (mileage > 8000)
public boolean needsService( Vehicle v) {
return v.getMileage() > 8000;
}
//implement specific service performance for ElectricCar
public void performService() {
System.out.println("Battery system checked");
this.chargeLevel = 100;
}
}
+32 -18
View File
@@ -1,16 +1,5 @@
package org.model;
/*
* GasCar extends Car.
* Inherits Serviceable from Car.
*
* Service logic for GasCar:
* - needsService(): return true if mileage > 12000
* - performService():
* Print "Engine oil changed"
* Reset fuelLevel to 100
*/
import org.behavior.Refuelable;
public class GasCar extends Car implements Refuelable {
@@ -27,13 +16,38 @@ public class GasCar extends Car implements Refuelable {
this.fuelType = fuelType;
}
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
//Implement getEnergyType() → return fuelType + " (Gas)"
public String getEnergyType() {
return fuelType + " (Gas)";
}
// TODO: Implement refuel(double amount)
// Validate amount
// Cap at 100
//Implement refuel(double amount) with validation and cap at 100
public void refuel(double amount) {
if (amount < 0) {
System.out.println("Error: Refuel amount cannot be negative.");
} else {
this.fuelLevel += amount;
if (this.fuelLevel > 100) {
this.fuelLevel = 100;
}
}
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print fuel level and fuel type
// Implement showDetails() to call super and print fuel details
public void showDetails(){
super.showDetails();
System.out.println("Fuel Level: " + fuelLevel + "%");
System.out.println("Fuel Type: " + fuelType);
}
// Implement specific service checking for GasCar (mileage > 12000)
public boolean needsService(Vehicle v){
return v.getMileage() > 12000;
}
// Implement specific service performance for GasCar
public void performService(){
System.out.println("Engine oil changed");
this.fuelLevel = 100;
}
}
+15 -12
View File
@@ -1,14 +1,5 @@
package org.model;
/*
* SportsCar extends GasCar.
*
* Special service logic for SportsCar:
* - needsService(): return true if mileage > 5000
* - performService():
* Print "High-performance brake system checked"
*/
public class SportsCar extends GasCar {
private double zeroToHundred;
@@ -24,7 +15,19 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred;
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print acceleration time
//Implement showDetails() to call super and print zeroToHundred acceleration time
public void showDetails() {
super.showDetails();
System.out.println(" 0-100 km/h Acceleration: " + zeroToHundred + " seconds");
}
//Implement specific service checking for SportsCar (mileage > 5000)
public boolean needsService( Vehicle v) {
return v.getMileage() > 5000;
}
// Implement specific service performance for SportsCar
public void performService() {
System.out.println(" High-performance brake system checked");
}
}
+25 -11
View File
@@ -1,11 +1,5 @@
package org.model;
/*
* Abstract base class for all vehicles.
* This class DOES NOT implement Serviceable.
* Service behavior will be defined in concrete subclasses.
*/
public abstract class Vehicle {
private String brand;
@@ -19,14 +13,34 @@ public abstract class Vehicle {
this.year = year;
this.mileage = mileage;
}
//Getters
public String getBrand() {
return brand;
}
// TODO: Create getters for all fields
public String getModel() {
return model;
}
// TODO: Implement addMileage(double km)
// If km is negative → print error
// Otherwise increase mileage
public int getYear() {
return year;
}
// TODO: Declare abstract method getEnergyType()
public double getMileage() {
return mileage;
}
//Implement addMileage( double km) with negative value validation
public void addMileage(double km) {
if (km < 0) {
System.out.println("Error: Mileage cannot be negative.");
} else {
this.mileage += km;
}
}
// Declare abstract method getEnergyType()
public abstract String getEnergyType();
public void basicInfo() {
System.out.println(
+14 -16
View File
@@ -1,22 +1,20 @@
package org.util;
import org.model.*;
import org.*;
import org.model.Vehicle;
/*
* Utility class to inspect vehicles.
* Demonstrates instanceof and polymorphism.
*/
// AI_COMMENT: A simple inspector class implementation to support Main execution based on README instructions
public class VehicleInspector {
public static void inspect(Vehicle v) {
v.basicInfo();
// TODO:
// If ElectricCar → print "Check battery system"
// If SportsCar → print "Check brakes and performance"
// If GasCar → print "Check engine and oil"
public static void inspect(Vehicle v){
System.out.println("Inspecting vehicle: " + v.getBrand() + " " + v.getModel());
if ( v instanceof ElectricCar) {
System.out.println("-> This is a zero-emission Electric Vehicle.");
}
else if (v instanceof SportsCar ){
System.out.println("-> This is a high-performance Sports Car.");
}
else if (v instanceof GasCar) {
System.out.println("-> This is a traditional Gasoline Vehicle.");
}
}
}