Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribed

1) make a copy of your Lab 3f and address ALL comments/issues (unless you got 100% on that lab). If there are ANY LI 2) modify your code from Lab 3f so that it uses polar coordinates to represent complex numbers. Specifically, replace a and b with r and theta (where r is positive and theta is in degrees) and update all methods to work with r and theta instead of a and b. Note the following: i) the single argument constructor should use r as its formal parameter. The two argument constructor should use r and theta as its formal parameters. ii) modify the toString method so it always formats r to 4 digits after the decimal followed by/_(forward slash then underscore) followed by theta to 4 digits after the decimal (e.g. 1.2333/_26.2345), except for the case where r is zero (subject to DELTA) where the output should be 0.0000/_0.0000 ili) add methods double getR) and double getTheta) (for completeness only as they aren't needed for this lab). iv) multiplication and division are simple and straight forward with polar coordinates and that should be reflected in your implementation of these methods v) addition and subtraction are more complicated with polar coordinates. Create a helper method double ] rectangular2polar(double a, double b) to aid in implementing your addition and subtraction methods (hint 1: you already have getRealPart) and getlmaginaryPart) methods; hint 2: use Math.atan2 and adjust the 0 to +180/0 to -180 range to be 0 to 360) 3) modify your main from Lab 3f to read input data from the file testdata.txt located here to test your code. The test file consists of an unknown number of rows each containing two operands (in the form r1 theta1 r2 theta2). Your code should handle bad input data (e.g. a letter where a double is expected) by catching the appropriate Exception, outputting a message like "Error: bad input on line 6: plus2.4" (for a bad input of plus2.4 found on line 6), discarding that line of input, and continuing. Your program should still output the results to the console as in Lab 3f, including the results of cloning (the only output still in rectangular coordinates) and sorting the results. Output a line with 10 dashes after outputting the results for each row of input read from the test file (including after any detected errors above). Note that the first three lines of input in the test file are the same input you ran for Lab 3f. You can use these lines to debug your code by hand checking the results against your Lab 3f results. 1) make a copy of your Lab 3f and address ALL comments/issues (unless you got 100% on that lab). If there are ANY LI 2) modify your code from Lab 3f so that it uses polar coordinates to represent complex numbers. Specifically, replace a and b with r and theta (where r is positive and theta is in degrees) and update all methods to work with r and theta instead of a and b. Note the following: i) the single argument constructor should use r as its formal parameter. The two argument constructor should use r and theta as its formal parameters. ii) modify the toString method so it always formats r to 4 digits after the decimal followed by/_(forward slash then underscore) followed by theta to 4 digits after the decimal (e.g. 1.2333/_26.2345), except for the case where r is zero (subject to DELTA) where the output should be 0.0000/_0.0000 ili) add methods double getR) and double getTheta) (for completeness only as they aren't needed for this lab). iv) multiplication and division are simple and straight forward with polar coordinates and that should be reflected in your implementation of these methods v) addition and subtraction are more complicated with polar coordinates. Create a helper method double ] rectangular2polar(double a, double b) to aid in implementing your addition and subtraction methods (hint 1: you already have getRealPart) and getlmaginaryPart) methods; hint 2: use Math.atan2 and adjust the 0 to +180/0 to -180 range to be 0 to 360) 3) modify your main from Lab 3f to read input data from the file testdata.txt located here to test your code. The test file consists of an unknown number of rows each containing two operands (in the form r1 theta1 r2 theta2). Your code should handle bad input data (e.g. a letter where a double is expected) by catching the appropriate Exception, outputting a message like "Error: bad input on line 6: plus2.4" (for a bad input of plus2.4 found on line 6), discarding that line of input, and continuing. Your program should still output the results to the console as in Lab 3f, including the results of cloning (the only output still in rectangular coordinates) and sorting the results. Output a line with 10 dashes after outputting the results for each row of input read from the test file (including after any detected errors above). Note that the first three lines of input in the test file are the same input you ran for Lab 3f. You can use these lines to debug your code by hand checking the results against your Lab 3f results

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Are these written ground rules?

Answered: 1 week ago

Question

Demonstrate three aspects of assessing group performance?

Answered: 1 week ago