Hallo,
ich hab in meinem aktuellen Uni-Programm das Problem, dass ich die Klassenvariablen durch die this-Anweisung verheize und er mir nach der ersten Benutzung der Klasse nur noch Murks anzeigt.
Ich seh den Wald vor lauter Fehlern nicht mehr....
Die Klasse
Das Testprogramm
ich hab in meinem aktuellen Uni-Programm das Problem, dass ich die Klassenvariablen durch die this-Anweisung verheize und er mir nach der ersten Benutzung der Klasse nur noch Murks anzeigt.
Ich seh den Wald vor lauter Fehlern nicht mehr....
Die Klasse
Java:
@SuppressWarnings("serial")
class DivideByZeroException extends Exception
{
public DivideByZeroException() {}
public DivideByZeroException(String s)
{
super(s);
}
}
public class Complex
{
private double realTeil = 0.0;
private double imagTeil = 0.0;
public Complex(double r, double i)
{
realTeil = r;
imagTeil = i;
}
double getRealTeil()
{
return this.realTeil;
}
double getImagTeil()
{
return this.imagTeil;
}
void set(double r, double i)
{
this.realTeil = r;
this.imagTeil = i;
}
void add(Complex z)
{
this.realTeil = this.realTeil + z.realTeil;
this.imagTeil = this.imagTeil + z.imagTeil;
}
public Complex plus(Complex z2)
{
this.add(z2);
return this;
}
void subtract(Complex z)
{
this.realTeil = this.realTeil - z.realTeil;
this.imagTeil = this.imagTeil - z.imagTeil;
}
public Complex minus(Complex z2)
{
this.subtract(z2);
return this;
}
void multiplyBy(Complex z)
{
this.realTeil = (this.realTeil * z.realTeil) - (this.imagTeil * z.imagTeil);
this.imagTeil = (this.realTeil * z.imagTeil) + (this.imagTeil * z.realTeil);
}
public Complex multipliedBy(Complex z2)
{
this.multiplyBy(z2);
return this;
}
void divideBy(Complex z) throws DivideByZeroException
{
double r1 = this.realTeil;
double r2 = z.realTeil;
double i1 = this.imagTeil;
double i2 = z.imagTeil;
double div = ((r2*r2)+(i2*i2));
if (div == 0.0)
{
throw new DivideByZeroException("0 ist als Divisor nicht definiert");
}
else
{
r1 = ((r1 * r2) + (i1 * i2))/(div);
i1 = ((r1 * i2) - (i1 * r2))/(div);
}
}
public Complex dividedBy(Complex z2) throws DivideByZeroException
{
this.divideBy(z2);
return this;
}
public double betrag()
{
double r = this.realTeil;
double i = this.imagTeil;
return Math.sqrt((r*r) + (i*i));
}
public boolean equals(Complex z)
{
double r1 = this.realTeil;
double r2 = z.realTeil;
double i1 = this.imagTeil;
double i2 = z.imagTeil;
if (r1 == r2 && i1 == i2)
return true;
return false;
}
public String toString()
{
if (imagTeil == 0)
{
return realTeil + "";
}
else if (realTeil == 0)
{
return imagTeil + "i";
}
else if (imagTeil < 0)
{
return realTeil + " - " + (-imagTeil) + "i";
}
else
{
return realTeil + " + " + imagTeil + "i";
}
}
}
Das Testprogramm
Java:
public class ComplexText {
public static void main(String[] args)
{
try
{
Complex z1 = new Complex(5.0, 6.0);
Complex z2 = new Complex(3.0, 2.0);
System.out.println("z1 = " + z1);
System.out.println("z2 = " + z2);
System.out.println("z1 + z2 = " + z1.plus(z2));
System.out.println("z1 - z2 = " + z1.minus(z2));
System.out.println("z1 * z2 = " + z1.multipliedBy(z2));
System.out.println("z1 / z2 = " + z1.dividedBy(z2));
System.out.println("|z1| = " + z1.betrag());
System.out.println("|z2| = " + z2.betrag());
System.out.println("z1 = z2? " + z1.equals(z2));
}
catch (DivideByZeroException exc)
{
System.out.println( exc.getMessage());
}
}
}