Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10b7fa7b53 | ||
|
|
a8ac92063e |
+42
-66
@@ -1,20 +1,29 @@
|
||||
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 static void main(String[] args) {
|
||||
|
||||
// ============================================================
|
||||
// TODO 1:
|
||||
// Create one instance of each vehicle type:
|
||||
// - ElectricCar
|
||||
// - GasCar
|
||||
// - SportsCar
|
||||
//
|
||||
// Requirements:
|
||||
// • Use the constructors you created in each class.
|
||||
// • Provide realistic values (brand, model, year, mileage, etc.).
|
||||
// • Store them in variables such as:
|
||||
// ElectricCar tesla = new ElectricCar(...);
|
||||
ElectricCar tesla = new ElectricCar(
|
||||
"Tesla", "Model 3", 2022, 5000,
|
||||
5, 100, 80
|
||||
);
|
||||
GasCar toyota = new GasCar(
|
||||
"Toyota", "Corolla", 2018, 85000,
|
||||
5, 50, "Petrol"
|
||||
);
|
||||
SportsCar ferrari = new SportsCar(
|
||||
"Ferrari", "F8", 2021, 12000,
|
||||
2, 60, "Petrol", 2.9
|
||||
);
|
||||
// ============================================================
|
||||
|
||||
|
||||
@@ -23,13 +32,7 @@ public class Main {
|
||||
|
||||
// ============================================================
|
||||
// TODO 2:
|
||||
// Create an array of Vehicle:
|
||||
// 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).
|
||||
Vehicle[] vehicles={tesla,toyota,ferrari};
|
||||
// ============================================================
|
||||
|
||||
|
||||
@@ -38,13 +41,12 @@ public class Main {
|
||||
|
||||
// ============================================================
|
||||
// TODO 3:
|
||||
// Loop through the Vehicle[] array and:
|
||||
// • Call v.basicInfo();
|
||||
// • Print a blank line after each.
|
||||
//
|
||||
// Purpose:
|
||||
// Demonstrate that all subclasses share the same Vehicle
|
||||
// behavior and can call inherited methods.
|
||||
for (int i = 0; i < vehicles.length; i++) {
|
||||
Vehicle v = vehicles[i];
|
||||
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
// ============================================================
|
||||
|
||||
|
||||
@@ -53,13 +55,9 @@ public class Main {
|
||||
|
||||
// ============================================================
|
||||
// TODO 4:
|
||||
// Use VehicleInspector to inspect each vehicle:
|
||||
//
|
||||
// VehicleInspector.inspect(v);
|
||||
//
|
||||
// Purpose:
|
||||
// • Demonstrate the use of `instanceof`
|
||||
// • Show subclass‑specific inspection messages
|
||||
for (int i = 0; i < vehicles.length; i++) {
|
||||
VehicleInspector.inspect(vehicles[i]);
|
||||
}
|
||||
// ============================================================
|
||||
|
||||
|
||||
@@ -68,16 +66,7 @@ public class Main {
|
||||
|
||||
// ============================================================
|
||||
// TODO 5:
|
||||
// Create an array of Serviceable:
|
||||
// Serviceable[] serviceables = { ... };
|
||||
//
|
||||
// This demonstrates **interface‑based polymorphism**.
|
||||
// Every Car subtype implements Serviceable.
|
||||
//
|
||||
// IMPORTANT:
|
||||
// • You must cast to Vehicle when needed:
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
Serviceable[] serviceables={tesla,toyota,ferrari};
|
||||
// ============================================================
|
||||
|
||||
|
||||
@@ -86,24 +75,17 @@ public class Main {
|
||||
|
||||
// ============================================================
|
||||
// TODO 6:
|
||||
// Loop through the Serviceable[] array:
|
||||
//
|
||||
// Steps inside loop:
|
||||
//
|
||||
// 1. Cast the current object to Vehicle:
|
||||
// Vehicle v = (Vehicle) s;
|
||||
//
|
||||
// 2. Check:
|
||||
// if (s.needsService(v)) { ... }
|
||||
//
|
||||
// 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.
|
||||
for (int i = 0; i < serviceables.length; i++) {
|
||||
Serviceable s = serviceables[i];
|
||||
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();
|
||||
}
|
||||
// ============================================================
|
||||
|
||||
|
||||
@@ -112,15 +94,9 @@ public class Main {
|
||||
|
||||
// ============================================================
|
||||
// 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) {
|
||||
|
||||
// TODO: Call super constructor
|
||||
super(brand,model,year,mileage);
|
||||
this.seats = seats;
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return "Unknown"
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Print brand, model, year, mileage, and number of 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"
|
||||
*/
|
||||
@java.lang.Override
|
||||
public String getEnergyType() {
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
|
||||
// TODO: Override showDetails()
|
||||
|
||||
@java.lang.Override
|
||||
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"
|
||||
@java.lang.Override
|
||||
public String getEnergyType() {
|
||||
return "Electric";
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print battery capacity and charge level
|
||||
@java.lang.Override
|
||||
public void showDetails() {
|
||||
super.showDetails();
|
||||
|
||||
System.out.println(
|
||||
"Battery:" + batteryCapacity + "| Charge Level:" + chargeLevel
|
||||
);
|
||||
}
|
||||
|
||||
// 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)"
|
||||
|
||||
@java.lang.Override
|
||||
public String getEnergyType() {
|
||||
return fuelType + " (Gas)";
|
||||
}
|
||||
|
||||
// TODO: Implement refuel(double amount)
|
||||
// Validate amount
|
||||
// Cap at 100
|
||||
public void refuel(double amount) {
|
||||
if (amount < 0) {
|
||||
System.out.println("Error: fuel amount cannot be negative");
|
||||
} else {
|
||||
fuelLevel += amount;
|
||||
|
||||
if (fuelLevel > 100) {
|
||||
fuelLevel = 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
// 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
|
||||
|
||||
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
|
||||
// Otherwise increase mileage
|
||||
public void addMileage(double km) {
|
||||
if (km <0){
|
||||
System.out.println("Error:mileage cannot be negative");
|
||||
} else {
|
||||
mileage+= km;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
public abstract String getEnergyType();
|
||||
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
|
||||
Reference in New Issue
Block a user