Workshop 3 (oop) is implemented #1
Generated
+1
@@ -0,0 +1 @@
|
||||
Vehicle.java
|
||||
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>
|
||||
+79
-13
@@ -1,5 +1,7 @@
|
||||
package org;
|
||||
|
||||
import org.behavior.Serviceable;
|
||||
import org.model.*;
|
||||
import org.util.VehicleInspector;
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -16,7 +18,36 @@ public class Main {
|
||||
// • Store them in variables such as:
|
||||
// ElectricCar tesla = new ElectricCar(...);
|
||||
// ============================================================
|
||||
ElectricCar tesla = new ElectricCar(
|
||||
"Tesla",
|
||||
"Model Y",
|
||||
2026,
|
||||
5000,
|
||||
7,
|
||||
81,
|
||||
65
|
||||
);
|
||||
|
||||
GasCar pride = new GasCar(
|
||||
"saipa",
|
||||
"pride",
|
||||
2020,
|
||||
123000,
|
||||
4,
|
||||
35,
|
||||
"Petrol"
|
||||
);
|
||||
|
||||
SportsCar mclaren = new SportsCar(
|
||||
"McLaren",
|
||||
"720S",
|
||||
2026,
|
||||
3600,
|
||||
2,
|
||||
72,
|
||||
"Premium Petrol",
|
||||
2.9
|
||||
);
|
||||
|
||||
|
||||
|
||||
@@ -31,7 +62,11 @@ public class Main {
|
||||
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
||||
// as their parent type (Vehicle).
|
||||
// ============================================================
|
||||
|
||||
Vehicle[] vehicles = {
|
||||
tesla,
|
||||
pride,
|
||||
mclaren
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -46,9 +81,12 @@ public class Main {
|
||||
// Demonstrate that all subclasses share the same Vehicle
|
||||
// behavior and can call inherited methods.
|
||||
// ============================================================
|
||||
System.out.println("=== Vehicle Information ===");
|
||||
|
||||
|
||||
|
||||
for (Vehicle v : vehicles) {
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
// ============================================================
|
||||
@@ -62,6 +100,13 @@ public class Main {
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
System.out.println("=== Vehicle Inspection ===");
|
||||
|
||||
for (Vehicle v : vehicles) {
|
||||
VehicleInspector.inspect(v);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -79,7 +124,11 @@ public class Main {
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
Serviceable[] serviceables = {
|
||||
tesla,
|
||||
pride,
|
||||
mclaren
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -106,21 +155,38 @@ public class Main {
|
||||
// Add blank lines between each item for readability.
|
||||
// ============================================================
|
||||
|
||||
System.out.println("=== Service Check ===");
|
||||
|
||||
for (Serviceable s : serviceables) {
|
||||
|
||||
Vehicle v = (Vehicle) s;
|
||||
|
||||
if (s.needsService(v)) {
|
||||
|
||||
// ============================================================
|
||||
// TODO 7 (Optional):
|
||||
// After everything is done, print a final message:
|
||||
//
|
||||
// System.out.println("=== Workshop Completed ===");
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
System.out.println(
|
||||
v.getBrand() + " " + v.getModel()
|
||||
+ " needs service."
|
||||
);
|
||||
|
||||
s.performService();
|
||||
}
|
||||
else {
|
||||
|
||||
System.out.println(
|
||||
v.getBrand() + " " + v.getModel()
|
||||
+ " does not need service."
|
||||
);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -18,24 +18,34 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
public Car(String brand, String model, int year,
|
||||
double mileage, int seats) {
|
||||
|
||||
// TODO: Call super constructor
|
||||
super(brand, model, year, mileage);
|
||||
this.seats = seats;
|
||||
}
|
||||
public int getSeats() {
|
||||
return seats;
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return "Unknown"
|
||||
// Here in getEnergyType we return null but later we override it accordingly to each type of car
|
||||
@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());
|
||||
System.out.println("Model: " + getModel());
|
||||
System.out.println("Year: " + getYear());
|
||||
System.out.println("Mileage: " + getMileage());
|
||||
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"
|
||||
*/
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage() > 10000;
|
||||
}
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,19 +21,48 @@ public class ElectricCar extends Car implements Chargeable {
|
||||
public ElectricCar(String brand, String model, int year,
|
||||
double mileage, int seats,
|
||||
int batteryCapacity, double chargeLevel) {
|
||||
|
||||
// becuase we inherit fields from car, we use super keyword
|
||||
super(brand, model, year, mileage, seats);
|
||||
this.batteryCapacity = batteryCapacity;
|
||||
this.chargeLevel = chargeLevel;
|
||||
}
|
||||
|
||||
// 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() {
|
||||
// we inherit showDetails from Car again so we use super
|
||||
super.showDetails();
|
||||
System.out.println("Battery Capacity: " + batteryCapacity);
|
||||
System.out.println("Charge Level: " + chargeLevel);
|
||||
}
|
||||
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
@Override
|
||||
public void charge(double amount) {
|
||||
|
||||
if (amount < 0) {
|
||||
System.out.println("Invalid charge amount");
|
||||
return;
|
||||
}
|
||||
|
||||
chargeLevel += amount;
|
||||
|
||||
if (chargeLevel > 100) {
|
||||
chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage() > 8000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Battery system checked");
|
||||
chargeLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,41 @@ 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
|
||||
@Override
|
||||
public void refuel(double amount) {
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
if (amount < 0) {
|
||||
System.out.println("Invalid fuel amount");
|
||||
return;
|
||||
}
|
||||
|
||||
fuelLevel += amount;
|
||||
|
||||
if (fuelLevel > 100) {
|
||||
fuelLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
System.out.println("Fuel Level: " + fuelLevel);
|
||||
System.out.println("Fuel Type: " + fuelType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage() > 12000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("Engine oil changed");
|
||||
fuelLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +23,18 @@ public class SportsCar extends GasCar {
|
||||
|
||||
this.zeroToHundred = zeroToHundred;
|
||||
}
|
||||
@Override
|
||||
public void showDetails(){
|
||||
super.showDetails();
|
||||
System.out.println("Acceleration Time: " + zeroToHundred + " sec");
|
||||
}
|
||||
@Override
|
||||
public boolean needsService(Vehicle v) {
|
||||
return v.getMileage() > 5000;
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print acceleration time
|
||||
@Override
|
||||
public void performService() {
|
||||
System.out.println("High-performance brake system checked");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,15 +19,33 @@ public abstract class Vehicle {
|
||||
this.year = year;
|
||||
this.mileage = mileage;
|
||||
}
|
||||
// Here is all getter method of the fields
|
||||
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;
|
||||
}
|
||||
|
||||
public void addMileage(double km) {
|
||||
if (km < 0) {
|
||||
System.out.println("Mileage cannot be negative");
|
||||
return;
|
||||
}
|
||||
mileage += km;
|
||||
}
|
||||
|
||||
public abstract String getEnergyType();
|
||||
// basicInfo returns the info about vehicle such as brand and energy
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
year + " " + brand + " " + model +
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.util;
|
||||
|
||||
import org.*;
|
||||
import org.model.Vehicle;
|
||||
import org.model.*;
|
||||
|
||||
/*
|
||||
* Utility class to inspect vehicles.
|
||||
@@ -13,10 +13,15 @@ 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"
|
||||
// Here we are using instanceof to check vehicle's kind
|
||||
if (v instanceof ElectricCar) {
|
||||
System.out.println("Check battery system");
|
||||
}
|
||||
else if (v instanceof SportsCar) {
|
||||
System.out.println("Check brakes and performance");
|
||||
}
|
||||
else if (v instanceof GasCar) {
|
||||
System.out.println("Check engine and oil");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user