4 Commits
Author SHA1 Message Date
Meraj 69509050a7 Merge pull request 'develop' (#1) from develop into master
Great job completing the workshop!(100/120)
2026-05-19 16:16:25 +00:00
Matin 2547e8eef1 matin mortazavi / I debugged the code. The issue was that it couldn’t recognize the file paths 2026-04-28 10:07:50 +04:30
Matin d78e46b957 another + bonus 2026-04-27 14:02:52 +04:30
Matin 78137e3270 ElectricCar + car + Vehicle 2026-04-27 12:00:55 +04:30
13 changed files with 349 additions and 58 deletions
+4 -11
View File
@@ -41,9 +41,9 @@ The project consists of the following classes and interfaces:
| GasCar | | ElectricCar |
| (Class) | | (Class) |
+-------------------+ +-------------------+
^ | |
^ ^ ^
| extends | implements | implements
|
| | |
+-------------------+ +-------------------+ +-------------------+
| SportsCar | | Refuelable | | Chargeable |
| (Class) | | (Interface) | | (Interface) |
@@ -118,14 +118,7 @@ Add a `HybridCar` class
- Create a `HybridCar` class that extends `Car`.
- It will need variables for both battery level and fuel level
- Implement `Refuelable` and `Chargeable` together
- Service logic for HybridCar:
```
needsService(): return true if mileage > 10000
performService():
Print "Engine oil changed and battery system checked"
Reset fuelLevel to 100
Reset chargeLevel to 100
```
- (Optional) Override methods to manage both power sources (driving drains battery first, then gas)
## Workflow
1. Fork the repository to your own Git account
@@ -140,6 +133,6 @@ git checkout -b develop
```
git push origin develop
```
7. Create a Pull Request from `develop` to `main` or `master`
7. Create a Pull Request from `develop` to `main`
# GOOD LUCK! :)
-39
View File
@@ -1,39 +0,0 @@
package org.model;
/*
* GasCar extends Car.
* Inherits Serviceable from Car.
*
* Service logic for GasCar:
* - needsService(): return true if mileage > 12000
* - performService():
* Print "Engine oil changed"
* Reset fuelLevel to 100
*/
import org.behavior.Refuelable;
public class GasCar extends Car implements Refuelable {
private double fuelLevel;
private String fuelType;
public GasCar(String brand, String model, int year,
double mileage, int seats,
double fuelLevel, String fuelType) {
super(brand, model, year, mileage, seats);
this.fuelLevel = fuelLevel;
this.fuelType = fuelType;
}
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
// TODO: Implement refuel(double amount)
// Validate amount
// Cap at 100
// TODO: Override showDetails()
// Call super.showDetails()
// Print fuel level and fuel type
}
@@ -1,4 +1,10 @@
package org;
import org.model.*;
import org.util.VehicleInspector;
import org.model.Vehicle;
import org.util.VehicleInspector;
import org.behavior.Serviceable;
public class Main {
public static void main(String[] args) {
@@ -84,6 +90,10 @@ public class Main {
// ============================================================
// TODO 6:
// Loop through the Serviceable[] array:
@@ -118,9 +128,43 @@ public class Main {
//
// ============================================================
Serviceable tesla = new ElectricCar("Tesla", "Model 3", 2023, 5000, 5, 75, 80);
Serviceable honda = new GasCar("Honda", "Civic", 2022, 15000, 5, 50, "Gasoline");
Serviceable porsche = new SportsCar("Porsche", "911", 2023, 2000, 2, 60, "Gasoline", 3.5);
Vehicle[] vehicles = {(Vehicle) tesla, (Vehicle) honda, (Vehicle) porsche};
for (Vehicle v : vehicles) {
v.basicInfo();
System.out.println();
}
for (Vehicle v : vehicles) {
VehicleInspector.inspect(v);
}
Serviceable[] serviceables = { tesla, honda, porsche };
for (Serviceable s : serviceables) {
Vehicle v = (Vehicle) s;
if (s.needsService(v)) {
System.out.println(v.GetBrand() + " " + v.GetBrand() + " needs service.");
s.performService();
} else {
System.out.println(v.GetBrand() + " " + v.GetModel() + " does not need service.");
}
System.out.println();
}
System.out.println("=== Workshop Completed ===");
}
}
@@ -11,7 +11,7 @@ package org.model;
import org.behavior.Serviceable;
public abstract class Car extends Vehicle implements Serviceable {
public abstract class Car extends org.model.Vehicle implements Serviceable {
private int seats;
@@ -19,13 +19,28 @@ 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"
@Override
public String getEnergyType()
{
return "Unknown" ;
}
// TODO: Override showDetails()
// Print brand, model, year, mileage, and number of seats
@Override
public void showDetails()
{
System.out.println("brand : " + GetBrand()) ;
System.out.println("model : " + GetModel() ) ;
System.out.println("year : " + GetYear() ) ;
System.out.println("mileage : " + GetMileage()) ;
System.out.println("seats : " + seats) ;
}
/*
* Implement Serviceable methods:
@@ -38,4 +53,16 @@ public abstract class Car extends Vehicle implements Serviceable {
* performService():
* - Print: "General car service completed"
*/
@Override
public boolean needsService(org.model.Vehicle v)
{
return this.GetMileage() > 10000 ;
}
public abstract boolean needService(org.model.Vehicle v);
@Override public void performService(){
System.out.println("General car service completed") ;
}
}
@@ -26,13 +26,49 @@ public class ElectricCar extends Car implements Chargeable {
this.batteryCapacity = batteryCapacity;
this.chargeLevel = chargeLevel;
}
@Override
// TODO: Override getEnergyType() return "Electric"
public String getEnergyType()
{
return "Electric" ;
}
// TODO: Override showDetails()
// Call super.showDetails()
// Print battery capacity and charge level
@Override
public void showDetails()
{
super.showDetails() ;
System.out.println("battry capacity : " + batteryCapacity) ;
System.out.println("charge level : " + chargeLevel) ;
}
@Override
public void charge(double amount)
{
if (amount < 0 )
{
System.out.println("No") ;
}
else
{
chargeLevel = chargeLevel + amount ;
if (chargeLevel > 100)
{
chargeLevel = 100 ;
}
}
}
@Override
public boolean needService(org.model.Vehicle v)
{
return (this.GetMileage()> 8000);
}
@Override
public void performService()
{
System.out.println("Battery system checked") ;
chargeLevel = 100 ;
}
// TODO: Implement charge(double amount)
// Validate negative
// Cap at 100
+81
View File
@@ -0,0 +1,81 @@
package org.model;
/*
* GasCar extends Car.
* Inherits Serviceable from Car.
*
* Service logic for GasCar:
* - needsService(): return true if mileage > 12000
* - performService():
* Print "Engine oil changed"
* Reset fuelLevel to 100
*/
import org.behavior.Refuelable;
public class GasCar extends Car implements Refuelable {
private double fuelLevel;
private String fuelType;
public GasCar(String brand, String model, int year,
double mileage, int seats,
double fuelLevel, String fuelType) {
super(brand, model, year, mileage, seats);
this.fuelLevel = fuelLevel;
this.fuelType = fuelType;
}
@Override
public boolean needsService(Vehicle v)
{
return this.GetMileage()> 12000 ;
}
@Override
public boolean needService(Vehicle v) {
return false;
}
@Override
public void performService()
{
System.out.println("Engine oil changed") ;
fuelLevel = 100 ;
}
@Override
public String getEnergyType()
{
return fuelType + " (Gas)" ;
}
@Override
public void refuel(double amount)
{
if (amount < 0)
{
System.out.println("no ! ") ;
}
else
{
fuelLevel = fuelLevel + amount ;
if (fuelLevel > 100)
fuelLevel = 100 ;
}
}
@Override
public void showDetails()
{
super.showDetails() ;
System.out.println("fuel level : " + fuelLevel) ;
System.out.println("fuel type : " + fuelType) ;
}
// TODO: Override getEnergyType() → return fuelType + " (Gas)"
// TODO: Implement refuel(double amount)
// Validate amount
// Cap at 100
// TODO: Override showDetails()
}
+88
View File
@@ -0,0 +1,88 @@
package org.model;
import org.behavior.Chargeable;
import org.behavior.Refuelable;
import org.model.Vehicle;
public class HybridCar extends Car implements Chargeable , Refuelable {
private int batteryCapacity;
private double chargeLevel;
private double fuelLevel;
private String fuelType;
public HybridCar(String brand, String model, int year,
double mileage, int seats,
double fuelLevel, String fuelType, int batteryCapacity, double chargeLevel) {
super(brand, model, year, mileage, seats);
this.fuelLevel = fuelLevel;
this.fuelType = fuelType;
this.batteryCapacity = batteryCapacity;
this.chargeLevel = chargeLevel;
}
@Override
// TODO: Override getEnergyType() → return "Electric"
public String getEnergyType()
{
return "Electric + fuel : " + fuelType ;
}
@Override
public void showDetails()
{
super.showDetails() ;
System.out.println("battry capacity : " + batteryCapacity) ;
System.out.println("charge level : " + chargeLevel) ;
System.out.println("fuel level : " + fuelLevel) ;
System.out.println("fuel type : " + fuelType) ;
}
@Override
public boolean needService(Vehicle v) {
return false;
}
@Override
public void charge(double amount)
{
if (amount < 0 )
{
System.out.println("No") ;
}
else
{
chargeLevel = chargeLevel + amount ;
if (chargeLevel > 100)
{
chargeLevel = 100 ;
}
}
}
@Override
public void refuel(double amount)
{
if (amount < 0)
{
System.out.println("no ! ") ;
}
else
{
fuelLevel = fuelLevel + amount ;
if (fuelLevel > 100)
fuelLevel = 100 ;
}
}
@Override
public boolean needsService(Vehicle v)
{
return this.GetMileage() > 10000 ;
}
@Override
public void performService()
{
System.out.println("Battery system checked") ;
chargeLevel = 100 ;
System.out.println("Engine oil changed") ;
fuelLevel = 100 ;
}
}
@@ -9,7 +9,7 @@ package org.model;
* Print "High-performance brake system checked"
*/
public class SportsCar extends GasCar {
public class SportsCar extends org.model.GasCar {
private double zeroToHundred;
@@ -23,8 +23,30 @@ public class SportsCar extends GasCar {
this.zeroToHundred = zeroToHundred;
}
@Override
public boolean needsService(org.model.Vehicle v)
{
return this.GetMileage() > 5000 ;
}
@Override
public void performService()
{
System.out.println("High-performance brake system checked") ;
}
@Override
public void showDetails()
{
System.out.println("*details*");
super.showDetails() ;
System.out.println("0 --> 100 " + zeroToHundred) ;
}
// TODO: Override showDetails()
// Print header
// Call super.showDetails()
// Print acceleration time
}
@@ -21,12 +21,39 @@ public abstract class Vehicle {
}
// TODO: Create getters for all fields
public String GetBrand()
{
return this.brand ;
}
public String GetModel()
{
return this.model ;
}
public int GetYear()
{
return this.year ;
}
public double GetMileage()
{
return this.mileage ;
}
// TODO: Implement addMileage(double km)
public void AddMileage(double km)
{
if (km < 0 )
{
System.out.println("error !") ;
}
else
{
this.mileage = this.mileage + km ;
}
}
// If km is negative print error
// Otherwise increase mileage
// TODO: Declare abstract method getEnergyType()
public abstract String getEnergyType();
public void basicInfo() {
System.out.println(
@@ -1,6 +1,6 @@
package org.util;
import org.*;
import org.model.*;
import org.model.Vehicle;
/*
@@ -13,6 +13,18 @@ public class VehicleInspector {
public static void inspect(Vehicle v) {
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 if (v instanceof GasCar)
{
System.out.println("Check engine and oil") ;
}
// TODO:
// If ElectricCar print "Check battery system"