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

a) Write a class for representing complex numbers, called math.Complex. Package

ID: 3793582 • Letter: A

Question

 a) Write a class for representing complex numbers, called math.Complex.  Package name is 'math'. The real and imaginary parts must be of type double.  Use http://en.wikipedia.org/wiki/Complex_numbers as a reference.  IMPORTANT: class Complex MUST BE IMMUTABLE  The Complex class should have the following public interface: - constructor taking real and imaginary part - constructor taking only the real part (imaginary defaults to 0) - toString() function that returns the string "3 + 2i" for a Complex object Complex(3,2)     and for Complex(3, -2), the string "3 - 2i" - accessors r() for the real part and i() for the imaginary - method add() for adding two Complex numbers, returning a new Complex object - method sub() for subtracting two Complex numbers, returning a new Complex object - method conj() for computing the complex conjugate , returning a new Complex object.  - method mult() for computing the product of two complex numbers, returning a new Complex object.  - method div() for computing the division of two complex numbers, returning a new Complex object.  - method equals() that compares two Complex numbers and returns true if they are equal.   The Complex class *must be immutable* and must show a correct interface design, as we learned in Chapter 3. Write the contract for each method (preconditions/postconditions (@return)).  Write the class invariant.   Note that some preconditions are trivial (i.e. no preconditions), but some operations don't work if they involve division by 0.    b) Write a main() method that show how the methods above are used on same sample Complex objects.  c) Write JUnit unit test methods in file TestComplex.java for these Complex methods:      - equals      - add()      - sub()      - mult()      - div() 

Explanation / Answer

Complex.java

public class Complex {
   // Declaring variables
   private double x, y;

   //Parameterized constructor
   public Complex(double x, double y) {
       super();
       this.x = x;
       this.y = y;
   }

   //One argumented constructor
   public Complex(double x) {
       super();
       this.x = x;
       this.y = 0;
   }

   //Zero argumented constructor
   public Complex() {
       this.x = 0;
       this.y = 0;
   }

   //Getters
   public double getX() {
       return x;
   }

   public double getY() {
       return y;
   }

   /* This method will add the two complex numbers
   * return a Complex number class object
   */
   public Complex add(Complex c) {
       Complex comp = new Complex();
       comp.x = this.x + c.x;
       comp.y = this.y + c.y;
       return comp;
   }

   /* This method will subtract the two complex numbers
   * return a Complex number class object
   */
   public Complex sub(Complex c) {
       Complex comp = new Complex();
       comp.x = this.x - c.x;
       comp.y = this.y - c.y;
       return comp;
   }

   /* This method will multiply the two complex numbers
   * return a Complex number class object
   */
   public Complex mult(Complex c) {
       Complex comp = new Complex();
       comp.x = this.x * c.x;
       comp.y = this.y * c.y;
       return comp;
   }

   /* This method will divide the two complex numbers
   * return a Complex number class object
   */
   public Complex div(Complex c) {
       double denominator=Math.pow(c.mod(),2);
   return new Complex((x*c.getX()+y*c.getY())/denominator,(y*c.getX()-x*c.getY())/denominator);
      
   }
public double mod() {
if (x!=0 || y!=0) {
return Math.sqrt(x*x+y*y);
} else {
return 0.0;
}
}

   /* This method will find the conjugate of a complex number
   * return a Complex number class object
   */
   public Complex conjugate() {
       Complex comp = new Complex();
       comp.x = this.x;
       comp.y = -(this.y);
       return comp;
   }

   //This method will check whether the two complex numbers are equal or not
   public boolean equals(Complex c) {
       if (this.x == c.getX() && this.y == c.getY())
           return true;
       else
           return false;
   }

   /*
   * toString() method is used to display the contents of an object inside it
   */
   @Override
   public String toString() {

       if (getX() == 0)
           return getY() + "i";
       else if (getY() == 0)
           return getX() + "";

       else if (getY() < 0)
           return x + "-" + (-y) + "i";
       else
           return x + "+" + y + "i";
   }

}

____________________

TestComplex.java

public class TestComplex {

   public static void main(String[] args) {
       //Creating the two Complex numbers class obejcts
       Complex c1=new Complex(3,4);
       Complex c2=new Complex(6,8);
      
       //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 sub() 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 mult() method on the Complex Class object
       Complex c5=c1.mult(c2);
       System.out.println(" Multiplication of "+c1.toString()+" and "+c2.toString()+" is "+c5.toString());
      
      
       //Dividing two complex numbers by calling the div() method on the Complex Class object
       Complex c6=c1.div(c2);
       System.out.println(" Division of "+c1.toString()+" and "+c2.toString()+" is "+c6.toString());
      
       //finding the conjugate of a complex number by calling conjugate() method on the Complex Class object
       Complex c7=c1.conjugate();
       System.out.println(" The conjugate of "+c1.toString()+" is "+c7.toString());
       Complex c8=c2.conjugate();
       System.out.println(" The conjugate of "+c2.toString()+" is "+c8.toString());
      
       Complex c9=new Complex(3,4);
       System.out.println(" Complex Number#9 :"+c9.toString());
       //Checking whether the two comlex numbers are equla or not
       Boolean bool=c9.equals(c1);
       if(bool)
           System.out.println("The two complex numbers "+c1.toString()+" and "+c9.toString()+" are equal");
       else
           System.out.println("The two complex numbers "+c1.toString()+" and "+c9.toString()+" are not equal");
      

   }

}

______________________

output:

Complex Number#1: 3.0+4.0i

Complex Number#2: 6.0+8.0i

Addition of 3.0+4.0i and 6.0+8.0i is 9.0+12.0i

Subtraction of 3.0+4.0i and 6.0+8.0i is -3.0-4.0i

Multiplication of 3.0+4.0i and 6.0+8.0i is 18.0+32.0i

Division of 3.0+4.0i and 6.0+8.0i is 0.5

The conjugate of 3.0+4.0i is 3.0-4.0i

The conjugate of 6.0+8.0i is 6.0-8.0i

Complex Number#9 :3.0+4.0i
The two complex numbers 3.0+4.0i and 3.0+4.0i are equal

__________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