简体   繁体   中英

Cloneable and Comparable Interface

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

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

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