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

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

ID: 3731212 • Letter: 7

Question

7. 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 -I. 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 constructor that has only a single parameter of type double call this parameter realPart and define the constructor so that the object will be initialized to realPart 0*i. Also include a no-argument constructor that initializes an object to 0 (that is, to o ). 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) cl 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 multiplication. 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 Alternate Part Two: Add a second version of the methods for addition, subtraction, 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,

Explanation / Answer

Complex.java

public class Complex {

private double real;

private double imaginary;

public Complex() {

this.real = 0;

this.imaginary = 0;

}

public Complex(double real) {

this.real = real;

this.imaginary = 0;

}

public Complex(double real, double imaginary) {

this.real = real;

this.imaginary = imaginary;

}

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;

}

public static Complex add(Complex c1,Complex c2)

{

Complex comp = new Complex();

comp.real = c1.real+ c2.real;

comp.imaginary = c1.imaginary + c2.imaginary;

return comp;

}

public static Complex sub(Complex c1,Complex c2)

{

Complex comp = new Complex();

comp.real = c1.real- c2.real;

comp.imaginary = c1.imaginary - c2.imaginary;

return comp;

}

public static Complex mul(Complex c1,Complex c2)

{

Complex comp = new Complex();

comp.real = c1.real * c2.imaginary;

comp.imaginary = c1.real * c2.imaginary;

return comp;

}

public static Complex div(Complex c1,Complex c2)

{

Complex comp=new Complex();

comp.real=((c1.real*c2.real)+(c1.imaginary*c2.imaginary))/((c2.real*c2.real)+(c2.imaginary+c2.imaginary));

comp.imaginary=((c1.imaginary*c2.real)-(c1.real*c2.imaginary))/((c2.real*c2.real)+(c2.imaginary+c2.imaginary));

return comp;

}

public Complex mul(Complex c)

{

Complex comp = new Complex();

comp.real = real * c.imaginary;

comp.imaginary =real * c.imaginary;

return comp;

}

public Complex div(Complex c)

{

Complex comp=new Complex();

comp.real=((real*c.real)+(imaginary*c.imaginary))/((c.real*c.real)+(c.imaginary+c.imaginary));

comp.imaginary=((imaginary*c.real)-(real*c.imaginary))/((c.real*c.real)+(c.imaginary+c.imaginary));

return comp;

}

public Complex add(Complex c)

{

Complex comp = new Complex();

comp.real = real+ c.real;

comp.imaginary = imaginary + c.imaginary;

return comp;

}

public Complex sub(Complex c)

{

Complex comp = new Complex();

comp.real = real- c.real;

comp.imaginary = imaginary - c.imaginary;

return comp;

}

public boolean equals(Complex other) {

if (Double.doubleToLongBits(imaginary) != Double

.doubleToLongBits(other.imaginary))

return false;

if (Double.doubleToLongBits(real) != Double

.doubleToLongBits(other.real))

return false;

return true;

}

public String toString()

{

return "("+real+(imaginary<0.0? "-":"+")+(imaginary<0.0?-imaginary:imaginary)+"i)";

}

}

_____________________

Test.java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

double a,b,c,d;

Scanner sc = new Scanner(System.in);

//Getting the First Complex number entered by the user

System.out.print("Enter complex number#1(real imaginary):");

a = sc.nextDouble();

b = sc.nextDouble();

//Creating the Complex numbers class object

Complex c1=new Complex(a,b);

  

//Getting the Second Complex number entered by the user

System.out.print("Enter complex number#2(real imaginary):");

c = sc.nextDouble();

d = sc.nextDouble();

  

//Creating the Complex numbers class object

Complex c2=new Complex(c,d);

  

  

//Displaying the two complex numbers

System.out.println("Complex Number#1: "+c1.toString());

System.out.println(" Complex Number#2: "+c2.toString());

  

//Adding two complex numbers by calling the add() method on the Complex Class object

Complex c3=c1.add(c2);

System.out.println(" Addition of "+c1.toString()+" and "+c2.toString()+" is "+c3.toString());

  

//Subtracting two complex numbers by calling the subtract() method on the Complex Class object

Complex c4=c1.sub(c2);

System.out.println(" Subtraction of "+c1.toString()+" and "+c2.toString()+" is "+c4.toString());

  

//Multiplying two complex numbers by calling the multiply() method on the Complex Class object

Complex c5=c1.mul(c2);

System.out.println(" Multiplication of "+c1.toString()+" and "+c2.toString()+" is "+c5.toString());

Complex c6=c1.div(c2);

System.out.println(" Division of "+c1.toString()+" and "+c2.toString()+" is "+c6.toString());

}

}

____________________

Output:

Enter complex number#1(real imaginary):7 4
Enter complex number#2(real imaginary):5 2
Complex Number#1: (7.0+4.0i)

Complex Number#2: (5.0+2.0i)

Addition of (7.0+4.0i) and (5.0+2.0i) is (12.0+6.0i)

Subtraction of (7.0+4.0i) and (5.0+2.0i) is (2.0+2.0i)

Multiplication of (7.0+4.0i) and (5.0+2.0i) is (14.0+14.0i)

Division of (7.0+4.0i) and (5.0+2.0i) is (1.4827586206896552+0.20689655172413793i)

_____________Thank You

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