Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a class named ComplexNumber in order to abstract the complex numbers. Com

ID: 3675858 • Letter: D

Question

Define a class named

ComplexNumber

in order to abstract the complex numbers.

Complex numbers have a real and an imaginary part, both of which to be represented by

double type in your class

definition. Define

add

and

subtract

methods which both

take an object of type

ComplexNumber

and return the

this

reference after performing

the addition or subtraction. Also, define

static add

and

subtract

functions that take

two

ComplexNumber

objects and r

eturn a new object. Additionally define the

equals

method

that can be used to compare two complex numbers

. Define at least three

constructors; a default one, one taking two double parameters for the real and the imaginary

parts, another one taking an object of

Compl

exNumber

class as parameter

to be used to

initialize

your current object

. Have a

TestComplexNumbers

class to test each of the

methods of the

ComplexNumber

class and print out the results

(no need to take in user

input, use any numbers for the testing purposes

.

Explanation / Answer

public class ComplexNumber
{
public ComplexNumber()
{
real = 0.0;
imaginary = 0.0;
}
public ComplexNumber(double real, double imaginary)
{
this.real = real;
this.imaginary = imaginary;
}

{
set(add(this,z));
}

public void subtract(ComplexNumber z)
{
set(subtract(this,z));
}

}
public void set(ComplexNumber z)
{
this.real = z.real;
this.imaginary = z.imaginary;
}
public static ComplexNumber add(ComplexNumber z1, ComplexNumber z2)
{
return new ComplexNumber(z1.real + z2.real, z1.imaginary + z2.imaginary);
}

public static ComplexNumber subtract(ComplexNumber z1, ComplexNumber z2)
{
return new ComplexNumber(z1.real - z2.real, z1.imaginary - z2.imaginary);
}

public String toString()
{
String re = this.real+"";
String im = "";
if(this.imaginary < 0)
im = this.imaginary+"i";
else
im = "+"+this.imaginary+"i";
return re+im;
}
public static ComplexNumber pow(ComplexNumber z, int power)
{
ComplexNumber output = new ComplexNumber(z.getRe(),z.getIm());
for(int i = 1; i < power; i++)
{
double _real = output.real*z.real - output.imaginary*z.imaginary;
double _imaginary = output.real*z.imaginary + output.imaginary*z.real;
output = new ComplexNumber(_real,_imaginary);
}
return output;
}
public double getRe()
{
return this.real;
}
public double getIm()
{
return this.imaginary;
}
public double getArg()
{
return Math.atan2(imaginary,real);
}
public static ComplexNumber parseComplex(String s)
{
s = s.replaceAll(" ","");
ComplexNumber parsed = null;
if(s.contains(String.valueOf("+")) || (s.contains(String.valueOf("-")) && s.lastIndexOf('-') > 0))
{
String re = "";
String im = "";
s = s.replaceAll("i","");
s = s.replaceAll("I","");
if(s.indexOf('+') > 0)
{
re = s.substring(0,s.indexOf('+'));
im = s.substring(s.indexOf('+')+1,s.length());
parsed = new ComplexNumber(Double.parseDouble(re),Double.parseDouble(im));
}
else if(s.lastIndexOf('-') > 0)
{
re = s.substring(0,s.lastIndexOf('-'));
im = s.substring(s.lastIndexOf('-')+1,s.length());
parsed = new ComplexNumber(Double.parseDouble(re),-Double.parseDouble(im));
}
}
else
{
// Pure imaginary number
if(s.endsWith("i") || s.endsWith("I"))
{
s = s.replaceAll("i","");
s = s.replaceAll("I","");
parsed = new ComplexNumber(0, Double.parseDouble(s));
}
// Pure real number
else
{
parsed = new ComplexNumber(Double.parseDouble(s),0);
}
}
return parsed;
}
@Override
public final boolean equals(Object z)
{
if (!(z instanceof ComplexNumber))
return false;
ComplexNumber a = (ComplexNumber) z;
return (real == a.real) && (imaginary == a.imaginary);
}
public ComplexNumber inverse()
{
return divide(new ComplexNumber(1,0),this);
}
public String format(int format_id) throws IllegalArgumentException
{
String out = "";
if(format_id == XY)
out = toString();
else if(format_id == RCIS)
{
out = mod()+" cis("+getArg()+")";
}
else
{
throw new IllegalArgumentException("Unknown Complex Number format.");
}
return out;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote