1 Commits
Author SHA1 Message Date
Fatemesadat Mirabootalebi 6903f5c0ad Complete Car project implementation. 2026-05-02 12:03:14 -07:00
7 changed files with 156 additions and 17 deletions
+30 -8
View File
@@ -1,4 +1,7 @@
package org; package org;
import org.model.*;
import org.behavior.*;
import org.util.*;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
@@ -16,7 +19,9 @@ 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 S", 2023, 9000, 5, 100, 85);
GasCar toyota = new GasCar("Toyota", "Camry", 2020, 15000, 5, 60, "Gasoline");
SportsCar ferrari = new SportsCar("Ferrari", "SF90", 2024, 4000, 2, 70, "Premium", 2.5);
@@ -31,6 +36,7 @@ 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, toyota, ferrari};
@@ -46,7 +52,10 @@ 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.
// ============================================================ // ============================================================
for (Vehicle v : vehicles){
v.basicInfo();
System.out.println();
}
@@ -61,7 +70,10 @@ public class Main {
// • Demonstrate the use of `instanceof` // • Demonstrate the use of `instanceof`
// • Show subclassspecific inspection messages // • Show subclassspecific inspection messages
// ============================================================ // ============================================================
for (Vehicle v : vehicles){
VehicleInspector.inspect(v);
System.out.println();
}
@@ -79,7 +91,7 @@ public class Main {
// Vehicle v = (Vehicle) s; // Vehicle v = (Vehicle) s;
// //
// ============================================================ // ============================================================
Serviceable[] serviceables = {tesla, toyota, ferrari};
@@ -105,6 +117,19 @@ public class Main {
// //
// Add blank lines between each item for readability. // Add blank lines between each item for readability.
// ============================================================ // ============================================================
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() + " does not need service.");
}
System.out.println();
}
@@ -117,10 +142,7 @@ public class Main {
// System.out.println("=== Workshop Completed ==="); // System.out.println("=== Workshop Completed ===");
// //
// ============================================================ // ============================================================
System.out.println("=== Workshop Completed ===");
} }
} }
+20 -2
View File
@@ -15,14 +15,18 @@ 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) {
// TODO: Call super constructor // TODO: Call super constructor
super(brand, model, year, mileage);
this.seats = seats; this.seats = seats;
} }
// TODO: Override getEnergyType() → return "Unknown" // TODO: Override getEnergyType() → return "Unknown"
@Override
public String getEnergyType() {
return "Unknown";
}
// TODO: Override showDetails() // TODO: Override showDetails()
// Print brand, model, year, mileage, and number of seats // Print brand, model, year, mileage, and number of seats
@@ -38,4 +42,18 @@ public abstract class Car extends Vehicle implements Serviceable {
* performService(): * performService():
* - Print: "General car service completed" * - Print: "General car service completed"
*/ */
@Override
public void showDetails(){
System.out.println("Car Details: " + getYear() + " " + getBrand() + " " + getModel() + " | Mileage: " + getMileage() + " | Seats: " + seats);
}
@Override
public boolean needsService(Vehicle v){
return v.getMileage() > 10_000;
}
@Override
public void performService() {
System.out.println("General car service completed");
}
} }
+36 -5
View File
@@ -18,22 +18,53 @@ 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" // TODO: Override getEnergyType() → return "Electric"
@Override
public String getEnergyType(){
return "Electric";
}
// TODO: Override showDetails() // TODO: Override showDetails()
// Call super.showDetails() // Call super.showDetails()
// Print battery capacity and charge level // Print battery capacity and charge level
@Override
public void showDetails(){
super.showDetails();
System.out.println("Battery Capacity: " + batteryCapacity + " kWh" + " | Charge Level: " + chargeLevel + "%"
);
}
// TODO: Implement charge(double amount) // TODO: Implement charge(double amount)
// Validate negative // Validate negative
// Cap at 100 // Cap at 100
@Override
public void charge(double amount){
if (amount < 0){
System.out.println("Error: Cannot charge with negative amount.");
return;
}
chargeLevel += amount;
if (chargeLevel > 100){
chargeLevel = 100;
}
System.out.println("Charged. Current charge level: " + chargeLevel + "%");
}
@Override
public boolean needsService(Vehicle v){
return v.getMileage() > 8000;
}
@Override
public void performService(){
System.out.println("Battery system checked");
chargeLevel = 100;
}
} }
+25
View File
@@ -28,12 +28,37 @@ public class GasCar extends Car implements Refuelable {
} }
// TODO: Override getEnergyType() → return fuelType + " (Gas)" // TODO: Override getEnergyType() → return fuelType + " (Gas)"
@Override
public String getEnergyType(){
return fuelType + " (Gas)";
}
// TODO: Implement refuel(double amount) // TODO: Implement refuel(double amount)
// Validate amount // Validate amount
// Cap at 100 // Cap at 100
@Override
public void refuel(double amount){
if (amount < 0){
System.out.println("Error: Cannot refuel with negative amount.");
return;
}
fuelLevel += amount;
if (fuelLevel > 100){
fuelLevel = 100;
}
System.out.println("Refueled. Current fuel level: " + fuelLevel + "%");
}
// TODO: Override showDetails() // TODO: Override showDetails()
// Call super.showDetails() // Call super.showDetails()
// Print fuel level and fuel type // Print fuel level and fuel type
@Override
public void showDetails(){
super.showDetails();
System.out.println("Fuel Type: " + fuelType + " | Fuel Level: " + fuelLevel + "%");
}
} }
+15
View File
@@ -27,4 +27,19 @@ public class SportsCar extends GasCar {
// TODO: Override showDetails() // TODO: Override showDetails()
// Call super.showDetails() // Call super.showDetails()
// Print acceleration time // Print acceleration time
@Override
public void showDetails(){
super.showDetails();
System.out.println("0-100 km/h Acceleration: " + zeroToHundred + " seconds");
}
@Override
public boolean needsService(Vehicle v) {
return v.getMileage() > 5000;
}
@Override
public void performService() {
System.out.println("High-performance brake system checked");
}
} }
+20 -1
View File
@@ -21,12 +21,31 @@ public abstract class Vehicle {
} }
// TODO: Create getters for all fields // TODO: Create getters for all fields
public String getBrand(){
return brand;
}
public String getModel(){
return model;
}
public int getYear(){
return year;
}
public double getMileage(){
return mileage;
}
// TODO: Implement addMileage(double km) // TODO: Implement addMileage(double km)
// If km is negative → print error // If km is negative → print error
// Otherwise increase mileage // Otherwise increase mileage
public void addMileage(double km){
if (km < 0) {
System.out.println("Error: mileage cannot be negative.");
return;
}
this.mileage += km;
}
// TODO: Declare abstract method getEnergyType() // TODO: Declare abstract method getEnergyType()
public abstract String getEnergyType();
public void basicInfo() { public void basicInfo() {
System.out.println( System.out.println(
@@ -18,5 +18,14 @@ public class VehicleInspector {
// If ElectricCar → print "Check battery system" // If ElectricCar → print "Check battery system"
// If SportsCar → print "Check brakes and performance" // If SportsCar → print "Check brakes and performance"
// If GasCar → print "Check engine and oil" // If GasCar → print "Check engine and oil"
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");
}
} }
} }