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

Hi there, thank you for taking the time to look at my problem. If you take it on

ID: 3769921 • Letter: H

Question

Hi there, thank you for taking the time to look at my problem. If you take it on, Can I ask that it be done with Java please? Can I also ask for // comments in the program explaining everything if possible as well? I will take what I can get though if not. I am having an emergency right now and since I'm still a beginner I'm not going to be able to take all the time I would need to figure this whole program out before it's due:

PROGRAM PROBLEM:

Your task is to create a class that will model a complex number. In much the way that our example class in chapter 7 was a class that modeled a Fraction.

A complex number is a number of the form a + bi, where a is the “real” part and b is the “imaginary” part. It is based on the mathematical premise that i2 = -1. So like the fraction class, you will define two data members for real and imaginary.

Your class will contain the following 17 methods:

-A constructor that takes no arguments and assigns all values to 0.

-A constructor that takes one double and assigns it to the “real” portion, 0 to the other.

-A constructor that takes two doubles, and assigns them to real, then imaginary.

- 2 accessors

- 2 mutators

-A toString( ) method, outputting a String neatly in the form a + bi

Mathematical Methods- Instance methods:

-2 different add methods, first takes one complex object as a parameter, the second takes a double (which is treated as the real portion of a complex only)

-2 subtract methods

-2 multiply methods

-2 divide methods

-An equals( ) method that takes a complex number as a parameter type and returns a boolean of true if the object is equal to the parameter.

These mathematical methods will be called like this:

                                    complex_object1.add(complex_object2);        OR

                                    complex_object1.add(double_value);

NOTES: You will also be required to create a tester file that successfully tests all your class methods. Remember: All of these mathematical methods will have only one complex number passed in as a parameter, the other is the object that is calling the method. If you get it to work but you are passing two complex objects, you are doing it wrong!

Each of these programs must be done in a separate file. Name them Complex.java, ComplexTester.java. Name the class in each file these names as well. If you do not name these files correctly you will lose points.

Complex Math:           A   +    Bi    refers to   A = real portion; B = imaginary portion

Adding: resulting complex number

A + Bi    plus a real number C      =>              (A + C)    +     (B)i

A + Bi                plus    C + Di            =>                    (A + C)     +   (B + D)i

Subtracting:                                                     resulting complex number

A + Bi    minus a real number C      =>           (A - C)    +     (B)i

A + Bi                minus    C + Di         =>                    (A - C)     +   (B - D)i

Multiplying:                                                    resulting complex number

A + Bi    times a real number C      =>            (A * C)    +     (B * C)i

A + Bi                times    C + Di          =>                    (AC - BD)     +   (AD + BC)i

Dividing:                                                         resulting complex number

A + Bi    divided by a real number C     =>   (A / C)    +     (B / C)i

A + Bi                divided by    C + Di             =>        (AC+BD)/(C2 + D2)    +   (BC-AD)/(C2 + D2)i

Note: make sure C2 + D2 is not equal to zero

Equals: means real portions of each must be the same and imaginary portions must be the same.

Explanation / Answer


/**
* The java class Complex that has constructorss
* setter , getter methods and add, subtract,multiplication
* and division.
* */
//Complex.java
public class Complex
{
   //instance variables
   private double real;
   private double imag;
  
  
   //Constructor complex
   public Complex()
   {
       real=0;
       imag=0;
   }
  
   //Parameterized constructor that takes real value
   public Complex(double real)
   {
       this.real=real;
       imag=0;
   }
  
  
   //Parameterized constructor that takes real value
   //and imaginary value
   public Complex(double real, double imag)
   {
       this.real=real;
       this.imag=imag;
   }
  
   //Set real value
   public void setReal(double real)  
   {
       this.real=real;
   }
  
   //Set imaginary value
   public void setImag(double imag)  
   {
       this.imag=imag;
   }
  
   //Get real value
   public double getReal()  
   {
       return real;
   }
  
   //Get imaginary value
   public double getImag()  
   {
       return imag;
   }
  
   //Adding a double_value to real
   public void add(double double_value)
   {
       this.real=real+double_value;      
   }
  
