Question: public class Complex implements Comparable , Cloneable{ private double realPart; private double imaginary; private final double DELTA = 0.000001; public Complex() { this.realPart = 0;

 public class Complex implements Comparable, Cloneable{ private double realPart; private doubleimaginary; private final double DELTA = 0.000001; public Complex() { this.realPart =0; this.imaginary = 0; } public Complex(double realPart) { this.realPart = realPart;this.imaginary = 0; } public Complex(double realPart, double imaginary) { this.realPart =

public class Complex implements Comparable, Cloneable{

private double realPart; private double imaginary; private final double DELTA = 0.000001;

public Complex() { this.realPart = 0; this.imaginary = 0; }

public Complex(double realPart) { this.realPart = realPart; this.imaginary = 0; }

public Complex(double realPart, double imaginary) { this.realPart = realPart; this.imaginary = imaginary; }

public Complex add(Complex c) { Complex result = new Complex(this.getRealPart() + c.getRealPart(), this.getImaginary() + c.getImaginary()); return result; }

public Complex subtract(Complex c) { Complex result = new Complex(this.getRealPart() - c.getRealPart(), this.getImaginary() - c.getImaginary()); return result; }

public Complex multiply(Complex c) { double real = (this.getRealPart() * c.getRealPart()) - (this.getImaginary() * c.getImaginary()); double img = (this.getRealPart() * c.getImaginary()) + (this.getImaginary() * c.getRealPart()); Complex result = new Complex(real, img); return result; }

public Complex divide(Complex c) { if(c.abs()

Complex result = new Complex(real, img); return result; } public double abs() { return Math.sqrt(Math.pow(getRealPart(), 2) + Math.pow(getImaginary(), 2)); } public double getRealPart() { return realPart; }

public double getImaginary() { return imaginary; }

public boolean greaterThen(Complex c) { if(equalTo(c)) return false; if((Math.abs(this.getRealPart() - c.getRealPart()) > DELTA && Math.abs(this.getImaginary() - c.getImaginary()) > DELTA) && (this.abs() > c.abs())) return true; return false; }

public boolean lessThen(Complex c) { if(equalTo(c) || greaterThen(c)) return false; return true; }

public boolean equalTo(Complex c) { if(Math.abs(Math.abs(this.getRealPart()) - Math.abs(c.getRealPart()))

@Override public int compareTo(Complex c) { if(equalTo(c)) return 0; if(greaterThen(c)) return 1; return -1; }

@Override public Complex clone() { Complex cloneObject = new Complex(this.getRealPart(), this.getImaginary()); return cloneObject; }

@Override public String toString() { String realPartString = String.format("(%.4f", getRealPart()); if(Math.abs(getImaginary())

private void printBasicOperationalValue(String a, String b, String c, String operation) { StringBuilder sb = new StringBuilder(); sb.append(a); sb.append(operation); sb.append(b); sb.append(" = "); sb.append(c); System.out.println(sb.toString()); }

private void printBasicOperationalValue(String a, String b, boolean c, String operation) { StringBuilder sb = new StringBuilder(); sb.append(a); sb.append(operation); sb.append(b); sb.append(" = "); sb.append(c); System.out.println(sb.toString()); }

public void printAbsoluteValue() { System.out.println("|" + this.toString() + "| = " + this.abs()); }

public void cloneOperation() { Complex clonedObject = this.clone(); System.out.println("First complex number reference == clone reference is " + (this == clonedObject)); System.out.println("Clone real part is " + clonedObject.getRealPart()); System.out.println("Clone imaginary part is " + clonedObject.getImaginary()); }

private void sortedResults(Complex[] src, int len) { Complex[] dest = new Complex[len]; System.arraycopy(src, 0, dest, 0, len); Arrays.sort(dest); System.out.println("Sorted Result: "); for(Complex complex : dest) { System.out.println(complex.toString()); } }

public void performComplexOperation(Complex c) { Complex[] complexes = new Complex[4]; int resultIndex = 0; complexes[resultIndex] = add(c); printBasicOperationalValue(this.toString(), c.toString(), complexes[resultIndex++].toString(), " + "); complexes[resultIndex] = subtract(c); printBasicOperationalValue(this.toString(), c.toString(), complexes[resultIndex++].toString(), " - "); complexes[resultIndex] = multiply(c); printBasicOperationalValue(this.toString(), c.toString(), complexes[resultIndex++].toString(), " * "); try { complexes[resultIndex] = divide(c); printBasicOperationalValue(this.toString(), c.toString(), complexes[resultIndex++].toString(), " / "); } catch (ArithmeticException exception) { printBasicOperationalValue(this.toString(), c.toString(), "java.lang.ArithmeticException: " + exception.getMessage(), " / "); } printAbsoluteValue(); boolean greaterThenResult = greaterThen(c); printBasicOperationalValue(this.toString(), c.toString(), greaterThenResult, " > "); boolean lessThenResult = lessThen(c); printBasicOperationalValue(this.toString(), c.toString(), lessThenResult, "

public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.printf("Enter the first complex number: "); Complex firstComplexNumber = new Complex(scanner.nextDouble(), scanner.nextDouble()); System.out.printf("Enter the second complex number: "); Complex secondComplexNumber = new Complex(scanner.nextDouble(), scanner.nextDouble()); firstComplexNumber.performComplexOperation(secondComplexNumber); } }

i) implement the Comparable interface with the comparison based on the absolute value of a Complex number for greater than or less than and on the real and imaginary parts being identical for equality. You should define a constant DELTA in Complex as follows: private final double DELTA 0.000001; and use it to check for equivalence and in comparisons. For example, if you have doubles x and y and want to see if they are equal, then you should use the expression Math.abs(x - y) DELTA or if you want to check if x > y then you should use the expression (x > y) && (Math.absx y)>DELTA) or if you want to check if x is zero then use Math.abs(x) DELTA ii) modify the divide method to throw an ArithmeticException with the message "Illegal divide by zero" if the denominator is zero (for this purpose, a double x is considered zero if Math.abs(x) DELTA) ii) the toString override should return four digits to the right of the decimal (hint: String.format) and if the imaginary part is zero (determined as in ii) above) the return only the real part (i.e. no parentheses or + sign) iv) add boolean methods greaterThan(Complex c), lessThan(Complex c), and equalTo(Complex c). greaterThan and lessThan should be based on the absolute value, while equalTo should return true only if the real and imaginary parts of both complex numbers are identical (subject to DELTA of course). (Hint: both greater than and less than should first check that the numbers are not equal) v) test your class using the main method that prompts the user for two complex numbers and output the results show below (green is user input and blue is program output). In the case of an exception, reprint the divide expression in your catch block so that the exception string is to the right of the equals sign (e.g. (1.0000+0000i)/ (0.0000+ 0.000Oi) java.lang.ArithmeticException: Illegal divide by zero) Run your program three times with the following inputs: First complex number 3.5 5.5 Second complex number -3.5 1 First complex number 60.5 1.1 Second complex number -60.5 1.1 Third run: i) implement the Comparable interface with the comparison based on the absolute value of a Complex number for greater than or less than and on the real and imaginary parts being identical for equality. You should define a constant DELTA in Complex as follows: private final double DELTA 0.000001; and use it to check for equivalence and in comparisons. For example, if you have doubles x and y and want to see if they are equal, then you should use the expression Math.abs(x - y) DELTA or if you want to check if x > y then you should use the expression (x > y) && (Math.absx y)>DELTA) or if you want to check if x is zero then use Math.abs(x) DELTA ii) modify the divide method to throw an ArithmeticException with the message "Illegal divide by zero" if the denominator is zero (for this purpose, a double x is considered zero if Math.abs(x) DELTA) ii) the toString override should return four digits to the right of the decimal (hint: String.format) and if the imaginary part is zero (determined as in ii) above) the return only the real part (i.e. no parentheses or + sign) iv) add boolean methods greaterThan(Complex c), lessThan(Complex c), and equalTo(Complex c). greaterThan and lessThan should be based on the absolute value, while equalTo should return true only if the real and imaginary parts of both complex numbers are identical (subject to DELTA of course). (Hint: both greater than and less than should first check that the numbers are not equal) v) test your class using the main method that prompts the user for two complex numbers and output the results show below (green is user input and blue is program output). In the case of an exception, reprint the divide expression in your catch block so that the exception string is to the right of the equals sign (e.g. (1.0000+0000i)/ (0.0000+ 0.000Oi) java.lang.ArithmeticException: Illegal divide by zero) Run your program three times with the following inputs: First complex number 3.5 5.5 Second complex number -3.5 1 First complex number 60.5 1.1 Second complex number -60.5 1.1 Third run

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!