Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d8cb156b0 |
Generated
+1
@@ -0,0 +1 @@
|
|||||||
|
Vehicle.java
|
||||||
Generated
+1
-1
@@ -8,7 +8,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
+79
-13
@@ -1,5 +1,7 @@
|
|||||||
package org;
|
package org;
|
||||||
|
import org.behavior.Serviceable;
|
||||||
|
import org.model.*;
|
||||||
|
import org.util.VehicleInspector;
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
@@ -16,7 +18,36 @@ public class Main {
|
|||||||
// • Store them in variables such as:
|
// • Store them in variables such as:
|
||||||
// ElectricCar tesla = new ElectricCar(...);
|
// 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
|
// subclasses (ElectricCar, GasCar, SportsCar) are stored
|
||||||
// as their parent type (Vehicle).
|
// 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
|
// Demonstrate that all subclasses share the same Vehicle
|
||||||
// behavior and can call inherited methods.
|
// 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
|
// • 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;
|
// Vehicle v = (Vehicle) s;
|
||||||
//
|
//
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
Serviceable[] serviceables = {
|
||||||
|
tesla,
|
||||||
|
pride,
|
||||||
|
mclaren
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -106,21 +155,38 @@ public class Main {
|
|||||||
// Add blank lines between each item for readability.
|
// 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)) {
|
||||||
|
|
||||||
// ============================================================
|
System.out.println(
|
||||||
// TODO 7 (Optional):
|
v.getBrand() + " " + v.getModel()
|
||||||
// After everything is done, print a final message:
|
+ " needs service."
|
||||||
//
|
);
|
||||||
// System.out.println("=== Workshop Completed ===");
|
|
||||||
//
|
|
||||||
// ============================================================
|
|
||||||
|
|
||||||
|
|
||||||
|
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,
|
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;
|
||||||
}
|
}
|
||||||
|
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()
|
@Override
|
||||||
// Print brand, model, year, mileage, and number of seats
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
@Override
|
||||||
* Implement Serviceable methods:
|
public boolean needsService(Vehicle v) {
|
||||||
*
|
return v.getMileage() > 10000;
|
||||||
* needsService(Vehicle v):
|
}
|
||||||
* - Check the mileage of the current car
|
@Override
|
||||||
* - If mileage > 10000 return true
|
public void performService() {
|
||||||
* - Otherwise return false
|
System.out.println("General car service completed");
|
||||||
*
|
}
|
||||||
* performService():
|
|
||||||
* - Print: "General car service completed"
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,19 +21,48 @@ public class ElectricCar extends Car implements Chargeable {
|
|||||||
public ElectricCar(String brand, String model, int year,
|
public ElectricCar(String brand, String model, int year,
|
||||||
double mileage, int seats,
|
double mileage, int seats,
|
||||||
int batteryCapacity, double chargeLevel) {
|
int batteryCapacity, double chargeLevel) {
|
||||||
|
// becuase we inherit fields from car, we use super keyword
|
||||||
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
|
// 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)
|
@Override
|
||||||
// Validate negative
|
public void charge(double amount) {
|
||||||
// Cap at 100
|
|
||||||
|
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;
|
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
|
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
if (amount < 0) {
|
||||||
// Call super.showDetails()
|
System.out.println("Invalid fuel amount");
|
||||||
// Print fuel level and fuel type
|
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;
|
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()
|
@Override
|
||||||
// Call super.showDetails()
|
public void performService() {
|
||||||
// Print acceleration time
|
System.out.println("High-performance brake system checked");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,15 +19,33 @@ public abstract class Vehicle {
|
|||||||
this.year = year;
|
this.year = year;
|
||||||
this.mileage = mileage;
|
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)
|
public int getYear() {
|
||||||
// If km is negative → print error
|
return year;
|
||||||
// Otherwise increase mileage
|
}
|
||||||
|
|
||||||
// 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() {
|
public void basicInfo() {
|
||||||
System.out.println(
|
System.out.println(
|
||||||
year + " " + brand + " " + model +
|
year + " " + brand + " " + model +
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package org.util;
|
package org.util;
|
||||||
|
|
||||||
import org.*;
|
import org.*;
|
||||||
import org.model.Vehicle;
|
import org.model.*;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Utility class to inspect vehicles.
|
* Utility class to inspect vehicles.
|
||||||
@@ -13,10 +13,15 @@ public class VehicleInspector {
|
|||||||
public static void inspect(Vehicle v) {
|
public static void inspect(Vehicle v) {
|
||||||
|
|
||||||
v.basicInfo();
|
v.basicInfo();
|
||||||
|
// Here we are using instanceof to check vehicle's kind
|
||||||
// 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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user