Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10b7fa7b53 | ||
|
|
a8ac92063e |
+42
-66
@@ -1,20 +1,29 @@
|
|||||||
package org;
|
package org;
|
||||||
|
|
||||||
|
import org.behavior.Serviceable;
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.GasCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
|
import org.model.Vehicle;
|
||||||
|
import org.util.VehicleInspector;
|
||||||
|
|
||||||
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:
|
ElectricCar tesla = new ElectricCar(
|
||||||
// - ElectricCar
|
"Tesla", "Model 3", 2022, 5000,
|
||||||
// - GasCar
|
5, 100, 80
|
||||||
// - SportsCar
|
);
|
||||||
//
|
GasCar toyota = new GasCar(
|
||||||
// Requirements:
|
"Toyota", "Corolla", 2018, 85000,
|
||||||
// • Use the constructors you created in each class.
|
5, 50, "Petrol"
|
||||||
// • Provide realistic values (brand, model, year, mileage, etc.).
|
);
|
||||||
// • Store them in variables such as:
|
SportsCar ferrari = new SportsCar(
|
||||||
// ElectricCar tesla = new ElectricCar(...);
|
"Ferrari", "F8", 2021, 12000,
|
||||||
|
2, 60, "Petrol", 2.9
|
||||||
|
);
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
@@ -23,13 +32,7 @@ public class Main {
|
|||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 2:
|
// TODO 2:
|
||||||
// Create an array of Vehicle:
|
Vehicle[] vehicles={tesla,toyota,ferrari};
|
||||||
// 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).
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
@@ -38,13 +41,12 @@ public class Main {
|
|||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 3:
|
// TODO 3:
|
||||||
// Loop through the Vehicle[] array and:
|
for (int i = 0; i < vehicles.length; i++) {
|
||||||
// • Call v.basicInfo();
|
Vehicle v = vehicles[i];
|
||||||
// • Print a blank line after each.
|
|
||||||
//
|
v.basicInfo();
|
||||||
// Purpose:
|
System.out.println();
|
||||||
// Demonstrate that all subclasses share the same Vehicle
|
}
|
||||||
// behavior and can call inherited methods.
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
@@ -53,13 +55,9 @@ public class Main {
|
|||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 4:
|
// TODO 4:
|
||||||
// Use VehicleInspector to inspect each vehicle:
|
for (int i = 0; i < vehicles.length; i++) {
|
||||||
//
|
VehicleInspector.inspect(vehicles[i]);
|
||||||
// VehicleInspector.inspect(v);
|
}
|
||||||
//
|
|
||||||
// Purpose:
|
|
||||||
// • Demonstrate the use of `instanceof`
|
|
||||||
// • Show subclass‑specific inspection messages
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
@@ -68,16 +66,7 @@ public class Main {
|
|||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 5:
|
// TODO 5:
|
||||||
// Create an array of Serviceable:
|
Serviceable[] serviceables={tesla,toyota,ferrari};
|
||||||
// Serviceable[] serviceables = { ... };
|
|
||||||
//
|
|
||||||
// This demonstrates **interface‑based polymorphism**.
|
|
||||||
// Every Car subtype implements Serviceable.
|
|
||||||
//
|
|
||||||
// IMPORTANT:
|
|
||||||
// • You must cast to Vehicle when needed:
|
|
||||||
// Vehicle v = (Vehicle) s;
|
|
||||||
//
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
@@ -86,24 +75,17 @@ public class Main {
|
|||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 6:
|
// TODO 6:
|
||||||
// Loop through the Serviceable[] array:
|
for (int i = 0; i < serviceables.length; i++) {
|
||||||
//
|
Serviceable s = serviceables[i];
|
||||||
// Steps inside loop:
|
Vehicle v = (Vehicle) s;
|
||||||
//
|
if (s.needsService(v)) {
|
||||||
// 1. Cast the current object to Vehicle:
|
System.out.println(v.getBrand() + " " + v.getModel() + " needs service");
|
||||||
// Vehicle v = (Vehicle) s;
|
s.performService();
|
||||||
//
|
} else {
|
||||||
// 2. Check:
|
System.out.println(v.getBrand() + " " + v.getModel() + " does NOT need service");
|
||||||
// if (s.needsService(v)) { ... }
|
}
|
||||||
//
|
System.out.println();
|
||||||
// 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.
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
@@ -112,15 +94,9 @@ public class Main {
|
|||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 7 (Optional):
|
// TODO 7 (Optional):
|
||||||
// After everything is done, print a final message:
|
System.out.println("=== Workshop Completed ===");
|
||||||
//
|
|
||||||
// System.out.println("=== Workshop Completed ===");
|
|
||||||
//
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,23 +19,42 @@ public abstract class Car extends Vehicle implements Serviceable {
|
|||||||
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()
|
|
||||||
// Print brand, model, year, mileage, and number of seats
|
|
||||||
|
|
||||||
/*
|
@java.lang.Override
|
||||||
* Implement Serviceable methods:
|
public String getEnergyType() {
|
||||||
*
|
return "Unknown";
|
||||||
* needsService(Vehicle v):
|
}
|
||||||
* - Check the mileage of the current car
|
|
||||||
* - If mileage > 10000 return true
|
|
||||||
* - Otherwise return false
|
// TODO: Override showDetails()
|
||||||
*
|
|
||||||
* performService():
|
@java.lang.Override
|
||||||
* - Print: "General car service completed"
|
public void showDetails() {
|
||||||
*/
|
System.out.println(
|
||||||
|
getYear() + " " +getBrand() +" " +getModel()+" | Mileage: " + getMileage() + " | Seats: " + seats
|
||||||
|
);
|
||||||
|
}
|
||||||
|
//needService
|
||||||
|
|
||||||
|
|
||||||
|
@java.lang.Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if (v.getMileage() > 10000) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//performanceService
|
||||||
|
@java.lang.Override
|
||||||
|
public void performService(){
|
||||||
|
System.out.println("General car service completed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -28,12 +28,49 @@ public class ElectricCar extends Car implements Chargeable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override getEnergyType() → return "Electric"
|
// TODO: Override getEnergyType() → return "Electric"
|
||||||
|
@java.lang.Override
|
||||||
|
public String getEnergyType() {
|
||||||
|
return "Electric";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
// TODO: Override showDetails()
|
||||||
// Call super.showDetails()
|
@java.lang.Override
|
||||||
// Print battery capacity and charge level
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Battery:" + batteryCapacity + "| Charge Level:" + chargeLevel
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement charge(double amount)
|
// TODO: Implement charge(double amount)
|
||||||
// Validate negative
|
|
||||||
// Cap at 100
|
@java.lang.Override
|
||||||
|
public void charge(double amount) {
|
||||||
|
if (amount < 0) {
|
||||||
|
System.out.println("Error:charge amount cannot be negative");
|
||||||
|
} else {
|
||||||
|
chargeLevel += amount;
|
||||||
|
if (chargeLevel > 100) {
|
||||||
|
chargeLevel = 100;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@java.lang.Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if (getMileage() > 8000) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@java.lang.Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Battery system checked");
|
||||||
|
chargeLevel = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -29,11 +29,46 @@ public class GasCar extends Car implements Refuelable {
|
|||||||
|
|
||||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||||
|
|
||||||
|
@java.lang.Override
|
||||||
|
public String getEnergyType() {
|
||||||
|
return fuelType + " (Gas)";
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement refuel(double amount)
|
// TODO: Implement refuel(double amount)
|
||||||
// Validate amount
|
public void refuel(double amount) {
|
||||||
// Cap at 100
|
if (amount < 0) {
|
||||||
|
System.out.println("Error: fuel amount cannot be negative");
|
||||||
|
} else {
|
||||||
|
fuelLevel += amount;
|
||||||
|
|
||||||
|
if (fuelLevel > 100) {
|
||||||
|
fuelLevel = 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
// TODO: Override showDetails()
|
||||||
// Call super.showDetails()
|
|
||||||
// Print fuel level and fuel type
|
@java.lang.Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println(
|
||||||
|
"Fuel Type: " + fuelType + " | Fuel Level: " + fuelLevel
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@java.lang.Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if (v.getMileage()>12000){
|
||||||
|
return true;
|
||||||
|
}else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@java.lang.Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed");
|
||||||
|
fuelLevel=100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,22 @@ public class SportsCar extends GasCar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Override showDetails()
|
// TODO: Override showDetails()
|
||||||
// Call super.showDetails()
|
|
||||||
// Print acceleration time
|
@java.lang.Override
|
||||||
|
public void showDetails() {
|
||||||
|
super.showDetails();
|
||||||
|
System.out.println(
|
||||||
|
"0-100 km/h: " +zeroToHundred+"seconds"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@java.lang.Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("High-performance brake system checked");
|
||||||
|
}
|
||||||
|
|
||||||
|
@java.lang.Override
|
||||||
|
public String getEnergyType() {
|
||||||
|
return super.getEnergyType();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,11 +22,33 @@ 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
|
public void addMileage(double km) {
|
||||||
// Otherwise increase mileage
|
if (km <0){
|
||||||
|
System.out.println("Error:mileage cannot be negative");
|
||||||
|
} else {
|
||||||
|
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(
|
||||||
|
|||||||
Reference in New Issue
Block a user