Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e17e4a0f17 |
+30
-26
@@ -1,5 +1,12 @@
|
|||||||
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) {
|
||||||
|
|
||||||
@@ -16,11 +23,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", "unknown", 2026, 0, 4, 100, 34);
|
||||||
|
GasCar toyota= new GasCar("toyota", "corola", 2015, 100, 4, 56, "Oil");
|
||||||
|
SportsCar f1=new SportsCar("f1?", "idk", 2026, 0, 2, 100, "Oil", 1.3);
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 2:
|
// TODO 2:
|
||||||
// Create an array of Vehicle:
|
// Create an array of Vehicle:
|
||||||
@@ -31,7 +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, f1};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -46,11 +51,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("\n");
|
||||||
|
v.getEnergyType();
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 4:
|
// TODO 4:
|
||||||
// Use VehicleInspector to inspect each vehicle:
|
// Use VehicleInspector to inspect each vehicle:
|
||||||
@@ -60,12 +64,9 @@ public class Main {
|
|||||||
// Purpose:
|
// Purpose:
|
||||||
// • Demonstrate the use of `instanceof`
|
// • Demonstrate the use of `instanceof`
|
||||||
// • Show subclass‑specific inspection messages
|
// • Show subclass‑specific inspection messages
|
||||||
// ============================================================
|
// ==========================================================
|
||||||
|
VehicleInspector.inspect(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 5:
|
// TODO 5:
|
||||||
// Create an array of Serviceable:
|
// Create an array of Serviceable:
|
||||||
@@ -79,10 +80,7 @@ public class Main {
|
|||||||
// Vehicle v = (Vehicle) s;
|
// Vehicle v = (Vehicle) s;
|
||||||
//
|
//
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
Serviceable[] serviceables ={toyota, tesla , f1};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 6:
|
// TODO 6:
|
||||||
@@ -105,10 +103,16 @@ 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());
|
||||||
|
s.performService();
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
System.out.println("the car does not need service");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// TODO 7 (Optional):
|
// TODO 7 (Optional):
|
||||||
@@ -118,7 +122,7 @@ public class Main {
|
|||||||
//
|
//
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
System.out.println("=== Workshop Completed ===");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,12 +18,17 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 +43,23 @@ 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(getBrand()+ " " + getModel() + " " + getYear()+ " "+getMileage()+ " "+ seats);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needsService(Vehicle v) {
|
||||||
|
if (v.getMileage()> 1000){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("General car service completed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,11 +29,42 @@ public class ElectricCar extends Car implements Chargeable {
|
|||||||
|
|
||||||
// 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(batteryCapacity+" "+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 (chargeLevel+amount<= batteryCapacity && chargeLevel+amount>=0) {
|
||||||
|
chargeLevel += amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public boolean needsService(ElectricCar ec) {
|
||||||
|
if (ec.getMileage()>8000){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Battery system checked");
|
||||||
|
chargeLevel=100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,11 +29,45 @@ 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 && amount+fuelLevel<100){
|
||||||
|
fuelLevel+=amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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(fuelLevel+" "+fuelType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean needsService(GasCar gs) {
|
||||||
|
if (gs.getMileage()>12000){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("Engine oil changed");
|
||||||
|
fuelLevel=100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,4 +27,24 @@ 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(zeroToHundred);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean needsService(SportsCar sc) {
|
||||||
|
if (sc.getMileage()>5000){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performService() {
|
||||||
|
System.out.println("High-performance brake system checked");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,12 +21,32 @@ 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("km should be positive");
|
||||||
|
}
|
||||||
|
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(
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package org.util;
|
package org.util;
|
||||||
|
|
||||||
import org.*;
|
import org.*;
|
||||||
|
import org.model.ElectricCar;
|
||||||
|
import org.model.SportsCar;
|
||||||
import org.model.Vehicle;
|
import org.model.Vehicle;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -13,7 +15,15 @@ public class VehicleInspector {
|
|||||||
public static void inspect(Vehicle v) {
|
public static void inspect(Vehicle v) {
|
||||||
|
|
||||||
v.basicInfo();
|
v.basicInfo();
|
||||||
|
if (v instanceof ElectricCar){
|
||||||
|
System.out.println("Check battery system");
|
||||||
|
}
|
||||||
|
else if (v instanceof SportsCar){
|
||||||
|
System.out.println("Check brakes and performance");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("Check engine and oil");
|
||||||
|
}
|
||||||
// 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"
|
||||||
|
|||||||
Reference in New Issue
Block a user