Danke
!!!|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
public class Auto
{
// Attribute
double tankvolumen, benzinstand;
double verbrauch, kmstand;
// Konstruktoren
public Auto()
{
tankvolumen = 70.0; // L
benzinstand = 50.0; // L
verbrauch = 8.6; // L pro 100 km
kmstand =77000; // km
}
public Auto(double tv, double bst, double verb, double km)
{
tankvolumen = tv;
benzinstand = bst;
verbrauch = verb;
kmstand = km;
}
// Oeffentliche Methoden
public void tanken(double liter)
{
benzinstand = benzinstand + liter;
}
public void anzeigen()
{
System.out.println("Tankvolumen = "+tankvolumen+" Liter");
System.out.println("Benzinstand = "+benzinstand+" Liter");
System.out.println("Verbrauch = "+verbrauch +" Liter/100 km");
System.out.println("km-Stand = "+kmstand +" km");
}
// Private Methoden
}
|
).
Quoted
Die Methode tanken() ist noch fehlerhaft.
a) Warum ist die Methode noch fehlerhaft; was könnte man besser machen? (1 Punkt)
b) Verbessern Sie die Methode entsprechend! (2 Punkte)
This post has been edited 2 times, last edit by "Skyclimber" (Oct 1st 2010, 11:18pm)
|
|
Java Quellcode |
1 |
private double tankvolumen, benzinstand verbrauch, kmstand; |
|
|
Java Quellcode |
1 2 3 4 5 6 7 |
public Auto() { this. tankvolumen = 70.0; // L this.benzinstand = 50.0; // L this.verbrauch = 8.6; // L pro 100 km this.kmstand =77000; // km } |
|
|
Java Quellcode |
1 2 3 4 5 6 7 8 |
public void tanken(double liter) { benzinstand = benzinstand + liter; if (benzinstand > tankvolumen ){ ................. } |
du willst ja auch noch was spaß haben ) 

Klar war mir das schon vorher, nur kam mir die Aufgabenstellung so vor, dass wirklich ein Fehler darin stecken würde 

)|
|
Java Quellcode |
1 2 3 4 5 6 7 8 9 |
public void tanken(double liter) { if (liter <= (tankvolumen - benzinstand)) { benzinstand = benzinstand + liter; } else System.out.println("Der Tank ist zu klein für diese Menge Benzin!"); } |
.....



