简体   繁体   中英

Implementing the Java Cloneable Interface

I am not sure on how to implement the Cloneable Interface in my Complex class. I have implemented the Comparable but i just cant seem to figure out the Cloneable. 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 public Complex clone () { then super.clone() and i think it returns new Complex (real part & imaginary part) but i am not so sure. Any suggestions on how i will implement is please?

import java.util.Comparator;
import java.util.Scanner;

public class Complex implements Cloneable, Comparable < Complex > {
  private double real;
  private double imag;

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

    System.out.println(
      "The Comparision of (" + a + " + " + b + "i) AND (" + c + " + " + d + "i) is " + c1.compareTo(c2));

  }

  @
  Override
  public int compareTo(Complex other) {
    int realCompare = Double.compare(getReal(), other.getReal());
    if (realCompare != 0) {
      return realCompare;
    }
    return Double.compare(getImag(), other.getImag());
  }

}

Implement like this and it will return the clone of Complex object. If you need something different then instead of calling super.clone() create Complex object on your own with whatever logic you need.

@Override
protected Object clone() throws CloneNotSupportedException {
    return super.clone();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM