KomplexeZahlen

Hendrix69

Mitglied
Servus !
Ich habe für Hausaufgabe Theme Komplexe Zahlen und jetzt habe ich ein problem .
Ich habe der Code für Operationen so gemacht :
Code:
public class Complex {
    private double realTeil, imagTeil;

    Complex(double realTeil, double imagTeil) {
        this.realTeil = realTeil;
        this.imagTeil = imagTeil;
    }

    double getRealTeil() {
        return realTeil;
    }

    double getImagTeil() {
        return imagTeil;
    }

    void set(double realTeil, double imagTeil) {
        this.realTeil = realTeil;
        this.imagTeil = imagTeil;
    }

    void add(Complex z) {
        this.realTeil = this.realTeil + z.realTeil;
        this.imagTeil = this.imagTeil + z.imagTeil;
    }

    public Complex plus(Complex z2) {
        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) {
        subtract(z2);
        return this;
    }

    void multiply(Complex z) {
        double r1 = this.realTeil;
        double r2 = z.realTeil;
        double i1 = this.imagTeil;
        double i2 = z.imagTeil;

        this.realTeil = (r1 * r2) - (i1 * i2);
        this.imagTeil = (r1 * i2) + (i1 * r2);
    }

    public Complex multiplyWith(Complex z2) {
        multiply(z2);
        return this;
    }

    void divide(Complex z) {
        double r1 = this.realTeil;
        double r2 = z.realTeil;
        double i1 = this.imagTeil;
        double i2 = z.imagTeil;
        double div = ((r2 * r2) + (i2 * i2));

        this.realTeil = ((r1 * r2) + (i1 * i2)) / (div);
        this.imagTeil = ((r1 * i2) - (i1 * r2)) / (div);
    }

    public Complex divideWith(Complex z2) {
        divide(z2);
        return this;
    }

    public double betrag() {
        double r = this.realTeil;
        double i = this.imagTeil;

        return Math.sqrt((r * r) + (i * i));
    }

    @Override
    public int hashCode() {
        return Double.valueOf(realTeil).hashCode() * 31 + Double.valueOf(imagTeil).hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        }
        if(obj instanceof Complex) {
            Complex other = (Complex) obj;
            return realTeil == other.realTeil && imagTeil == other.imagTeil;
        }
        return false;
    }

    @Override
    public String toString() {
        if (imagTeil == 0.0 || Double.isNaN(imagTeil)) {
            return realTeil + "";
        } else if (realTeil == 0.0 || Double.isNaN(realTeil)) {
            return imagTeil + "i";
        } else if (imagTeil < 0.0) {
            return realTeil + " - " + (-imagTeil) + "i";
        } else {
            return realTeil + " + " + imagTeil + "i";
        }
    }
}




und wie rufe ich diese Operationen jetzt auf, dass wie auf diesem Bild sieht so aus :
12752


Und kann ich des in dieser Code so verwenden ?
Code:
public class KomplexeZahlen {
   
    public static void main (String[] args) {
        Out.println("n - fur neue komplexe Zahl ");
        Out.println("c - eine bestehende Zahl soll kopert werden ");
        Out.println("a - fur eine Addition " ) ;
        Out.println("s - fur eine Subtraktion ");
        Out.println("m - fur eine Multiplikation");
        Out.println("d - fur eine Division ");
        Out.println("e - fur Ende");

        char ch = 0; ;
       
        while(ch != 'e') {
            Out.println("---------------");
            Out.print("Biite Operation eingeben : ");
            ch = In.readChar();
            Out.println("---------------");
           
            if(ch == 'n') {
               
            } else if(ch == 'c') {
               
            } else if(ch == 'a') {
               
            } else if(ch == 's') {
               
            } else if(ch == 'm') {
               
            } else if(ch == 'd') {
               
            } else if(ch == 'e') {
               
            }
           
        }
    }
}

Und wie schreibe ich Funktion ,dass ich zwei nummer geben muss ?
 
