Question

I have to implement a class ComplexNumber. It has two generic parameter T and U, which must be from some class that inherits from Number class. The Complex class has 2 fields( instance variables) : real and imaginary part, and has to implement these methods :

  1. ComplexNumber(T real, U imaginary) - constructor
  2. getReal():T
  3. getImaginary():U
  4. modul():double - this is modulus of complex number
  5. compareTo(ComplexNumber<?, ?> o) - this method makes comparison based on modulus of 2 complex numbers

I have implemented all these methods except the last one, compareTo, since I don't know how to manipulate with these wildcards.

Here is my code : help here - pastebin

class ComplexNumber <T extends Number,U extends Number> implements Comparable<ComplexNumber> {

    private T real;
    private U imaginary;

    public ComplexNumber(T real, U imaginary) {
        super();
        this.real = real;
        this.imaginary = imaginary;
    }

    public T getR() {
        return real;
    }

    public U getI() {
        return imaginary;
    }

    public double modul(){

        return Math.sqrt(Math.pow(real.doubleValue(),2)+ Math.pow(imaginary.doubleValue(), 2));

    }



    public int compareTo(ComplexNumber<?, ?> o){

        //HELP HERE 
    }




}

Anybody help with this method?

Était-ce utile?

La solution

Since you only have to compare the modulus, you don't care about the type parameters.

@Override
public int compareTo(ComplexNumber<?, ?> o) {
    return Double.valueOf(modul()).compareTo(Double.valueOf(o.modul()));
}

However, you have to add the wildcards in the type declaration as well

class ComplexNumber <T extends Number,U extends Number> implements Comparable<ComplexNumber<?, ?>>

Autres conseils

Try it:

class ComplexNumber<T extends Number, U extends Number> implements Comparable<ComplexNumber<T, U>> {
  @Override
  public int compareTo(ComplexNumber<T, U> o) {
    return 0;
  }
}

It seems that both of your arguments can handle class that extends java.lang.Number and all the concrete classes have compare to one of the way you may want to do is as follows :

@Override
public int compareTo(ComplexNumber o) {

    if (o.real  instanceof BigInteger && this.real instanceof BigInteger) {
        int realCompValue = ((BigInteger)(o.real)).compareTo((BigInteger)(this.real));
        if (realCompValue == 0 ) {
            return compareImaginaryVal(o.imaginary, this.imaginary);
        } else {
            return realCompValue;
        }
    } else if (o.real  instanceof BigDecimal && this.real instanceof BigDecimal) {
        int realCompValue = ((BigDecimal)(o.real)).compareTo((BigDecimal)(this.real));
        if (realCompValue == 0 ) {
            return compareImaginaryVal(o.imaginary, this.imaginary);
        } else {
            return realCompValue;
        }
    } 

    // After checking all the Number extended class...
    else {
        // Throw exception.
    }
}

private int compareImaginaryVal(Number imaginary2, U imaginary3) {
    // TODO Auto-generated method stub
    return 0;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top