|
|
Java Quellcode |
1 2 3 4 5 6 7 8 |
public Auto(double tv, double bst, double verb, double km, int a) { this.alter = a; this.tankvolumen = tv; this.benzinstand = bst; this.verbrauch = verb; this.kmstand = km; } |
|
|
Java Quellcode |
1 2 3 4 5 6 7 8 |
// erstellen von 2 Objekten // tvolumen benzinst verb KM alter Auto Mercedes = new Mercedes (70.00, 50.00, 8.6, 30000.00, 3); Auto Opel = new Opel (70.00, 50.00, 8.6, 70000.00, 10); // Aufruf der Methode alter (); Mercede.alter(); Opel.alter(); |
|
|
Java Quellcode |
1 2 3 4 |
public void alter (){ System.out.println(alter+"Jahre"); } |
|
|
Java Quellcode |
1 2 |
Auto Opel = new Auto(); Auto Mercedes = new Auto(); |
|
|
Java Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
public class Auto { // Attribute private double ggewicht, lgewicht, gepaeck, tankvolumen, benzinstand, verbrauch, kmstand, zuladung; private int personen, passagiere; // Konstruktoren public Auto() { tankvolumen = 70.0; // L benzinstand = 50.0; // L verbrauch = 8.6; // L pro 100 km kmstand =77000; // km ggewicht = 3600; // zulässiges Gesamtgewicht in kg lgewicht = 1400; // Leergewicht in kg personen = 1; // Anzahl der Personen im Auto gepaeck = 0; // Gewicht des Gepaeck in kg } public Auto(double tv, double bst, double verb, double km, double lgw, int p, double gep) { tankvolumen = tv; benzinstand = bst; verbrauch = verb; kmstand = km; ggewicht = 3600; // zulässiges Gesamtgewicht in kg lgewicht = lgw; personen = p; gepaeck = gep; } // Oeffentliche Methoden public void tanken(double liter) { if (liter <= (tankvolumen - benzinstand)) { benzinstand = benzinstand + liter; } else System.out.println("Der Tank ist zu klein für diese Menge Benzin!"); } public void anzeigen() { zuladung = ((personen*50)+gepaeck); // gesamte Zuladung in kg bestehend aus Gepäck und Passagieren if (zuladung+lgewicht <= 1800) verbrauch = verbrauch; else if (zuladung+lgewicht <= 2400) verbrauch = (verbrauch+0.73); else if (zuladung+lgewicht <= 3000) verbrauch = (verbrauch+(2*0.73)); else if (zuladung+lgewicht <= 3600) verbrauch = (verbrauch+(3*0.73)); System.out.println("Tankvolumen = "+tankvolumen+" Liter"); System.out.println("Benzinstand = "+benzinstand+" Liter"); System.out.println("Verbrauch = "+verbrauch +" Liter/100 km"); System.out.println("km-Stand = "+kmstand +" km"); System.out.println("Leergewicht = "+lgewicht +" kg"); System.out.println("zulässiges Gesamtgewicht = "+ggewicht +" kg"); System.out.println("Personenanzahl = "+personen +" Personen"); System.out.println("Gewicht des Gepäck = "+gepaeck +" kg"); System.out.println("Zuladung = "+zuladung +" kg"); } public void passagiere(int passagiere) { if (passagiere > 4) System.out.println("So viele Leute passen nicht in dein Auto!"); else personen = (personen+passagiere); } public void fahren(double kilometer) { if ((kilometer/100*verbrauch) > benzinstand) { System.out.println("Für diese Strecke ist nicht genug Benzin vorhanden!"); } else { kmstand = kmstand + kilometer; benzinstand = benzinstand - kilometer/100 * verbrauch; } } // Private Methoden } |

Was hast du Vergessen ??? .... this. (also uns wird das zumindest im Studium eingetrichtert) !|
|
Java Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public void passagiere ( int passagiere){ { if (passagiere > 4) System.out.println("So viele Leute passen nicht in dein Auto!"); else personen = (personen+passagiere); } zuladung = ((personen*50)+gepaeck); // gesamte Zuladung in kg bestehend aus Gepäck und Passagieren if (zuladung+lgewicht <= 1800) verbrauch = verbrauch; else if (zuladung+lgewicht <= 2400) verbrauch = (verbrauch+0.73); else if (zuladung+lgewicht <= 3000) verbrauch = (verbrauch+(2*0.73)); else if (zuladung+lgewicht <= 3600) verbrauch = (verbrauch+(3*0.73)); } |
|
|
Java Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 |
public Auto2(double tv, double bst, double verb, double km, double lgw, int p, double gep) { this.tankvolumen = tv; this.benzinstand = bst; this.verbrauch = verb; this.kmstand = km; this.ggewicht = 3600; // zulässiges Gesamtgewicht in kg this.lgewicht = lgw; this.gepaeck = gep; this.passagiere(p); } |
|
|
Java Quellcode |
1 2 3 |
public void passagiere ( int passagiere){ } |
|
|
Java Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public void passagiere ( int passagiere, doubel gepaeck2 ){ . . . // hier rechnest du das gewicht ja mit ein was du im Konstruktor übergeben hast // fehlt also noch eine Zeile this.gepaeck=gepaeck+gepaeck2; zuladung = ((personen*50)+gepaeck); . . . } |
)


This post has been edited 1 times, last edit by "Skyclimber" (Oct 5th 2010, 5:45pm)