Eine Klasse mit 2 Konstruktoren

ich wollte ein Klasse namens Complex für Komplexe ertellen:



Code:
import java.util.*;
class Complex {

// real
public double real;

// imag
public double imag;


public Complex(double myReal, double myImag) {
this.real = myReal;
this.imag = myImag;
}


public double getReal() {
return real;
}


public double getImag() {
return imag;
}


public String ausgabe(String iOderJ){
String erg=String.format(Locale.ENGLISH, "%+-1.3f%+-1.3f%s",
this.real,
this.imag,
iOderJ);
return erg;
}



}


Als Konstruktor wollte ich auch ein Konstruktor mit den Argumenten betrag und winkel. Leider das hat nix geklappt






Code:
import java.util.*;
class Complex {

// real
public double real;

// imag
public double imag;


public Complex(double myBetrag, double myWinkel) {
this.real = myBetrag*Math.cos(myWinkel);
this.imag =myBetrag*Math.sin(myWinkel);
}

public Complex(double myReal, double myImag) {
this.real = myReal;
this.imag = myImag;
}


public double getReal() {
return real;
}


public double getImag() {
return imag;
}


public String ausgabe(String iOderJ){
String erg=String.format(Locale.ENGLISH, "%+-1.3f%+-1.3f%s",
this.real,
this.imag,
iOderJ);
return erg;
}



}
 
Das liegt daran, dass die Signatur beider Konstruktoren gleich ist: beide mal ist es "double, double". Wenn jetzt der Konstruktor aufgerufen wird, kann Java nicht genau sagen, welchen Konstruktor es jetzt anhand der gegebenen Parameter aufrufen soll. Was Du allerdings machen kannst, ist, dass Du eine Klassenmethode schreibst, aus der Name hervorgeht, von welchen Ausgangswerten Du ausgehst, und dementsprechend den Konstruktor mit den jeweils sinnvollen Werten aufruft.
 
Du könntest für den Winkel auch einen flaot nehmen statt einen double ^^ ist das erste was mir jetzt dazu eingefallen ist :D

Java:
import java.util.*;

class Complex {

   // real
   public double real;

   // imag
   public double imag;

   private int debug = 1;
   public Complex(double myBetrag, float myWinkel) {
     if(debug==1)
     {
       System.out.println("Bertrag\t" + myBetrag + "*" + "\tcos(" + myWinkel + ")=" + myBetrag + "*" +Math.cos(myWinkel) + "\t=" +myBetrag * Math.cos(myWinkel));
       System.out.println("Bertrag\t" + myBetrag + "*" + "\tsin(" + myWinkel + ")=" + myBetrag + "*" +Math.sin(myWinkel) + "\t=" +myBetrag * Math.sin(myWinkel));
       
     }
     this.real = myBetrag * Math.cos(myWinkel);
     this.imag = myBetrag * Math.sin(myWinkel);
   }

   public Complex(double myReal, double myImag) {
     this.real = myReal;
     this.imag = myImag;
   }

   public double getReal() {
     return real;
   }

   public double getImag() {
     return imag;
   }

   public String ausgabe(String iOderJ) {
     String erg = String.format(Locale.ENGLISH, "%+-1.3f%+-1.3f%s", this.real, this.imag, iOderJ);
     return erg;
   }

   public static void main(String[] args) {
     Complex comp = new Complex(12.45, (float) 0.1234);
     System.out.println(comp.ausgabe("i"));
     comp = new Complex(12.45, /*(float)*/ 0.1234);
     System.out.println(comp.ausgabe("i"));

   }

}

Viele Grüße
Youza

Ps.: weiß aber nicht ob du damit auf dauer glücklich werden würdest, denn jedes mal wenn du nicht zu float castest bei der 2 Variablen wird automatisch der Konstruktor mit den Doublewerten genommen.
 
Ich dachte an so etwas:
Java:
class Complex {
  public double real;
  public double imaginary;

  public static Complex fromPolar(double distance, double arc) {
    return new Complex(distance * Math.cos(arc), distance * Math.sin(arc));
  }

  public static Complex fromCartesian(double real, double imaginary) {
    return new Complex(real, imaginary);
  }

  private Complex(double real, double imaginary) {
    this.real      = real;
    this.imaginary = imaginary;
  }

  public double getRealPart() {
    return this.real;
  }

  public double getImaginaryPart() {
    return this.imaginary;
  }
}
Java:
Complex polar     = Complex.fromPolar(100.0, 45.0);
Complex cartesian = Complex.fromCartesian(20.0, 20.0);
 
Und was sagen Sie dazu (Vorschlag eines Kommilitonen)


Code:
import java.util.*;
class Complex {

// real
public double real;

// imag
public double imag;




public Complex(double myReal, double myImag) {
this.real = myReal;
this.imag = myImag;
}

public Complex(double myBetrag, double myWinkel, String einheit) {
if(einheit.equals("rad"))
{
this.real = myBetrag*Math.cos(myWinkel);
this.imag =myBetrag*Math.sin(myWinkel);
}

if(einheit.equals("deg"))
{
this.real = myBetrag*Math.cos(myWinkel*Math.PI/180);
this.imag =myBetrag*Math.sin(myWinkel*Math.PI/180);
}
}

public double getReal() {
return real;
}


public double getImag() {
return imag;
}


public String ausgabe(String iOderJ){
String erg=String.format(Locale.ENGLISH, "%+-1.3f%+-1.3f%s",
this.real,
this.imag,
iOderJ);
return erg;
}



}
 
Kann man schon so machen, aber ich persönlich würde aufgrund der Klarheit auf meinen Ansatz zurückgreifen. Da kann man dann auch noch als optionalen dritten Parameter die entsprechende Einheit angeben:
Java:
class Complex {
  public double real;
  public double imaginary;

  public static Complex fromPolar(double distance, double arc) {
    return new Complex(distance * Math.cos(arc), distance * Math.sin(arc));
  }

  public static Complex fromPolar(double distance, double arc, String unit) {
    switch (unit) {
      case "rad" : return fromPolar(distance, arc * (180 / Math.PI));
      case "grad": return fromPolar(distance, arc);
    }
  }

  public static Complex fromCartesian(double real, double imaginary) {
    return new Complex(real, imaginary);
  }

  private Complex(double real, double imaginary) {
    this.real      = real;
    this.imaginary = imaginary;
  }

  public double getRealPart() {
    return this.real;
  }

  public double getImaginaryPart() {
    return this.imaginary;
  }
}
 
Zurück