Question: The following code is what I have done but read file is wrong. Please help me it is due tonight. public class Complex implements Comparable


The following code is what I have done but read file is wrong. Please help me it is due tonight.
public class Complex implements Comparable
private double r;
private double theta;
private final double DELTA = 0.000001;
public Complex() {
this.r = 0;
this.theta = 0;
}
public Complex(double r) {
this.r = r;
this.theta = 0;
}
public Complex(double r, double theta) {
this.r = r;
this.theta = theta;
}
public Complex add(Complex c) {
double real = this.getRealPart() + c.getRealPart();
double img = this.getImaginaryPart() + c.getImaginaryPart();
Complex result = new Complex(Math.sqrt((real * real + img * img)), Math.atan(img / real));
return result;
}
public Complex subtract(Complex c) {
double real = this.getRealPart() - c.getRealPart();
double img = this.getImaginaryPart() - c.getImaginaryPart();
Complex result = new Complex(Math.sqrt((real * real + img * img)), Math.atan(img / real));
return result;
}
public Complex multiply(Complex c) {
double r = this.r * c.r;
double theta = this.theta + c.theta;
Complex result = new Complex(r, theta);
return result;
}
public Complex divide(Complex c) {
double r = this.r / c.r;
double theta = this.theta - c.theta;
Complex result = new Complex(r, theta);
return result;
}
public double abs() {
return r;
}
public double getR() {
return r;
}
public double getTheta() {
return theta;
}
public double getRealPart() {
double[] rec = this.polartorectangular(this.getR(), this.getTheta());
return rec[0];
}
public double getImaginaryPart() {
double[] rec = this.polartorectangular(this.getR(), this.getTheta());
return rec[1];
}
public boolean greaterThen(Complex c) {
if(equalTo(c)) return false;
if((Math.abs(this.getRealPart() - c.getRealPart()) > DELTA
&& Math.abs(this.getImaginaryPart() - c.getImaginaryPart()) > 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()))
&& Math.abs(this.getImaginaryPart() - c.getImaginaryPart())
return true;
return false;
}
@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.getR(), this.getTheta());
return cloneObject;
}
@Override
public String toString() {
if(Math.abs(getR()) return "0.0000/_0.0000"; } else { return String.format("%.4f", getR()) + "/_" + String.format("%.4f", getTheta()); } } private String 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); return sb.toString() + " "; } private String 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); return sb.toString() + " "; } public String printAbsoluteValue() { return "|" + this.toString() + "| = " + String.format("%.4f", this.abs()) + " "; } public String cloneOperation() { Complex clonedObject = this.clone(); return "First complex number reference == clone reference is " + (this == clonedObject) + " " + "Clone real part is " + String.format("%.4f", clonedObject.getRealPart()) + " " + "Clone imaginary part is " + String.format("%.4f", clonedObject.getImaginaryPart()) + " "; } private String sortedResults(Complex[] src, int len) { String res = ""; Complex[] dest = new Complex[len]; System.arraycopy(src, 0, dest, 0, len); Arrays.sort(dest); res += "Sorted Result: "; for(Complex complex : dest) { res += complex.toString() + " "; } return res; } public String performComplexOperation(Complex c) { Complex[] complexes = new Complex[4]; String res = ""; int resultIndex = 0; complexes[resultIndex] = add(c); res += printBasicOperationalValue(this.toString(), c.toString(), complexes[resultIndex++].toString(), " + "); complexes[resultIndex] = subtract(c); res += printBasicOperationalValue(this.toString(), c.toString(), complexes[resultIndex++].toString(), " - "); complexes[resultIndex] = multiply(c); res += printBasicOperationalValue(this.toString(), c.toString(), complexes[resultIndex++].toString(), " * "); try { complexes[resultIndex] = divide(c); res += printBasicOperationalValue(this.toString(), c.toString(), complexes[resultIndex++].toString(), " / "); } catch (ArithmeticException exception) { res += printBasicOperationalValue(this.toString(), c.toString(), "java.lang.ArithmeticException: " + exception.getMessage(), " / "); } res += printAbsoluteValue(); boolean greaterThenResult = greaterThen(c); res += printBasicOperationalValue(this.toString(), c.toString(), greaterThenResult, " > "); boolean lessThenResult = lessThen(c); res += printBasicOperationalValue(this.toString(), c.toString(), lessThenResult, " boolean equalResult = equalTo(c); res += printBasicOperationalValue(this.toString(), c.toString(), equalResult, " == "); res += cloneOperation(); res += sortedResults(complexes, resultIndex); return res; } public double[] polartorectangular(double r, double theta) { double real = r * Math.cos(theta); double img = r * Math.sin(theta); double[] rectangular = {real, img}; return rectangular; } public double[] rectangular2polar(double x, double y) { double r = (double)Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); double theta = (double)Math.atan2(y, x); if(theta theta = 2* Math.PI + theta; } double[] polar = {r, theta}; System.out.println(r + ", " + theta); return polar; } public static void main(String[] args) throws Exception { java.io.File file = new java.io.File("testdata.txt"); Scanner sc = new Scanner(file); while( sc.hasNext()) { double[] valueArr = new double[4]; valueArr[1] = valueArr[1] / 180.0 * Math.PI; valueArr[3] = valueArr[3] / 180.0 * Math.PI; Complex firstComplexNumber = new Complex(valueArr[0], valueArr[1]); Complex secondComplexNumber = new Complex(valueArr[2], valueArr[3]); String res = firstComplexNumber.performComplexOperation(secondComplexNumber); res += "---------- "; System.out.println(res); } sc.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
