2016-11-15 1 views
-5

Die Methode sollte die Zeichenfolgendarstellung für eine komplexe Zahl zurückgeben. Die toString() Methode sollte Komplexe Zahlen zurück in der Form a + bi als String, wobei A der Realteil ist und b der ImaginärteilWie schreibe ich eine toString() -Methode, die komplexe Zahl in der Form a + bi als String zurückgeben soll?

public class Complex { 
     private double real; 
     private double imaginary; 

     //1.Complex() : This is the default constructor. 
     public void Complex(){ 
      this.real = 0; 
      this.imaginary = 0; 
     } 

     //2.Complex(double,double) 
     public void Complex(double r, double i){ 
      this.real = r; 
      this.imaginary = i; 
     } 

     //3.double getReal() 
     public double getReal(){ 
      return this.real; 
     } 

     //4.void setReal(double) 
     public void setReal(double real){ 
      this.real = real; 
     } 

     //5.double getImginary() 
     public double getImaginary(){ 
      return this.imaginary ; 
     } 

     //6. void setImaginary(double) 
     public void setImaginary(double imaginary){ 
      this.imaginary = imaginary; 
     } 

     //7.String toString() 
     public String toString1() { 
      return this.real + " + " + this.imaginary + "i"; 
     } 

     //8.Complex add(Complex) 
     public Complex add(Complex n){ 
      double r=this.real+n.real; 
      double i=this.imaginary + n.imaginary; 
      Complex s= new Complex(r,i); 
      return s; 

     } 

     //9.Complex sub(Complex) 
     public Complex sub(Complex n){ 
      double r= this.real- n.real; 
      double i= this.imaginary - n.imaginary; 
      Complex s= new Complex(r,i); 
      return s; 
     } 

     //10.Complex mul(Complex) 
     public Complex mul(Complex n){ 
      double r= this.real*n.real - this.imaginary*n.imaginary; 
      double i= this.real*n.imaginary+this.imaginary*n.real; 
      Complex s=new Complex(r,i); 
      return s; 
     } 


     //11.Complex div(Complex) 
     public Complex div(Complex n){ 
      double r= this.real/n.real- this.imaginary/n.imaginary; 
      double i = this.real/n.imaginary+this.imaginary/n.real; 
      Complex s=new Complex(r,i); 
      return s; 

} }

+0

Wie sieht die Klasse aus ??? –

+0

Zeigen Sie uns die grundlegende Klassenstruktur, die Sie verwenden, wie die Felder sind, wie erhalten Sie Eingabe 'a' und' b'. – SkrewEverything

+0

Wie würden Sie das als String erstellen, außerhalb einer 'toString()' Methode? Nun, machen Sie dasselbe, nur innerhalb einer 'toString()' Methode. –

Antwort

0

Sie überschreiben können toString() zu Holen Sie sich die gewünschte Beschreibung der Instanzen/Klassen.

Code:

class ComplexNum 
{ 
    int a,b; 

    ComplexNum(int a , int b) 
    { 
     this.a = a; 
     this.b = b; 
    } 

    @Override 
    public String toString() 
    { 
     return a + "+" + b + "i"; 
    } 

    public static void main(String[] args) 
    { 
     ComplexNum c = new ComplexNum(10,12); 
     System.out.println("Complex Number is: " + c); 
    } 
} 

ich es half hoffen.

Verwandte Themen