Skip to content

Solution for Polymorphy 01 #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: exercises/polymorphy/01
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class Car extends Vehicle {

private final int seats;

public Car(String make, String model, Engine engine, int seats) {
super(make, model, engine);
this.seats = seats;
}

public int getSeats() {
return seats;
}

public void doATurboBoost() {
speedInKmh *= 2;
System.out.println(
getMake()
+ " "
+ getModel()
+ " macht einen Turboboost und beschleunigt auf "
+ speedInKmh
+ " km/h");
}

public String toString() {
return getMake()
+ " "
+ getModel()
+ " ("
+ getEngine().getDescription()
+ ", "
+ seats
+ " Sitzplaetze)";
}
}
2 changes: 1 addition & 1 deletion Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public enum Engine {
GAS("Gas"),
ELECTRO("Elektro");

private String description;
private final String description;

Engine(String description) {
this.description = description;
Expand Down
15 changes: 12 additions & 3 deletions Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ public class Exercise {
public static void main(String[] args) {
System.out.println("Anzahl Fahrzeuge: " + Vehicle.getNumberOfVehicles());

Vehicle vehicle1 = new Vehicle("Porsche", "911", Engine.ELECTRO);
Vehicle vehicle2 = new Vehicle("MAN", "TGX", Engine.DIESEL);
Vehicle vehicle3 = new Vehicle("Opel", "Zafira Life", Engine.DIESEL);
Car car1 = new Car("Porsche", "911", Engine.ELECTRO, 2);
Truck truck1 = new Truck("MAN", "TGX", Engine.DIESEL, 20);
Car car2 = new Car("Opel", "Zafira Life", Engine.DIESEL, 7);

System.out.println("Anzahl Fahrzeuge: " + Vehicle.getNumberOfVehicles());

System.out.println(car1.toString());
System.out.println(truck1.toString());
System.out.println(car2.toString());

car1.accelerate(50);
truck1.transform();
car1.doATurboBoost();
truck1.transform();
}
}
32 changes: 32 additions & 0 deletions Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Truck extends Vehicle {

private final int cargo;
private boolean isTransformed;

public Truck(String make, String model, Engine engine, int cargo) {
super(make, model, engine);
this.cargo = cargo;
}

public int getCargo() {
return cargo;
}

public boolean isTransformed() {
return isTransformed;
}

public void transform() {
if (isTransformed) {
System.out.println(
getMake() + " " + getModel() + " verwandelt sich in einen Lastwagen zurueck");
} else {
System.out.println(getMake() + " " + getModel() + " verwandelt sich in einen Autobot");
}
isTransformed = !isTransformed;
}

public String toString() {
return getMake() + " " + getModel() + " (" + getEngine().getDescription() + ", " + cargo + "t)";
}
}
2 changes: 1 addition & 1 deletion Vehicle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ public class Vehicle {

private String make;
private String model;
private double speedInKmh;
protected double speedInKmh;
private Engine engine;
private static int numberOfVehicles;

Expand Down