Hallo,
habe schon wieder ein kleines Problem. Gegeben ist eine Klasse:
und noch eine andere Klasse die variablen der Typ Rational enthält. Ich muss diese klasse implementieren:
verstehe leider nicht wie ich in RationalComplexadd addieren soll:ich dachte an sowas:
geht leider nicht Fehlermeldung:The operator + is undefined for the argument type(s) .Rational,
Kann mir bitte einer helfen?
habe schon wieder ein kleines Problem. Gegeben ist eine Klasse:
Java:
class Rational {
private int numerator, denominator;
public Rational() {
this.numerator = 0;
this.denominator = 1;
}
public Rational(int z, int n) {
if (n < 0) {
n = -n;
z = -z;
}
numerator = z;
denominator = n;
reduce();
}
public Rational(Rational other) {
this.numerator = other.numerator;
this.denominator = other.denominator;
}
public int getDenominator() {
return denominator;
}
public int getNumerator() {
return numerator;
}
public Rational add(Rational other) {
int n = this.denominator*other.denominator;
int z = this.numerator*other.denominator+other.numerator*this.denominator;
return new Rational(z,n);
}
public Rational sub(Rational other) {
int n = this.denominator*other.denominator;
int z = this.numerator*other.denominator-other.numerator*this.denominator;
return new Rational(z,n);
}
public Rational mul (Rational other) {
int z = this.numerator*other.numerator;
int n = this.denominator*other.denominator;
return new Rational(z,n);
}
public Rational div (Rational other) {
int z = this.numerator*other.denominator;
int n = this.denominator*other.numerator;
return new Rational(z,n);
}
private void reduce() {
int g = gcd(Math.abs(this.numerator),this.denominator);
this.numerator /= g;
this.denominator /= g;
}
private int gcd(int a,int b) {
if (b == 0)
return a;
return gcd(b,a%b);
}
}
und noch eine andere Klasse die variablen der Typ Rational enthält. Ich muss diese klasse implementieren:
Java:
RationalComplex() {}
RationalComplex(Rational real, Rational imag) {
this.real = new Rational(real);
this.imag = new Rational(imag);
}
RationalComplex(RationalComplex other) {
this.real = new Rational(other.real);
this.imag = new Rational(other.imag);
}
public RationalComplex add (RationalComplex other) {
return new RationalComplex();
}
public RationalComplex sub (RationalComplex other) {
return new RationalComplex();
}
}
}
verstehe leider nicht wie ich in RationalComplexadd addieren soll:ich dachte an sowas:
Java:
public RationalComplex add (RationalComplex other) {
RationalComplex a=new RationalComplex();
a.real=this.real+other.real;
return a;
}
Kann mir bitte einer helfen?