feat: adding vehicle classes and main program
The classes are on Vehicles folder and being used in Main.java file.
This commit is contained in:
90
README.md
Normal file
90
README.md
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
# Java Classes
|
||||||
|
|
||||||
|
This project contains how Java classes can extend from a parent class and how to be implemented in different files.
|
||||||
|
The main class is `Main.java`, where the class Sport, SUV and Hybrid are being used and extend from Vehicle class.
|
||||||
|
|
||||||
|
|
||||||
|
## Diagram
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
classDiagram
|
||||||
|
|
||||||
|
class Vehicle {
|
||||||
|
-String make
|
||||||
|
-String model
|
||||||
|
+int doors
|
||||||
|
-int year
|
||||||
|
+EngineType typeOfEngine
|
||||||
|
+Vehicle(String make, String model, int year)
|
||||||
|
+EngineType getManualEngineType()
|
||||||
|
+EngineType getAutomaticEngineType()
|
||||||
|
+String getMake()
|
||||||
|
+EngineType getTypeOfEngine()
|
||||||
|
+int getDoors()
|
||||||
|
+String getModel()
|
||||||
|
+int getYear()
|
||||||
|
+String forward()
|
||||||
|
+String reverse()
|
||||||
|
+String stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
class EngineType {
|
||||||
|
<<enumeration>>
|
||||||
|
AUTOMATIC
|
||||||
|
MANUAL
|
||||||
|
}
|
||||||
|
|
||||||
|
class SUV {
|
||||||
|
-boolean fourWheelDrive
|
||||||
|
+SUV(String make, String model, int year, boolean fourWheelDrive)
|
||||||
|
+boolean hasFourWheelDrive()
|
||||||
|
+String forward()
|
||||||
|
+String reverse()
|
||||||
|
+String stop()
|
||||||
|
+String engageFourWheelDrive()
|
||||||
|
}
|
||||||
|
|
||||||
|
class Sport {
|
||||||
|
-int topSpeed
|
||||||
|
-GearNumbers gear
|
||||||
|
+Sport(String make, String model, int year, int topSpeed)
|
||||||
|
-GearNumbers setGear(int gear)
|
||||||
|
+double getTopSpeed()
|
||||||
|
+String forward()
|
||||||
|
+String reverse()
|
||||||
|
+String stop()
|
||||||
|
+String drift()
|
||||||
|
+String nitroBoost()
|
||||||
|
+void changeGearUp()
|
||||||
|
+GearNumbers getGear()
|
||||||
|
+void changeGearDown()
|
||||||
|
+void setGearToNeutral()
|
||||||
|
}
|
||||||
|
|
||||||
|
class GearNumbers {
|
||||||
|
<<enumeration>>
|
||||||
|
R
|
||||||
|
N
|
||||||
|
GEAR_1
|
||||||
|
GEAR_2
|
||||||
|
GEAR_3
|
||||||
|
GEAR_4
|
||||||
|
GEAR_5
|
||||||
|
GEAR_6
|
||||||
|
}
|
||||||
|
|
||||||
|
class Hybrid {
|
||||||
|
-double batteryCapacity
|
||||||
|
+Hybrid(String make, String model, int year, double batteryCapacity)
|
||||||
|
+double getBatteryCapacity()
|
||||||
|
+String forward()
|
||||||
|
+String reverse()
|
||||||
|
+String stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
Vehicle <|-- SUV
|
||||||
|
Vehicle <|-- Sport
|
||||||
|
Vehicle <|-- Hybrid
|
||||||
|
Vehicle *-- EngineType
|
||||||
|
Sport *-- GearNumbers
|
||||||
|
```
|
||||||
BIN
src/Main.class
Normal file
BIN
src/Main.class
Normal file
Binary file not shown.
79
src/Main.java
Normal file
79
src/Main.java
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
import Vehicles.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int HybridBatteryCapacity = 100;
|
||||||
|
int SportTopSpeed = 205;
|
||||||
|
boolean SUVFourWheelDrive = true;
|
||||||
|
int hybridYear = 2020;
|
||||||
|
int sportYear = 2019;
|
||||||
|
int suvYear = 2021;
|
||||||
|
String hybridMake = "Toyota";
|
||||||
|
String sportMake = "Ferrari";
|
||||||
|
String suvMake = "Jeep";
|
||||||
|
String hybridModel = "Prius";
|
||||||
|
String sportModel = "488 GTB";
|
||||||
|
String suvModel = "Grand Cherokee";
|
||||||
|
|
||||||
|
Hybrid myHybrid = new Hybrid(hybridMake, hybridModel, hybridYear, HybridBatteryCapacity);
|
||||||
|
Sport mySport = new Sport(sportMake, sportModel, sportYear, SportTopSpeed);
|
||||||
|
SUV mySUV = new SUV(suvMake, suvModel, suvYear, SUVFourWheelDrive);
|
||||||
|
Vehicle myVehicle = new Vehicle("Generic", "Model", 2000);
|
||||||
|
|
||||||
|
System.out.println("=== Generic Vehicle ===");
|
||||||
|
System.out.println("Make by: " + myVehicle.getMake());
|
||||||
|
System.out.println("Model: " + myVehicle.getModel());
|
||||||
|
System.out.println("Year: " + myVehicle.getYear());
|
||||||
|
System.out.println("Type of engine: " + myVehicle.getTypeOfEngine());
|
||||||
|
System.out.println(myVehicle.forward());
|
||||||
|
System.out.println(myVehicle.reverse());
|
||||||
|
System.out.println(myVehicle.stop());
|
||||||
|
|
||||||
|
System.out.println("\n=== Hybrid Vehicle ===");
|
||||||
|
System.out.println("Make by: " + myHybrid.getMake());
|
||||||
|
System.out.println("Model: " + myHybrid.getModel());
|
||||||
|
System.out.println("Year: " + myHybrid.getYear());
|
||||||
|
System.out.println(myHybrid.forward());
|
||||||
|
System.out.println(myHybrid.reverse());
|
||||||
|
System.out.println(myHybrid.stop());
|
||||||
|
System.out.println("Battery remaining: " + myHybrid.getBatteryCapacity());
|
||||||
|
|
||||||
|
System.out.println("\n=== Sport Vehicle ===");
|
||||||
|
System.out.println("Make by: " + mySport.getMake());
|
||||||
|
System.out.println("Model: " + mySport.getModel());
|
||||||
|
System.out.println("Year: " + mySport.getYear());
|
||||||
|
System.out.println(mySport.forward());
|
||||||
|
System.out.println(mySport.reverse());
|
||||||
|
System.out.println(mySport.stop());
|
||||||
|
System.out.println(mySport.drift());
|
||||||
|
System.out.println("Speed: " + mySport.getTopSpeed());
|
||||||
|
System.out.println(mySport.forward());
|
||||||
|
System.out.println("Gear on: " + mySport.getGear());
|
||||||
|
System.out.println("Speed: " + mySport.getTopSpeed());
|
||||||
|
mySport.changeGearUp();
|
||||||
|
System.out.println("Gear on: " + mySport.getGear());
|
||||||
|
System.out.println("Speed: " + mySport.getTopSpeed());
|
||||||
|
mySport.changeGearUp();
|
||||||
|
System.out.println("Gear on: " + mySport.getGear());
|
||||||
|
System.out.println("Speed: " + mySport.getTopSpeed());
|
||||||
|
System.out.println(mySport.drift());
|
||||||
|
mySport.changeGearDown();
|
||||||
|
System.out.println("Gear on: " + mySport.getGear());
|
||||||
|
System.out.println("Speed: " + mySport.getTopSpeed());
|
||||||
|
System.out.println(mySport.nitroBoost());
|
||||||
|
System.out.println("Speed: " + mySport.getTopSpeed());
|
||||||
|
mySport.changeGearDown();
|
||||||
|
System.out.println("Gear on: " + mySport.getGear());
|
||||||
|
System.out.println("Speed: " + mySport.getTopSpeed());
|
||||||
|
mySport.setGearToNeutral();
|
||||||
|
System.out.println("Gear on: " + mySport.getGear());
|
||||||
|
|
||||||
|
System.out.println("\n=== SUV Vehicle ===");
|
||||||
|
System.out.println("Make by: " + mySUV.getMake());
|
||||||
|
System.out.println("Model: " + mySUV.getModel());
|
||||||
|
System.out.println("Year: " + mySUV.getYear());
|
||||||
|
System.out.println(mySUV.forward());
|
||||||
|
System.out.println(mySUV.reverse());
|
||||||
|
System.out.println("4X4?: " + mySUV.hasFourWheelDrive());
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/Vehicles/Hybrid.class
Normal file
BIN
src/Vehicles/Hybrid.class
Normal file
Binary file not shown.
29
src/Vehicles/Hybrid.java
Normal file
29
src/Vehicles/Hybrid.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package Vehicles;
|
||||||
|
|
||||||
|
public class Hybrid extends Vehicle {
|
||||||
|
private double batteryCapacity; // in kWh
|
||||||
|
|
||||||
|
public Hybrid(String make, String model, int year, double batteryCapacity) {
|
||||||
|
super(make, model, year);
|
||||||
|
this.batteryCapacity = batteryCapacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getBatteryCapacity() {
|
||||||
|
return batteryCapacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String forward() {
|
||||||
|
return "The hybrid vehicle is moving forward using both engine and electric power.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String reverse() {
|
||||||
|
return "The hybrid vehicle is moving reverse using electric power.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String stop() {
|
||||||
|
return "The hybrid vehicle is stopping smoothly.";
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/Vehicles/SUV.class
Normal file
BIN
src/Vehicles/SUV.class
Normal file
Binary file not shown.
37
src/Vehicles/SUV.java
Normal file
37
src/Vehicles/SUV.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package Vehicles;
|
||||||
|
|
||||||
|
public class SUV extends Vehicle {
|
||||||
|
private boolean fourWheelDrive;
|
||||||
|
|
||||||
|
public SUV(String make, String model, int year, boolean fourWheelDrive) {
|
||||||
|
super(make, model, year);
|
||||||
|
this.fourWheelDrive = fourWheelDrive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasFourWheelDrive() {
|
||||||
|
return fourWheelDrive;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String forward() {
|
||||||
|
return "The SUV is moving forward steadily.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String reverse() {
|
||||||
|
return "The SUV is reversing carefully.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String stop() {
|
||||||
|
return "The SUV has come to a stop.";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String engageFourWheelDrive() {
|
||||||
|
if (fourWheelDrive) {
|
||||||
|
return "Four-wheel drive engaged.";
|
||||||
|
} else {
|
||||||
|
return "This SUV does not have four-wheel drive.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/Vehicles/Sport$GearNumbers.class
Normal file
BIN
src/Vehicles/Sport$GearNumbers.class
Normal file
Binary file not shown.
BIN
src/Vehicles/Sport.class
Normal file
BIN
src/Vehicles/Sport.class
Normal file
Binary file not shown.
174
src/Vehicles/Sport.java
Normal file
174
src/Vehicles/Sport.java
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
package Vehicles;
|
||||||
|
|
||||||
|
public class Sport extends Vehicle {
|
||||||
|
private int topSpeed;
|
||||||
|
|
||||||
|
public enum GearNumbers {
|
||||||
|
R,
|
||||||
|
N,
|
||||||
|
GEAR_1,
|
||||||
|
GEAR_2,
|
||||||
|
GEAR_3,
|
||||||
|
GEAR_4,
|
||||||
|
GEAR_5,
|
||||||
|
GEAR_6
|
||||||
|
}
|
||||||
|
|
||||||
|
private GearNumbers gear;
|
||||||
|
|
||||||
|
public Sport(String make, String model, int year, int topSpeed) {
|
||||||
|
super(make, model, year);
|
||||||
|
this.topSpeed = topSpeed;
|
||||||
|
this.typeOfEngine = getManualEngineType();
|
||||||
|
this.doors = 2;
|
||||||
|
this.gear = this.setGear(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private GearNumbers setGear(int gear) {
|
||||||
|
switch (gear) {
|
||||||
|
case -1:
|
||||||
|
return GearNumbers.R;
|
||||||
|
case 0:
|
||||||
|
return GearNumbers.N;
|
||||||
|
case 1:
|
||||||
|
return GearNumbers.GEAR_1;
|
||||||
|
case 2:
|
||||||
|
return GearNumbers.GEAR_2;
|
||||||
|
case 3:
|
||||||
|
return GearNumbers.GEAR_3;
|
||||||
|
case 4:
|
||||||
|
return GearNumbers.GEAR_4;
|
||||||
|
case 5:
|
||||||
|
return GearNumbers.GEAR_5;
|
||||||
|
case 6:
|
||||||
|
return GearNumbers.GEAR_6;
|
||||||
|
default:
|
||||||
|
return GearNumbers.N;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getTopSpeed() {
|
||||||
|
if (this.gear == GearNumbers.N || this.gear == GearNumbers.R) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
switch (this.gear) {
|
||||||
|
case GEAR_1:
|
||||||
|
return topSpeed * 0.2;
|
||||||
|
case GEAR_2:
|
||||||
|
return topSpeed * 0.35;
|
||||||
|
case GEAR_3:
|
||||||
|
return topSpeed * 0.5;
|
||||||
|
case GEAR_4:
|
||||||
|
return topSpeed * 0.65;
|
||||||
|
case GEAR_5:
|
||||||
|
return topSpeed * 0.8;
|
||||||
|
case GEAR_6:
|
||||||
|
return topSpeed;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String forward() {
|
||||||
|
if (this.gear == GearNumbers.N) {
|
||||||
|
this.changeGearUp();
|
||||||
|
} else if (this.gear == GearNumbers.R) {
|
||||||
|
this.gear = this.setGear(1);
|
||||||
|
}
|
||||||
|
return "The sport vehicle is zooming forward at high speed!";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String reverse() {
|
||||||
|
if (this.gear != GearNumbers.R) {
|
||||||
|
this.gear = this.setGear(-1);
|
||||||
|
}
|
||||||
|
return "The sport vehicle is reversing quickly!";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String stop() {
|
||||||
|
if (this.gear != GearNumbers.N) {
|
||||||
|
this.setGearToNeutral();
|
||||||
|
}
|
||||||
|
return "The sport vehicle screeches to a halt!";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String drift() {
|
||||||
|
if (this.gear == GearNumbers.N || this.gear == GearNumbers.R) {
|
||||||
|
return "Cannot drift while in Neutral or Reverse gear!";
|
||||||
|
}
|
||||||
|
return "The sport vehicle is drifting around the corner!";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String nitroBoost() {
|
||||||
|
if (this.gear == GearNumbers.N || this.gear == GearNumbers.R) {
|
||||||
|
return "Cannot activate nitro boost while in Neutral or Reverse gear!";
|
||||||
|
}
|
||||||
|
if (this.gear != GearNumbers.GEAR_6) {
|
||||||
|
this.gear = this.setGear(6);
|
||||||
|
}
|
||||||
|
return "The sport vehicle activates nitro boost for extra speed!";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changeGearUp() {
|
||||||
|
switch (this.gear) {
|
||||||
|
case R:
|
||||||
|
this.gear = this.setGear(0);
|
||||||
|
break;
|
||||||
|
case N:
|
||||||
|
this.gear = this.setGear(1);
|
||||||
|
break;
|
||||||
|
case GEAR_1:
|
||||||
|
this.gear = this.setGear(2);
|
||||||
|
break;
|
||||||
|
case GEAR_2:
|
||||||
|
this.gear = this.setGear(3);
|
||||||
|
break;
|
||||||
|
case GEAR_3:
|
||||||
|
this.gear = this.setGear(4);
|
||||||
|
break;
|
||||||
|
case GEAR_4:
|
||||||
|
this.gear = this.setGear(5);
|
||||||
|
break;
|
||||||
|
case GEAR_5:
|
||||||
|
this.gear = this.setGear(6);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public GearNumbers getGear() {
|
||||||
|
return this.gear;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changeGearDown() {
|
||||||
|
switch (this.gear) {
|
||||||
|
case GEAR_6:
|
||||||
|
this.gear = this.setGear(5);
|
||||||
|
break;
|
||||||
|
case GEAR_5:
|
||||||
|
this.gear = this.setGear(4);
|
||||||
|
break;
|
||||||
|
case GEAR_4:
|
||||||
|
this.gear = this.setGear(3);
|
||||||
|
break;
|
||||||
|
case GEAR_3:
|
||||||
|
this.gear = this.setGear(2);
|
||||||
|
break;
|
||||||
|
case GEAR_2:
|
||||||
|
this.gear = this.setGear(1);
|
||||||
|
break;
|
||||||
|
case GEAR_1:
|
||||||
|
this.gear = this.setGear(1);
|
||||||
|
break;
|
||||||
|
case N:
|
||||||
|
this.gear = this.setGear(-1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGearToNeutral() {
|
||||||
|
this.gear = GearNumbers.N;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/Vehicles/Vehicle$EngineType.class
Normal file
BIN
src/Vehicles/Vehicle$EngineType.class
Normal file
Binary file not shown.
BIN
src/Vehicles/Vehicle.class
Normal file
BIN
src/Vehicles/Vehicle.class
Normal file
Binary file not shown.
63
src/Vehicles/Vehicle.java
Normal file
63
src/Vehicles/Vehicle.java
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package Vehicles;
|
||||||
|
|
||||||
|
public class Vehicle {
|
||||||
|
private String make;
|
||||||
|
private String model;
|
||||||
|
public int doors;
|
||||||
|
private int year;
|
||||||
|
|
||||||
|
public enum EngineType {
|
||||||
|
AUTOMATIC,
|
||||||
|
MANUAL
|
||||||
|
}
|
||||||
|
|
||||||
|
public EngineType typeOfEngine;
|
||||||
|
|
||||||
|
public Vehicle(String make, String model, int year) {
|
||||||
|
this.make = make;
|
||||||
|
this.model = model;
|
||||||
|
this.year = year;
|
||||||
|
this.typeOfEngine = EngineType.AUTOMATIC;
|
||||||
|
this.doors = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EngineType getManualEngineType() {
|
||||||
|
return EngineType.MANUAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EngineType getAutomaticEngineType() {
|
||||||
|
return EngineType.AUTOMATIC;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMake() {
|
||||||
|
return make;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EngineType getTypeOfEngine() {
|
||||||
|
return typeOfEngine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDoors() {
|
||||||
|
return doors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getYear() {
|
||||||
|
return year;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String forward() {
|
||||||
|
return "The vehicle is moving forward.";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String reverse() {
|
||||||
|
return "The vehicle is moving reverse.";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String stop() {
|
||||||
|
return "The vehicle has stopped.";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user