0

My Question is about Complex numbers in Java. I created a class performing several mathematical operations like addition, subtraction, multiplication & division successfully. But my problem is how to implement cloneable and comparable interfaces which i dont understand. I understand the concept of cloning but i just cant seem to execute it, so as the comparable. Any ideas please? Thanks. You can take a look at my code below.

import java.util.Scanner;

public class Complex implements Cloneable {
 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";
 }

 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());

 }

}
Kamal
  • 37
  • 2
  • 10

1 Answers1

0

Cloneable interface :

A clone of an object is an object with distinct identity and equal contents. To define clone, a class must implement cloneable interface and must override Object’s clone method with a public modifier. The following code is a simple override of clone method.

public Complex clone() {
    // violation of contract to call super.clone() when creating instance of
    // the right class
    return new Complex(real,**img**);
}

Comparable interface :

A comparable interface will give an object a ability to tell if a given object will less or greater or equal to itself. It might be usefull when you sort a array of Complex objects . To give an object this ability a , one to implement comparable interface and override the compareTo method . A simple Example is

public int compareTo(Complex other) {
   //do your comparison here and return appropriately 
   return this.getReal() - other.getReal(); 
}   
Raghavan
  • 883
  • 1
  • 8
  • 19
  • Okay but what exactly am i to do in the compareTo method? Like what am i comparing? I have an idea but i'm not so sure if it's right. Like i create a new class lets call it complexText implements Comparable . Then; public int compareTo (Complex c1, Complex c2) { ....}. I need brief suggestions please. Thank you – Kamal Mar 12 '16 at 11:31
  • Lets say you have a array of complex number (objects of class complex) and If you want to sort them . Comparable interface gives you the ability to do this in a simpler way . When you implement the compareTo method in your Complex class . Then you can simply sort the array and the objects of Class complex will know how to compare themselves . Pls pay attention to the example of compareTo given. You have to implement compareTo in the Class Complex and not in some other class. – Raghavan Mar 12 '16 at 12:07
  • Your `clone` method won't be correct for subclasses of `Complex` -- it won't return an instance of the right class. If you had used `super.clone()`, then it will automatically give you an instance of the right class. – newacct Mar 15 '16 at 00:16
  • 1
    Also, your `compareTo` implementation has many problems. Subtraction of two doubles can overflow or underflow, or it could be out of range when you cast to `int`. Also, when the real values differ by less than 1, your function will return 0 (equal) -- this causes inconsistencies where A and B can be equal, and B and C can be equal, but A and C are not equal. – newacct Mar 15 '16 at 00:19