Using Java ((Abstract Class and Interface)) 13.17 (Math: The complex class) A co
ID: 639048 • Letter: U
Question
Using Java ((Abstract Class and Interface))
13.17 (Math: The complex class) A complaex number is a number in the from a + bi,
where a and b are real number i is square root of -1 . the number a and b are known
as the real part and imaginary oart of the comlex number, respectively, you can
perform addition, subtraction, multiplcation, and divison for complex number using the fllowing formulas:
a + bi + c + di = ( a + c) + (b + d )i
a - bi - c - di = ( a - c) - (b - d )i
(a + bi) * (c + di) = ( ac - bd ) + (bc + ad )i
(a + bi) / (c + di) = (ac + bd ) / (c^2 + d^2 ) + (bc - ad) i/ (c^2 + d^2)
you can also obtain the absolut value for a coplaex number using the fllowing formula:
|a + bi| = square root a^2 + b^2
(A complaex number can be interpreted as a point on plane by identifying the (a,b)
values as the coordinates of the point. The absolute of the number corresponds to the
distance of the point to the origin.) Desing a class named complex for representing complex
numbers and the method add, subtract, multiply, divide and ads for performing complex- number
oprations and override toString method for retuning a sting repre- sentation for Complex number.
The toSring method return (a + bi) as string. if b is 0, it simply returns a. Your Copmlex class
should also implement the Cloneable interface.
Provid three constructors Complex ( a, b) Complex (a) and Complex(). Complex() creats a Complex object
for number 0 and Complex(a) creats a Complex object with 0 for b. Also, provide the getRealPart()
and Complex number, respectively.
Write Test program that prompte the user to enter two Complex numbers and display the result of their addition, subtraction,
multiplication, divison, and absolute value.
Explanation / Answer
public final class Complex { // The number stored is x+I*y. final private double x, y; // I don't want to allow anyone to access these numbers so I've labeled // them private. /** Construct a point from real and imaginary parts. */ public Complex(double real_part, double imaginary_part) { x=real_part; y=imaginary_part; } /** Construct a real number. */ public Complex(double real_part) { x=real_part; y=0; } // A static constructor. /** Construct a complex number from the given polar coordinates. */ public static Complex fromPolar(double r, double theta) { return new Complex(r*Math.cos(theta), r*Math.sin(theta)); } // Basic operations on Complex numbers. /** Return the real part. */ public double re(){ return x; } /** Return the imaginary part. */ public double im(){ return y; } /** Return the complex conjugate */ public Complex conj() { return new Complex(x,-y); } /** Return the square of the absolute value. */ public double absSquared() { return x*x+y*y; } /** Return the absolute value. */ public double abs() { // The java.lang.Math package contains many useful mathematical functions, // including the square root function. return Math.sqrt(absSquared()); } // ARITHMETIC /** Add a complex number to this one. * * @param z The complex number to be added. * @return A new complex number which is the sum. */ public Complex add(Complex z) { return new Complex(x+z.x, y+z.y); } /** Subtract a complex number from this one. * * @param z The complex number to be subtracted. * @return A new complex number which is the sum. */ public Complex minus(Complex z) { return new Complex(x-z.x, y-z.y); } /** Negate this complex number. * * @return The negation. */ public Complex neg() { return new Complex(-x, -y); } /** Compute the product of two complex numbers * * @param z The complex number to be multiplied. * @return A new complex number which is the product. */ public Complex mult(Complex z) { return new Complex(x*z.x-y*z.y, x*z.y+z.x*y); } /** Divide this complex number by a real number. * * @param q The number to divide by. * @return A new complex number representing the quotient. */ public Complex div(double q) { return new Complex(x/q,y/q); } /** Return the multiplicative inverse. */ public Complex inv() { // find the square of the absolute value of this complex number. double abs_squared=absSquared(); return new Complex(x/abs_squared, -y/abs_squared); } /** Compute the quotient of two complex numbers. * * @param z The complex number to divide this one by. * @return A new complex number which is the quotient. */ public Complex div(Complex z) { return mult(z.inv()); } /** Return the complex exponential of this complex number. */ public Complex exp() { return new Complex(Math.exp(x)*Math.cos(y),Math.exp(x)*Math.sin(y)); } // FUNCTIONS WHICH KEEP JAVA HAPPY: /** Returns this point as a string. * The main purpose of this function is for printing the string out, * so we return a string in a (fairly) human readable format. */ // The _optional_ override directive "@Override" below just says we are // overriding a function defined in a parent class. In this case, the // parent is java.lang.Object. All classes in Java have the Object class // as a superclass. @Override public String toString() { // Comments: // 1) "" represents the empty string. // 2) If you add something to a string, it converts the thing you // are adding to a string, and then concatentates it with the string. // We do some voodoo to make sure the number is displayed reasonably. if (y==0) { return ""+x; } if (y>0) { return ""+x+"+"+y+"*I"; } // otherwise y>> 32)); hash = 83 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32)); return hash; } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.