class Complex implements Comparable { private double real, imag; public Complex(double real, double imag) { this.real = real; this.imag = imag; } public double abs() { return Math.sqrt(this.real*this.real + imag*imag); } // Add the given complex number to self public void add(Complex c1) { this.real += c1.real; this.imag += c1.imag; } public static Complex subtract(Complex c1, Complex c2) { return new Complex(c1.real-c2.real, c1.imag-c2.imag); } public void copyFrom(Complex c1) { real = c1.real; imag = c1.imag; } public void subtract(Complex c1) { Complex c3 = subtract(this, c1); copyFrom(c3); //this = c3; } int compareTo(Complex x) { double my_abs = abs(); double x_abs = x.abs(); if(my_abs < x_abs) { return -1; } else if(my_abs > x_abs) { return 1; } else { return 0; } // return abs() - x.abs(); } public int compareTo(Object x) { System.out.println("Before cast"); Complex x1 = (Complex)x; System.out.println("After cast"); return compareTo(x1); } public static void main(String[] args) { Complex c1 = new Complex(0,0); Complex c2 = new Complex(0,3); Complex c3 = new Complex(2, 0); Complex[] A = {c1, c2, c3}; GenericSort.sort(A); for(int i = 0; i < A.length; i++) { System.out.print(" "+A[i].abs()+","); } System.out.println(); //String s1 = "abc"; //System.out.println(c1.compareTo(s1)); } } // End class Complex