Skip to content

Solution for Object Oriented 07 #84

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 3 commits into
base: exercises/oo/07
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
9 changes: 5 additions & 4 deletions Exercise.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
public class Exercise {

public static void main(String[] args) {
System.out.println("Anzahl Fahrzeuge: " + Vehicle.getNumberOfVehicles());

Vehicle vehicle = new Vehicle("Porsche", "911");
Vehicle vehicle1 = new Vehicle("Porsche", "911");
Vehicle vehicle2 = new Vehicle("MAN", "TGX");
Vehicle vehicle3 = new Vehicle("Opel", "Zafira Life");

vehicle.accelerate(30);
vehicle.accelerate(30);
vehicle.brake(20);
vehicle.accelerate(40);
System.out.println("Anzahl Fahrzeuge: " + Vehicle.getNumberOfVehicles());
}
}
6 changes: 6 additions & 0 deletions Vehicle.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ public class Vehicle {
private String make;
private String model;
private double speedInKmh;
private static int numberOfVehicles;

public Vehicle(String make, String model) {
this.make = make;
this.model = model;
Vehicle.numberOfVehicles++;
}

public String getMake() {
Expand All @@ -21,6 +23,10 @@ public double getSpeedInKmh() {
return speedInKmh;
}

public static int getNumberOfVehicles() {
return Vehicle.numberOfVehicles;
}

public void accelerate(int valueInKmh) {
speedInKmh += valueInKmh;
System.out.println(toString() + " beschleunigt auf " + speedInKmh + "km/h");
Expand Down