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

Part One: Define a class for complex numbers. A complex number is a number of th

ID: 3533182 • Letter: P

Question

Part One: Define a class for complex numbers. A complex number is a number of the form

a + b*i

For our purposes, a and b are numbers of type double, and i is a number that

represents the quantity 1-1. Represent a complex number as two values of type double. Name the instance variables real and imaginary. (The instance variable

for the number that is multiplied by i is the one called imaginary.) Call the class Complex. Include a constructor with two parameters of type double that can be used to set the instance variables of an object to any values. Also include a construc- tor that has only a single parameter of type double; call this parameter realPart

and define the constructor so that the object will beinitialized to real Part + 0*i. Also include a no-argument constructor that initializes an object to 0 (that is, to 0 + 0*i ). Define accessor and mutator methods as well as the methods equals and toString. Define static methods for addition, subtraction, and multiplication of objects of your class Complex. These methods should be static and should each have two parameters of type Complex and return a value of type Complex. For example, Complex.add(c1, c2) will return the result of adding the two complex numbers (two objects of the class Complex) c1 and c2. Also write a test program to test your class.

Hints: To add or subtract two complex numbers, you add or subtract the two instance variables of type double. The product of two complex numbers is given by the following formula:

Part Two: Add a second version of the methods for addition, subtraction, and mul- tiplication. These methods should have the same names as the static version but should use a calling object and a single argument. For example, this version of the add method (for addition) has a calling object and one argument. So c1.add(c2) returns the result of adding the complex numbers c1 and c2. Note that your class should have all these methods; for example, there should be two methods named add.

(OPTIONAL) Alternate Part Two: Add a second version of the methods for addition, subtrac- tion, and multiplication. These methods should have the same names as the static version but should use a calling object and a single argument. The methods will be void methods. The result is given as the changed value of the calling object. For example, this version of the add method (for addition) has a calling object and one argument. Therefore,

c1.add(c2);

changes the values of the instance variables of c1 so they represent the result of adding c2 to the original version of c1. Note that your class should have all these methods; for example, there should be two methods named add.

(If you want to do both Part Two and Alternate Part Two, they must be two classes. You cannot include the methods from both Part Two and Alternate Part Two in a single class. Do you know why?)



Anything that is not in java will not be considered for points

Explanation / Answer

Complex.java



public class Complex

{

public double real;

public double imaginary;

public Complex(double r,double i)

{

this.real=r;

this.imaginary=i;

}

public Complex(double realPart)

{

this.real=realPart;

this.imaginary=0;

}

public Complex()

{

this.real=0;

this.imaginary=0;

}

public boolean equals(Complex e)

{

if(this.real==e.real && this.imaginary==e.imaginary)

{

return true;

}

else

{

return false;

}

}

public String toString()

{

return this.real+" + "+this.imaginary+"i";

}

public static Complex add(Complex c1,Complex c2)

{

double real=c1.real+c2.real;

double img=c1.imaginary+c2.imaginary;

return new Complex(real, img);

}

public static Complex subtract(Complex c1,Complex c2)

{

double real=c1.real-c2.real;

double img=c1.imaginary-c2.imaginary;

return new Complex(real, img);

}

public static Complex multiply(Complex c1,Complex c2)

{

double r1=c1.real;

double r2=c2.real;

double i1=c1.imaginary;

double i2=c2.imaginary;

double r=r1*r2-i1*i2;

double i=r1*i2+r2*i1;

return new Complex(r, i);

}

public Complex add(Complex c)

{

double real=this.real+c.real;

double img=this.imaginary+c.imaginary;

return new Complex(real, img);

}

public Complex subtract(Complex c)

{

double real=this.real-c.real;

double img=this.imaginary-c.imaginary;

return new Complex(real, img);

}

public Complex multiply(Complex c)

{

double r1=this.real;

double r2=c.real;

double i1=this.imaginary;

double i2=c.imaginary;

double r=r1*r2-i1*i2;

double i=r1*i2+r2*i1;

return new Complex(r, i);

}


public double getReal() {

return real;

}


public void setReal(double real) {

this.real = real;

}


public double getImaginary() {

return imaginary;

}


public void setImaginary(double imaginary) {

this.imaginary = imaginary;

}

}