Ich möchte gerne eine Klasse Car basteln und in der sollen Speed, Maxximalgeschwindigkeit usw. festgehalten werden.... man sollte hinterher verschiedene autos erstellen können und sie sich gegenseitig überholen lassen können usw. nun wollte ich ausschließen,dass einem Auto mehr Geschwindigkeit zugewiesen werden kann als die Maximalgeschwindigkeit.. aber ich habe das mit der Exception irgendwie nicht ordentlich reinbekommen
Ausgabe ist nämlich:
----------------
Name: Opel Corsa
Farbe: rot
Maximalgeschwindigkeit: 180
aktuelle Geschwindigkeit: 0
----------------
----------------
Name: Fiat Punto
Farbe: gelb
Maximalgeschwindigkeit: 160
aktuelle Geschwindigkeit: 0
----------------
Opel Corsa(0kmh) ist schneller als, Fiat Punto(0kmh).
Fiat Punto(0kmh) ist schneller als, Opel Corsa(0kmh).
Die beiden Wagen sind gleich schnell unterwegs!
Das hält der Motor nicht aus.
Bitte geben Sie eine passende Geschwindigkeit an.
Exception in thread "main" Car$SpeedException
at Car.setSpeed(Car.java:60)
at Start.main(Start.java:13)
Kann mir hier jemand weiterhelfen?
Ausgabe ist nämlich:
----------------
Name: Opel Corsa
Farbe: rot
Maximalgeschwindigkeit: 180
aktuelle Geschwindigkeit: 0
----------------
----------------
Name: Fiat Punto
Farbe: gelb
Maximalgeschwindigkeit: 160
aktuelle Geschwindigkeit: 0
----------------
Opel Corsa(0kmh) ist schneller als, Fiat Punto(0kmh).
Fiat Punto(0kmh) ist schneller als, Opel Corsa(0kmh).
Die beiden Wagen sind gleich schnell unterwegs!
Das hält der Motor nicht aus.
Bitte geben Sie eine passende Geschwindigkeit an.
Exception in thread "main" Car$SpeedException
at Car.setSpeed(Car.java:60)
at Start.main(Start.java:13)
Kann mir hier jemand weiterhelfen?
Code:
public class Car { protected String name; protected String color; protected int curSpeed; protected int maxSpeed; // Constructor #1 public Car(String name) { this.name = name; } // Constructor #2 public Car(String name, String color) { this.name = name; this.color = color; } // Constructor #3 public Car(String name, int maxSpeed) { this.name = name; this.maxSpeed = maxSpeed; } // Constructor #4 public Car(String name, String color, int maxSpeed) { this.name = name; this.color = color; this.maxSpeed = maxSpeed; } public void stop() { this.curSpeed = 0; System.out.println(this.name + "(" + this.color + ") bremst bis zum Stillstand (" + this.curSpeed + "kmh)."); } public void setMaxSpeed(int maxSpeed) { this.stop(); this.maxSpeed = maxSpeed; System.out.println(this.name + "(" + this.color + ") hat nun einen neuen Motor mit neuer Maximalgeschwindigkeit (" + this.maxSpeed + "kmh)."); } public void setSpeed(int curSpeed) throws SpeedException { boolean raise = false; try { if (curSpeed > this.maxSpeed) { } } finally { if (true) { throw new SpeedException(curSpeed); } else { if(curSpeed > this.curSpeed) raise = true; this.curSpeed = curSpeed; if(raise == true) System.out.println(this.name + "(" + this.color + ") beschleunigt auf " + this.curSpeed + "kmh."); else System.out.println(this.name + "(" + this.color + ") bremst die Geschwindigkeit auf " + this.curSpeed + "kmh."); } } } public void setColor(String color) { this.color = color; } public boolean overtake(Car car) { boolean canOvertake = false; if ((this.curSpeed - car.curSpeed) >= 20) { canOvertake = true; } if (canOvertake == true) { System.out.println(this.name + "(" + this.curSpeed + "kmh)" + " hat " + car.name + "(" + car.curSpeed + "kmh) überholt!"); } else { System.out.println(this.name + "(" + this.curSpeed + "kmh)" + " kann " + car.name + "(" + car.curSpeed + "kmh) nicht überholen!"); } return canOvertake; } public boolean isFaster(Car car) { boolean isFaster = false; if (this.curSpeed > car.curSpeed) { isFaster = true; } if (isFaster == true) System.out.println(this.name + "(" + this.curSpeed + "kmh)" + " ist langsamer als, " + car.name + "(" + car.curSpeed + "kmh)."); else System.out.println(this.name + "(" + this.curSpeed + "kmh)" + " ist schneller als, " + car.name + "(" + car.curSpeed + "kmh)."); return isFaster; } public String getColor() { return this.color; } public String getName() { return this.name; } public int getCurSpeed() { return this.curSpeed; } public int getMaxSpeed() { return this.maxSpeed; } public String stats() { System.out.println("----------------\nName: " + this.name + "\nFarbe: " + this.color + "\nMaximalgeschwindigkeit: " + this.maxSpeed + "\naktuelle Geschwindigkeit: " + this.curSpeed + "\n----------------"); return this.name + this.color + this.maxSpeed + this.curSpeed; } public static void testCurSpeed(Car car1, Car car2) { if (car1.isFaster(car2) == true) { System.out.println("Der " + car1.name + " ist mit " + car1.curSpeed + "kmh, schneller als der " + car2.name + " mit " + car2.curSpeed + "kmh!"); } else if(car2.isFaster(car1) == true) { System.out.println("Der " + car2.name + " ist mit " + car2.curSpeed + "kmh, schneller als der " + car1.name + " mit " + car1.curSpeed + "kmh!"); } else { System.out.println("Die beiden Wagen sind gleich schnell unterwegs!"); } } public static Car testMaxSpeed(Car car1, Car car2) { if(car1.maxSpeed > car2.maxSpeed) { System.out.println("Der " + car1.name + " hat mit " + car1.maxSpeed + "kmh, die höhere Maximalgeschwindigkeit als " + car2.name + " mit " + car2.maxSpeed + "kmh!"); return car1; } else if(car2.maxSpeed > car1.maxSpeed) { System.out.println("Der " + car2.name + " hat mit " + car2.maxSpeed + "kmh, die höhere Maximalgeschwindigkeit als " + car1.name + " mit " + car1.maxSpeed + "kmh!"); return car2; } else { System.out.println("Beide Autos haben die gleiche Maximalgeschwindigkeit!"); return car1; } } public class SpeedException extends Exception { /** * */ private static final long serialVersionUID = 1L; public SpeedException() { // TODO Auto-generated constructor stub System.out.println("Geben Sie bitte eine Geschwindigkeit in Zahlen an"); } public SpeedException(int curSpeed) { System.out.println("Das hält der Motor nicht aus. \nBitte geben Sie eine passende Geschwindigkeit an."); } } }
Code:
public class Start { public static void main(String[] args) throws Car.SpeedException { Car corsa = new Car("Opel Corsa", "rot", 180); Car punto = new Car("Fiat Punto", "gelb", 160); corsa.stats(); punto.stats(); Car.testCurSpeed(corsa, punto); corsa.setSpeed(80); punto.setSpeed(100); punto.overtake(corsa); Car.testCurSpeed(corsa, punto); punto.setSpeed(40); corsa.overtake(punto); corsa.setSpeed(30); punto.overtake(corsa); corsa.stop(); punto.setMaxSpeed(220); corsa.setSpeed(50); punto.setSpeed(100); corsa.overtake(punto); Car.testCurSpeed(corsa, punto); Car.testMaxSpeed(corsa, punto); corsa.setMaxSpeed(240); Car.testMaxSpeed(corsa, punto); punto.stats(); Car ferrari = new Car("Ferrari 550 Maranello", "schwarz", 320); ferrari.setSpeed(250); ferrari.overtake(corsa); ferrari.overtake(punto); Car.testCurSpeed(ferrari, punto); ferrari.stats(); punto.setSpeed(340); } }
Comment