   //Adding a complex_objec to complex number
   public void add(Complex complex_object)
   {
       this.real=real+complex_object.getReal();
       this.imag=imag+complex_object.getImag();      
   }
  
   //Subtract complex from double_value
   public void sub(double double_value)
   {
       this.real=real-double_value;  
       this.imag=imag-double_value;
   }
  
   //Subtract complex from complex_object
   public void sub(Complex complex_object)
   {
       this.real=real-complex_object.getReal();
       this.imag=imag-complex_object.getImag();      
   }
  
   //Multiplication of real number by double value
   public void multiply(double double_value)
   {
       this.real=real*double_value;
       this.imag=imag*double_value;
   }
  
  
   //Multiplication of real number by complex value
   public void multiply(Complex complex_object)
   {
       this.real=real*complex_object.getReal()
               -imag*complex_object.getImag();
       this.imag=real*complex_object.getImag()
               +imag*complex_object.getReal();
   }
  
  
   //Division of complex number by double value
   public void divide(double double_value)
   {
       this.real=real/double_value;
       this.imag=imag/double_value;
   }
  
  
   //Division of complex number
   public void divide(Complex complex_object)
   {
       this.real=(real*complex_object.getReal()
               +imag*complex_object.getImag())
               /(complex_object.getReal()+complex_object.getReal());
       this.imag=(imag*complex_object.getReal()
               -real*complex_object.getImag())
               /(complex_object.getReal()+complex_object.getReal());;
   }
  
  
   //Returns the string representation of complex number
   @Override
   public String toString()
   {      
       return real+"+i"+imag;
   }
  
  
   //Returns true if real and imaginary
   //values
   @Override
   public boolean equals(Object obj)
   {
       Complex complex=(Complex)obj;
       return real==complex.getReal()
               && imag==complex.getImag();
   }
}//end of Complex


-----------------------------------------------------------------------

/**
* The java program that tests the methods
* of Complex class .
* The complex object calls addition, subtraction,
* multiplication and division of complex
* numbers.Print the results
*
* **/
//ComplexTester.java
public class ComplexTester
{
   public static void main(String[] args)
   {
       Complex comp1=new Complex();

       System.out.println("Complex Number");
       System.out.println("Default constructor");
       System.out.println(comp1.toString());

       Complex comp2=new Complex(5);      
       System.out.println("Single argument constructor");
       System.out.println(comp2.toString());

       Complex comp3=new Complex(5,6);      
       System.out.println("Single argument constructor");
       System.out.println(comp3.toString());

       //Addition
       comp1.add(2);
       System.out.println("Adding a double value");
       System.out.println(comp1.toString());


       //Addition of constructor
       comp1.add(comp2);
       System.out.println("Adding a complex number");
       System.out.println(comp1.toString());

       //Subtraction
       comp1.sub(2);
       System.out.println("Subtraction a double value");
       System.out.println(comp1.toString());

       //Subtraction of constructor
       comp1.sub(comp2);
       System.out.println("Subtraction a complex number");
       System.out.println(comp1.toString());

       //Multiplication
       comp1.multiply(2);
       System.out.println("Multiplication by a double value");
       System.out.println(comp1.toString());
      
       //Multiplication of constructor
       comp1.multiply(comp2);
       System.out.println("Multiplication by a complex number");
       System.out.println(comp1.toString());

       //Division
       comp1.divide(2);
       System.out.println("Division by a double value");
       System.out.println(comp1.toString());

       //Division of constructor
       comp1.divide(comp2);
       System.out.println("Division by a complex number");
       System.out.println(comp1.toString());
      
   }//end of main method
}//end of class


-----------------------------------------------------------------------

Sample Output:

Complex Number
Default constructor
0.0+i0.0
Single argument constructor
5.0+i0.0
Single argument constructor
5.0+i6.0
Adding a double value
2.0+i0.0
Adding a complex number
7.0+i0.0
Subtraction a double value
5.0+i-2.0
Subtraction a complex number
0.0+i-2.0
Multiplication by a double value
0.0+i-4.0
Multiplication by a complex number
0.0+i-20.0
Division by a double value
0.0+i-10.0
Division by a complex number
0.0+i-5.0

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