Verständnisproblem

valentina2013

Bekanntes Mitglied
Hallo,

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;
	}
geht leider nicht Fehlermeldung:The operator + is undefined for the argument type(s) .Rational,

Kann mir bitte einer helfen?
 
Hallo.
Etwa so?
Java:
    public RationalComplex add (RationalComplex other) {
            RationalComplex a = new RationalComplex();
            a.real = this.real.add(other.real);
            a.imag = this.imag.add(other.imag);
            return a;
        }
 
Zuletzt bearbeitet:
So müsste das grob aussehen:

Java:
public class MeineZahl() {
    public int wert;

    public MeineZahl(int wert) {
        this.wert = wert;
    }

    public MeineZahl add(MeineZahl other) {
        return new MeineZahl(wert + other.wert);
    }
}

Andere Klasse:
Java:
//...
MeineZahl zahl1 = new MeineZahl(5);
MeineZahl zahl2 = new MeineZahl(2);
// "5".add("2") -> "5" in zahl1 und "2" in zahl2
MeineZahl addierterWert = zahl1.add(zahl2); // == "7"
//...
 
Danke für eure Antworten:hänge immer noch an diese Aufgbe: jetzt muss ich die komplexe Coniugierte immplementiere-also ich muss nur der imaginere Teil von einer komplexen Zahl negieren. So etwas geht leider nicht:
Java:
public RationalComplex con () {
	RationalComplex c=new RationalComplex();
    	c.real=this.real;
        c.imag=-1*this.imag;
        return new RationalComplex(c.real,c.imag);
	
	}
habt ihr eine Idee wie ich es machen könnte?
 
Zuletzt bearbeitet:

Zurück
Oben