complete project #1
+67
-107
@@ -1,124 +1,84 @@
|
||||
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 S", 2022, 1000,
|
||||
100, 550, 50 // batteryCapacity, range
|
||||
);
|
||||
|
||||
GasCar toyota = new GasCar(
|
||||
"Toyota", "Corolla", 2018, 85000,
|
||||
5, 50, "Petrol" // seats, fuelCapacity, fuelType
|
||||
);
|
||||
|
||||
SportsCar ferrari = new SportsCar(
|
||||
"Ferrari", "F8 Tributo", 2021, 12000,
|
||||
2, 3.9, "oil", 710 // seats, engineSize, horsepower
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
Vehicle[] parking = {tesla , ferrari ,toyota};
|
||||
|
||||
|
||||
|
||||
|
||||
for (Vehicle v : parking){
|
||||
v.basicInfo();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
|
||||
|
||||
for (Vehicle v : parking){
|
||||
VehicleInspector.inspect(v);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Serviceable[] serviceables = {tesla , toyota , ferrari};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// 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).
|
||||
// ============================================================
|
||||
for(Serviceable s : serviceables){
|
||||
|
||||
Vehicle v = (Vehicle) s;
|
||||
|
||||
System.out.println("Brand :" + v.getBrand() + " Model:" + v.getModel());
|
||||
|
||||
|
||||
if(s.needsService(v) ){
|
||||
s.performService();
|
||||
}
|
||||
else {
|
||||
System.out.println("the car does not need service");
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// 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.
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 4:
|
||||
// Use VehicleInspector to inspect each vehicle:
|
||||
//
|
||||
// VehicleInspector.inspect(v);
|
||||
//
|
||||
// Purpose:
|
||||
// • Demonstrate the use of `instanceof`
|
||||
// • Show subclass‑specific inspection messages
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// 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;
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// 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.
|
||||
// ============================================================
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ============================================================
|
||||
// TODO 7 (Optional):
|
||||
// After everything is done, print a final message:
|
||||
//
|
||||
// System.out.println("=== Workshop Completed ===");
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
|
||||
System.out.println("=== Workshop Completed ===");
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ package org.model;
|
||||
* This class SHOULD implement Serviceable.
|
||||
*
|
||||
* Service logic for Car:
|
||||
* - needsService(): return true if mileage > 10,000
|
||||
* - performService(): print "General car service completed"
|
||||
*/
|
||||
|
||||
@@ -18,24 +17,36 @@ public abstract class Car extends Vehicle implements Serviceable {
|
||||
public Car(String brand, String model, int year,
|
||||
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"
|
||||
*/
|
||||
@Override
|
||||
public String getEnergyType(){
|
||||
return "Unknow";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDetails(){
|
||||
basicInfo();
|
||||
System.out.print(" | Seat :" + this.seats);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsService(Vehicle v){
|
||||
if(v.getMileage() > 10000) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performService(){
|
||||
System.out.println("General car service completed");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -27,13 +27,41 @@ public class ElectricCar extends Car implements Chargeable {
|
||||
this.chargeLevel = chargeLevel;
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return "Electric"
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print battery capacity and charge level
|
||||
public String getEnergyType(){
|
||||
return "Electric";
|
||||
}
|
||||
|
||||
// TODO: Implement charge(double amount)
|
||||
// Validate negative
|
||||
// Cap at 100
|
||||
public void showDetails(){
|
||||
super.showDetails();
|
||||
System.out.println(" |Battert capacity:" + this.batteryCapacity + "|charge level:" + this.chargeLevel);
|
||||
}
|
||||
|
||||
public boolean needsService(Vehicle v){
|
||||
if(v.getMileage()> 8000){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void performService(){
|
||||
System.out.println( "Battery system checked");
|
||||
this.chargeLevel=100;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void charge(double amount){
|
||||
if(amount <0){
|
||||
return;
|
||||
}
|
||||
else if (amount>100) {
|
||||
chargeLevel = 100;
|
||||
}
|
||||
else {
|
||||
chargeLevel=amount;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -27,13 +27,38 @@ public class GasCar extends Car implements Refuelable {
|
||||
this.fuelType = fuelType;
|
||||
}
|
||||
|
||||
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
|
||||
public boolean needsService(Vehicle v){
|
||||
if(v.getMileage()> 12000){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Implement refuel(double amount)
|
||||
// Validate amount
|
||||
// Cap at 100
|
||||
|
||||
// TODO: Override showDetails()
|
||||
// Call super.showDetails()
|
||||
// Print fuel level and fuel type
|
||||
|
||||
public void performService(){
|
||||
System.out.println("Engine oil changed");
|
||||
this.fuelLevel =100;
|
||||
}
|
||||
|
||||
|
||||
public String getEnergyType(){
|
||||
return fuelType + " (Gas)";
|
||||
}
|
||||
|
||||
public void refuel(double amount){
|
||||
if (amount > 100){
|
||||
this.fuelLevel = 100;
|
||||
}
|
||||
else {
|
||||
this.fuelLevel = amount;
|
||||
}
|
||||
}
|
||||
|
||||
public void showDetails(){
|
||||
super.showDetails();
|
||||
System.out.println("|fuel level" + this.fuelLevel + "|fuel type" + this.fuelType);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +24,22 @@ public class SportsCar extends GasCar {
|
||||
this.zeroToHundred = zeroToHundred;
|
||||
}
|
||||
|
||||
// TODO: Override showDetails()
|
||||
public boolean needsService(Vehicle v){
|
||||
if(v.getMileage() > 5000) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void performService(){
|
||||
System.out.println( "High-performance brake system checked");
|
||||
}
|
||||
|
||||
|
||||
public void showDetails(){
|
||||
super.showDetails();
|
||||
System.out.println("| acceleration :" + this.zeroToHundred);
|
||||
}
|
||||
// Call super.showDetails()
|
||||
// Print acceleration time
|
||||
}
|
||||
|
||||
@@ -20,13 +20,35 @@ public abstract class Vehicle {
|
||||
this.mileage = mileage;
|
||||
}
|
||||
|
||||
// TODO: Create getters for all fields
|
||||
public String getBrand(){
|
||||
return this.brand;
|
||||
}
|
||||
|
||||
// TODO: Implement addMileage(double km)
|
||||
// If km is negative → print error
|
||||
// Otherwise increase mileage
|
||||
public String getModel(){
|
||||
return this.model;
|
||||
}
|
||||
|
||||
// TODO: Declare abstract method getEnergyType()
|
||||
public int getYear(){
|
||||
return this.year;
|
||||
}
|
||||
|
||||
public double getMileage(){
|
||||
return this.mileage;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void addMileage(double km){
|
||||
if(km < 0){
|
||||
System.out.println("error");
|
||||
}
|
||||
else {
|
||||
this.mileage +=km;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract String getEnergyType();
|
||||
|
||||
public void basicInfo() {
|
||||
System.out.println(
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package org.util;
|
||||
|
||||
import org.*;
|
||||
import org.model.ElectricCar;
|
||||
import org.model.GasCar;
|
||||
import org.model.SportsCar;
|
||||
import org.model.Vehicle;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
/*
|
||||
* Utility class to inspect vehicles.
|
||||
* Demonstrates instanceof and polymorphism.
|
||||
@@ -14,9 +19,14 @@ public class VehicleInspector {
|
||||
|
||||
v.basicInfo();
|
||||
|
||||
// TODO:
|
||||
// If ElectricCar → print "Check battery system"
|
||||
// If SportsCar → print "Check brakes and performance"
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user