title: Implementing the Java comparable interface?
body: <p>I am not sure how to implement a comparable interface into my complex class. I have the following example code that I am using to try and get my head around it. I know it should be something like <code>public double compareTo (Complex o)</code> and something like that but I am not so sure on how to do it. Any suggestions on how i will implement it?:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Complex implements Cloneable, Comparable {
      private double real;
      private double imag;
  
      /*
       * public Object clone() throws CloneNotSupportedException { Complex
       * objClone = new Complex(); objClone.setReal(this.real);
       * objClone.setImag(this.imag); return objClone; }
       */
  
      public Complex(double real, double imag) {
          this.real = real;
          this.imag = imag;
      }
  
      public Complex(double real) {
          this.real = real;
      }
  
      public Complex() {
  
      }
  
      public void setReal(double real) {
          this.real = real;
      }
  
      public void setImag(double imag) {
          this.imag = imag;
      }
  
      public double getReal() {
          return real;
      }
  
      public double getImag() {
          return imag;
      }
  
      public void add(Complex num1, Complex num2) {
          this.real = num1.real + num2.real;
          this.imag = num1.imag + num2.imag;
  
      }
  
      public Complex subtract(Complex num) {
          Complex a = this;
          double real = a.real - num.real;
          double imag = a.imag - num.imag;
          return new Complex(real, imag);
      }
  
      public Complex multiply(Complex num) {
          Complex a = this;
          double real = a.real * num.real - a.imag * num.imag;
          double imag = a.real * num.imag + a.imag * num.real;
          return new Complex(real, imag);
      }
  
      public Complex divide(Complex c1, Complex c2) {
          return new Complex((c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag),
                  (c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag));
      }
  
      public double absolute() {
          return Math.sqrt(real * real + imag * imag);
      }
  
      public String toString() {
          return this.real + " + " + this.imag + "i";
      }
  
      @Override
      public Complex clone() throws CloneNotSupportedException {
          super.clone();
          return new Complex(real, imag);
      }
  
      public static void main(String[] args) {
  
          Scanner in = new Scanner(System.in);
          System.out.print("Enter the first set of complex numbers respectively: ");
          double a = in.nextDouble();
          double b = in.nextDouble();
  
          Complex c1 = new Complex(a, b);
  
          System.out.print("Enter the second set of complex numbers respectively: ");
          double c = in.nextDouble();
          double d = in.nextDouble();
  
          Complex c2 = new Complex(c, d);
  
          Complex result = new Complex(c, d);
          result.add(c1, c2);
  
          System.out.println("(" + a + " + " + b + "i) + (" + c + " + " + d + "i) = " + result.toString());
          System.out.println("(" + a + " + " + b + "i) - (" + c + " + " + d + "i) = " + c1.subtract(c2));
          System.out.println("(" + a + " + " + b + "i) * (" + c + " + " + d + "i) = " + c1.multiply(c2));
          System.out.println("(" + a + " + " + b + "i) / (" + c + " + " + d + "i) = " + result.divide(c1, c2).toString());
          System.out.println("|" + a + " + " + b + "i| = " + c1.absolute());
  
      }
  
      public double compareTo(Complex other) {
  
          return this.getReal() - other.getReal();
      }
  
  }
  </code></pre>