1 Commits
Author SHA1 Message Date
Arshida_0 18e4f486ae Implement WS-03 2026-04-27 22:04:23 +04:30
9 changed files with 224 additions and 31 deletions
+1 -1
View File
@@ -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>
+36 -11
View File
@@ -1,10 +1,17 @@
package org; package org;
import org.behavior.Serviceable;
import org.model.Vehicle;
import org.util.VehicleInspector;
import org.model.GasCar;
import org.model.ElectricCar;
import org.model.SportsCar;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// ============================================================ // ============================================================
// TODO 1: // TODO 1: .
// Create one instance of each vehicle type: // Create one instance of each vehicle type:
// - ElectricCar // - ElectricCar
// - GasCar // - GasCar
@@ -16,13 +23,17 @@ 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", 2022, 1000, 100, 550, 50);
GasCar toyota = new GasCar("Toyota", "Corolla", 2018, 85000, 5, 50, "Petrol");
SportsCar ferrari = new SportsCar("Ferrari", "F8 Tributo", 2021, 12000, 2, 3.9, "oil", 710);
// ============================================================ // ============================================================
// TODO 2: // TODO 2: .
// Create an array of Vehicle: // Create an array of Vehicle:
// Vehicle[] vehicles = { ... }; // Vehicle[] vehicles = { ... };
// //
@@ -31,13 +42,13 @@ 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, ferrari, toyota};
// ============================================================ // ============================================================
// TODO 3: // TODO 3: .
// Loop through the Vehicle[] array and: // Loop through the Vehicle[] array and:
// • Call v.basicInfo(); // • Call v.basicInfo();
// • Print a blank line after each. // • Print a blank line after each.
@@ -46,13 +57,16 @@ 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();
}
// ============================================================ // ============================================================
// TODO 4: // TODO 4: .
// Use VehicleInspector to inspect each vehicle: // Use VehicleInspector to inspect each vehicle:
// //
// VehicleInspector.inspect(v); // VehicleInspector.inspect(v);
@@ -61,13 +75,15 @@ 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);
}
// ============================================================ // ============================================================
// TODO 5: // TODO 5: .
// Create an array of Serviceable: // Create an array of Serviceable:
// Serviceable[] serviceables = { ... }; // Serviceable[] serviceables = { ... };
// //
@@ -79,6 +95,7 @@ public class Main {
// Vehicle v = (Vehicle) s; // Vehicle v = (Vehicle) s;
// //
// ============================================================ // ============================================================
Serviceable[] serviceables = {tesla, ferrari, toyota};
@@ -105,19 +122,27 @@ 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("Brand: " + v.getBrand() + " Model: " + v.getModel());
s.performService();
}
else System.out.println("The car does not need service");
System.out.println();
}
// ============================================================ // ============================================================
// TODO 7 (Optional): // TODO 7 (Optional): .
// After everything is done, print a final message: // After everything is done, print a final message:
// //
// System.out.println("=== Workshop Completed ==="); // System.out.println("=== Workshop Completed ===");
// //
// ============================================================ // ============================================================
System.out.println("=== Workshop Completed ===");
+28 -6
View File
@@ -18,24 +18,46 @@ 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 // 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" .
// TODO: Override showDetails() @Override
public String getEnergyType() {
return "Unknown";
}
// TODO: Override showDetails() .
// Print brand, model, year, mileage, and number of seats // Print brand, model, year, mileage, and number of seats
@Override
public void showDetails() {
System.out.println("Brand: " + getBrand() + " Model: " + getModel() + " Year: " + getYear() + " Mileage: " + getMileage() + "Number of seats: " + seats);
}
/* /*
* Implement Serviceable methods: * Implement Serviceable methods: .
* *
* needsService(Vehicle v): * needsService(Vehicle v): .
* - Check the mileage of the current car * - Check the mileage of the current car
* - If mileage > 10000 return true * - If mileage > 10000 return true
* - Otherwise return false * - Otherwise return false
* *
* performService(): * performService(): .
* - Print: "General car service completed" * - Print: "General car service completed"
*/ */
@Override
public boolean needsService(Vehicle v) {
if(v.getMileage() > 10000) return true;
else return false;
}
@Override
public void performService() {
System.out.println("General car service completed");
}
} }
+28 -3
View File
@@ -27,13 +27,38 @@ public class ElectricCar extends Car implements Chargeable {
this.chargeLevel = chargeLevel; this.chargeLevel = chargeLevel;
} }
// TODO: Override getEnergyType() → return "Electric" // TODO: Override getEnergyType() → return "Electric" .
@Override
// TODO: Override showDetails() public String getEnergyType() {
return "Electric";
}
// 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("Capacity: " + batteryCapacity + "Charge level: " + chargeLevel);
}
// TODO: Implement charge(double amount) // TODO: Implement charge(double amount)
// Validate negative // Validate negative
// Cap at 100 // Cap at 100
public void charge(double amount) {
if (amount < 0) System.out.println("invalid amount");
else chargeLevel += amount;
if (chargeLevel > 100) chargeLevel = 100;
}
@Override
public boolean needsService(Vehicle v) {
if(v.getMileage() > 8000) return true;
else return false;
}
@Override
public void performService() {
System.out.println("Battery system checked");
chargeLevel = 100;
}
} }
+30 -3
View File
@@ -27,13 +27,40 @@ public class GasCar extends Car implements Refuelable {
this.fuelType = fuelType; this.fuelType = fuelType;
} }
// TODO: Override getEnergyType() → return fuelType + " (Gas)" // TODO: Override getEnergyType() → return fuelType + " (Gas)" .
// TODO: Implement refuel(double amount) @Override
public String getEnergyType() {
return (fuelType + " (Gas)");
}
// TODO: Implement refuel(double amount) .
// Validate amount // Validate amount
// Cap at 100 // Cap at 100
public void refuel(double amount) {
if (amount < 0) System.out.println("invalid amount");
else fuelLevel += amount;
// TODO: Override showDetails() if (fuelLevel > 100) fuelLevel = 100;
}
// 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 level: " + fuelLevel + " Fuel type: " + fuelType);
}
@Override
public boolean needsService(Vehicle v) {
if(v.getMileage() > 12000) return true;
else return false;
}
@Override
public void performService() {
System.out.println("Engine oil changed");
fuelLevel = 100;
}
} }
+52
View File
@@ -0,0 +1,52 @@
package org.model;
import org.behavior.Chargeable;
import org.behavior.Refuelable;
public class HybridCar extends Car implements Chargeable, Refuelable {
private double fuelLevel;
private double chargeLevel;
public HybridCar(String brand, String model, int year,
double mileage, int seats,
double fuelLevel, double chargeLevel) {
super(brand, model, year, mileage, seats);
this.chargeLevel = chargeLevel;
this.fuelLevel = fuelLevel;
}
public void refuel(double amount) {
if (amount < 0) System.out.println("invalid amount");
else fuelLevel += amount;
if (fuelLevel > 100) fuelLevel = 100;
}
public void charge(double amount) {
if (amount < 0) System.out.println("invalid amount");
else chargeLevel += amount;
if (chargeLevel > 100) chargeLevel = 100;
}
@Override
public void showDetails() {
super.showDetails();
System.out.println("Fuel level: " + fuelLevel + "Charge level: " + chargeLevel);
}
@Override
public String getEnergyType() {
return "Hybrid";
}
@Override
public boolean needsService(Vehicle v) {
if(v.getMileage() > 10000) return true;
else return false;
}
@Override
public void performService() {
System.out.println("Engine oil changed and battery system checked");
fuelLevel = 100;
chargeLevel = 100;
}
}
+17 -1
View File
@@ -24,7 +24,23 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred; this.zeroToHundred = zeroToHundred;
} }
// 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("Acceleration time: " + zeroToHundred);
}
@Override
public boolean needsService(Vehicle v) {
if(v.getMileage() > 5000) return true;
else return false;
}
@Override
public void performService() {
System.out.println("High-performance brake system checked");
}
} }
+24 -5
View File
@@ -20,14 +20,33 @@ public abstract class Vehicle {
this.mileage = mileage; this.mileage = mileage;
} }
// TODO: Create getters for all fields // TODO: Create getters for all fields .
// TODO: Implement addMileage(double km) 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) .
// If km is negative → print error // If km is negative → print error
// Otherwise increase mileage // Otherwise increase mileage
public void addMileage(double km) {
// TODO: Declare abstract method getEnergyType() if (km < 0) System.out.println("invalid number");
else mileage += km;
}
// TODO: Declare abstract method getEnergyType() .
public abstract String getEnergyType();
public void basicInfo() { public void basicInfo() {
System.out.println( System.out.println(
year + " " + brand + " " + model + year + " " + brand + " " + model +
+8 -1
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;
/* /*
@@ -14,9 +17,13 @@ public class VehicleInspector {
v.basicInfo(); v.basicInfo();
// TODO: // TODO: .
// 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 GasCar) System.out.println("Check engine and oil");
else if (v instanceof ElectricCar) System.out.println("Check battery system");
else if (v instanceof SportsCar) System.out.println("Check brakes and performance");
System.out.println();
} }
} }