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

Create a class for working with complex numbers. Only 2 private float data membe

ID: 3805871 • Letter: C

Question

Create a class for working with complex numbers. Only 2 private float data members are needed, the real part of the complex number and the imaginary part of the complex number The following methods should be in your class: a. A default constructor that uses default arguments in case no initializers are included in the main. b. Add two complex numbers and store the sum. c. Subtract two complex numbers and store the difference. d. Multiply two complex numbers and store the product. e. Print a complex number in the form a + bi or a - bi where a is the real part of the complex number and b is the imaginary part of the complex number. f. Change a complex number to its square. Your main should instantiate two complex numbers and call each of the class methods. The two complex numbers should be printed along with the sum, difference, and product of the two complex numbers. Lastly, print the squares of the two complex numbers. (Please do not overload the operators for this program.)

Explanation / Answer


public class ComplexNumber {

   private float a,b;
  
   public ComplexNumber()
   {
       a=0;
       b=0;
   }
   public ComplexNumber(float a,float b)
   {
       this.a=a;
       this.b=b;
   }
   public String getComplexValue() {
       if (this.b < 0) {
           return a + "" + b + "i";
       } else {
           return a + "-" + b + "i";
       }
   }
   //returns the addition
   public static String addition(ComplexNumber co1, ComplexNumber co2) {
       float a1 = co1.a + co2.a;
       float b1 = co1.b + co2.b;
       if (b1 < 0) {
           return a1 + "" + b1 + "i";
       } else {
           return a1 + "+" + b1 + "i";
       }
   }
   //returns the difference
   public static String diff(ComplexNumber co1, ComplexNumber co2) {
       float a1 = co1.a - co2.a;
       float b1 = co1.b - co2.b;
      
       if (b1 < 0) {
           return a1 + "" + b1 + "i";
       } else {
           return a1 + "+" + b1 + "i";
       }
   }
   //return the product of Complex Number
   public static String multiply(ComplexNumber co1, ComplexNumber co2) {
       float a1 = (co1.a * co2.a)-(co1.b*co2.b);
       float b1 = (co1.a * co2.b)+(co1.b*co2.a);
       if (b1 < 0) {
           return a1 + "" + b1 + "i";
       } else {
           return a1 + "+" + b1 + "i";
       }
   }
   //returns the square of complex Number
   public String square(float a,float b)
   {
       if(a*b<0)
       {
           return a*a+""+a*b+"i+"+b*b+"i";
       }
       return a*a+"+"+a*b+"i+"+b*b+"i";
   }
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       ComplexNumber co1=new ComplexNumber(5,6);
       ComplexNumber co2=new ComplexNumber(-3,4);
       System.out.println("Complex Number 1:"+co1.getComplexValue());
       System.out.println("Complex Number 2:"+co2.getComplexValue());
       System.out.println("Addition of :"+co1.getComplexValue()+" and "+co2.getComplexValue()+" is:"+ ComplexNumber.addition(co1, co2));
       System.out.println("Differnce of:"+co1.getComplexValue()+" and "+co2.getComplexValue()+" is:"+ ComplexNumber.diff(co1, co2));
       System.out.println("Product of :"+co1.getComplexValue()+" and "+co2.getComplexValue()+" is:"+ ComplexNumber.multiply(co1, co2));
       System.out.println("Square of "+co1.getComplexValue()+ " is:" +co1.square(co1.a,co1.b));
       System.out.println("Square of "+co2.getComplexValue()+ " is:"+co2.square(co2.a,co2.b));
   }//end of main

}//end of class


/*******OUTPUT*********
Complex Number 1:5.0-6.0i
Complex Number 2:-3.0-4.0i
Addition of :5.0-6.0i and -3.0-4.0i is:2.0+10.0i
Differnce of:5.0-6.0i and -3.0-4.0i is:8.0+2.0i
Product of :5.0-6.0i and -3.0-4.0i is:-39.0+2.0i
Square of 5.0-6.0i is:25.0+30.0i+36.0i
Square of -3.0-4.0i is:9.0-12.0i+16.0i
*/

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