You are not logged in.

  • Login

Dear visitor, welcome to Coder Forum. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Wednesday, December 12th 2007, 12:36pm

boolean Abfrage?

Hallo und zwar habe ich folgendes Problem.
Ich habe eine main-Klasse und eine andere Klasse (Konto und TestKonto).
Ich habe in der Klasse mehrere Methoden:

Konto.java

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
public class Konto {
private double kontoStand;
private double betrag;
 
//Konstruktor
public Konto(double k) {
this.kontoStand=k;
}
 
public boolean einzahlen(double betrag) {
 
if (betrag>0) {
 
return true;
}
else
{
return false;
}
} 
 
public double getKontoStand() {
return this.kontoStand+=betrag;
 
}

Jetzt möchte ich, wenn true zurückgegeben wird, den Betrag (den ich in der TestKlasse angeb) auf den Kontostand dazuaddiert wird. Des is ganz einfach, ich hab das auch schon öfters gemacht, aber irgend wie krieg ich des grad nicht hin. Ich brauch ja ne if-Verzweigung, ungefähr so: if(einzahlen=true) { this.kontoStand+=betrag}else{this.kontoStand=kontoStand}........aber so funktioniert es in der Klasse Konto jedenfalls schon mal nicht......
Ich würde mich über eine schnelle Hilfe sehr freuen.
Liebe Grüße, Angelika.

2

Wednesday, December 12th 2007, 2:40pm

Das ist doch eine setzende Methode. Du solltest die Operation also auch hier durchführen.

Java Quellcode

1
2
3
4
5
6
7
8
public boolean einzahlen(double betrag) {
    if(betrag > 0) {
        kontoStand += betrag;
        return true;
    } else {
        return false;
    }
}


Und das ist eine holende Methode. Die sollte nichts manipulieren.

Java Quellcode

1
2
3
public double getKontoStand() {
    return kontoStand;
}


Ist das schon die Lösung?

3

Thursday, December 13th 2007, 4:28pm

In Java musst du unterscheiden zwischen einer Zuweisung:

Java Quellcode

1
int x = 17;

und einem Vergleich

Java Quellcode

1
if (x == 17) { ... }

-> das 2. "=" beachten (ohne Abstand)!

Social bookmarks