Hello how can i do this in Java? Design a class ComplexNo that allows you to add
ID: 3926057 • Letter: H
Question
Hello how can i do this in Java?
Design a class ComplexNo that allows you to add, subtract, multiply anddivide two complex numbers. Recall that a complex number is: a + bi, wherea, b are real numbers and i =-1. Note: to divide 2 complex numbers: (a + bi /c + di), multiply the fraction by 1 in the form of (c – di / c – di). The numbersc – di and c + di are called conjugates because their product is always c2 + d2,with no imaginary part.Provide (in addition to add, subtract, multiply, and divide) methods forequals (which returns boolean), and toString( ) (that returns a stringrepresenting the receiving object). Throw an exception if an attempt is madeto divide by 0 (0 + 0i). Your implementation should return the results of eachoperation WITHOUT changing the operands.
Explanation / Answer
public class Complex {
private double real; // the real part
private double imag; // the imag part
//creates a complex object with real and imaginary parts.
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
// returns a string object.
public String toString() {
if (imag == 0) return real + "";
if (real == 0) return imag + "i";
if (imag < 0) return real + " - " + (-imag) + "i";
return real + " + " + imag + "i";
}
// adds a given given complex object.
public Complex add(Complex b) {
Complex a = this; // invoking object
double real = a.real + b.real;
double imag = a.imag + b.imag;
return new Complex(real, imag);
}
// substracts a given complex object.
public Complex substract(Complex b) {
Complex a = this;
double real = a.real - b.real;
double imag = a.imag - b.imag;
return new Complex(real, imag);
}
// multiplies with a given complex object.
public Complex multiply(Complex b) {
Complex a = this;
double real = a.real * b.real - a.imag * b.imag;
double imag = a.real * b.imag + a.imag * b.real;
return new Complex(real, imag);
}
// return a new Complex object whose value is the reciprocal of this
public Complex reciprocal() {
double scale = real*real + imag*imag;
return new Complex(real / scale, -imag / scale);
}
// return a / b
public Complex divide(Complex b) {
Complex a = this;
return a.multiply(b.reciprocal());
}
// Equals method returns a boolean true if complex objects are equal, false if not equal.
public boolean equals(Complex x) {
return (this.real == x.real) && (this.imag == x.imag);
}
// Testing samples
public static void main(String[] args) {
Complex a = new Complex(3.0, 4.0);
Complex b = new Complex(-1.0, 2.0);
System.out.println("Equals:"+a.equals(new Complex(3.0,4.0)));
System.out.println("a : " + a);
System.out.println("b : " + b);
System.out.println("b + a : " + b.add(a));
System.out.println("a - b : " + a.substract(b));
System.out.println("a * b : " + a.multiply(b));
System.out.println("a / b : " + a.divide(b));
}
}
/* sample output
Equals:true
a : 3.0 + 4.0i
b : -1.0 + 2.0i
b + a : 2.0 + 6.0i
a - b : 4.0 + 2.0i
a * b : -11.0 + 2.0i
a / b : 1.0 - 2.0i
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.