Zuletzt bearbeitet:

mihe7

Top Contributor
Mit new Complex(re, im); erzeugst Du ein Complex-Objekt (das ja eine komplexe Zahl darstellt), wobei re un im natürlich durch den Real- bzw. Imaginärteil zu ersetzen wäre.
 

White_Fox

Top Contributor
Na, indem du die Methoden aufrufst:

Java:
Complex a = new Complex(1, 0);
Complex b = new Complex(0, 1);

Complex c = a.add(b);
 

Hendrix69

Mitglied
So ich habe jetzt Code bisschen geändert , und schaut so aus :
Code:
class Complex {
    double  real,image;
    public Complex ()
     {}
    public Complex ( double real, double image) {
        this.real=real;
        this.image= image;
    }
    
    public String GetValue() {
        return real +"+"+image+"i";
    }
    
    public static Complex Add(Complex c1,  Complex c2) {
        Complex c3 = new Complex();
        c3.real=c1.real+c2.real;
        c3.image=c1.image+c2.image;
        return c3;
     }
    
        public static Complex Sub(Complex c1,  Complex c2) {
        Complex c3 = new Complex();
        c3.real=c1.real-c2.real;
        c3.image=c1.image-c2.image;
        return c3;
    }
        
        public static Complex Multi(Complex c1,  Complex c2) {
        Complex c3 = new Complex();
        c3.real= ((c1.real * c2.real) - (c1.image * c2.image)) ;
        c3.image=((c1.real * c2.image) + (c1.image * c2.real)) ;
        return c3;
    }
        
        public static Complex Div(Complex c1,  Complex c2) {
        Complex c3 = new Complex();
        c3.real=(((c1.real * c2.real) + (c1.image * c2.image)) / ((c2.real * c2.real)+(c2.image * c2.image))) ;
        c3.image=(((c1.image * c2.real)-(c1.real * c2.image)) /  ((c2.real * c2.real)+(c2.image * c2.image))) ;
        return c3;
    }
        
        public String toString() {
        if (image == 0.0 || Double.isNaN(image)) {
            return real + "";
        } else if (real == 0.0 || Double.isNaN(real)) {
            return image + "i";
        } else if (image < 0.0) {
            return real + " - " + (-image) + "i";
        } else {
            return real + " + " + image + "i";
        }
    }
}


public class unbenannt {
    
    public static void main (String[] args) {
        Out.println("n - fur neue komplexe Zahl ");
        Out.println("c - eine bestehende Zahl soll kopert werden ");
        Out.println("a - fur eine Addition " ) ;
        Out.println("s - fur eine Subtraktion ");
        Out.println("m - fur eine Multiplikation");
        Out.println("d - fur eine Division ");
        Out.println("e - fur Ende");
        char ch = 0;
    
        while(ch != 'e') {
            Out.println("------------------------------");
            Out.print("Operation : ");
            ch = In.readChar();
        if(ch == 'n') {
        Out.print("Zahl eingeben (r i )  : ");
        Complex a= new Complex(In.readDouble() ,In.readDouble());
        Out.println(""+ a.GetValue());
        Complex b =new Complex(In.readDouble(),In.readDouble());
        Out.println(""+ b.GetValue());
        Complex c = Complex.Add(a,b);
        Complex s = Complex.Sub(a,b);
        Complex m = Complex.Multi(a,b);
        Complex d = Complex.Div(a,b);

        } else if(ch == 'a') {
Complex c = Complex.Add(a,b);  ???? 
 
        } else if(ch == 's') {
        
        
            
        } else if(ch == 'm') {
        
        
        
        } else if(ch == 'd') {
    
        
        
        } else if(ch == 'e') {
        
    }
}
}
}

Jetzt habe ich Problem , es funktioniert nicht wann ich so Methode aufrufen?
Wie kann ich noch die Methoden aufrufen in diese Situation?
 

Neue Themen


Oben