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

Write a Java class which can be used to instantiate Complex number objects, and

ID: 3537279 • Letter: W

Question

Write a Java class which can be used to instantiate Complex number objects, and then use Javadoc utility to generate API documentation for this Complex class.


Here are the specifications you must use with no exception for the Complex class members:


private double real --stores the real part of this

private double imaginary --stores the coefficient of the imaginary part of this

public Complex(double x, double y) -- constructor which initializes a new complex x + yi

public Complex(double x) --constructor which which initializes a new complex x + 0.0i

public Complex() --default constructor, which initializes a new complex 0.0 + 0.0i

public String toString() --returns a string representation of this complex number

public boolean equals(Complex rhs) --returns true is this equals rhs, otherwise returns false

public void setComplex(double x, double y) --sets the values of real to x and imaginary to y

public void setReal(double x) --sets the value of real to x

public void setImaginary(double y) --sets the value of imaginary to y

public double getReal() --returns the value of real

public double getImaginary() --returns the value of imaginary

public static Complex add(Complex c1, Complex c2) --returns a new complex number representing c1 + c2

public static Complex subtract(Complex c1, Complex c2) --returns a new complex number representing c1 - c2

public static Complex times(Complex c1, Complex c2) --returns a new complex number representing c1 * c2

public static Complex divide(Complex c1, Complex c2) --returns a new complex number representing c1 / c2


The arithmetic operations for complex numbers are defined mathematically as follows:


c1 + c2 =(real1 + real2) + (imaginary1 + imaginary2)i

c1 - c2 =(real1 - real2) + (imaginary1 - imaginary2)i

c1 * c2 =(real1*real2 - imaginary1 * imaginary2) + (real2*imaginary1 + real1* imaginary2)i

c1 / c2 =((real1*real2 + imaginary1 * imaginary2) / (real2^2 + imaginary2^2)) + ((real2*imaginary1 - real1* imaginary2)/ (real2^2+ imaginary2^2))i


Notice that the arithmetic operations (unlike in the RationalNumber class) are "static" methods. Therefore, in order to invoke them we don't need a controlling object. We use the class name instead:

Complex t = new Complex(1.5, 2.3);

Complex u = new Complex(6).

Complex v = Complex.add(t, u); // instead of t.add(u)


To test your complex numbers, we need a test program like the following:


public class TestComplex {

public static void main(String[] args) {

Complex c1 = new Complex(5.3, 2.0); // test 2-arg constructor

Complex c2 = new Complex(-6.0, -4.3); // test negative

Complex c3 = new Complex(7.556); // test 1-arg constructor

Complex c4 = new Complex(0.0, -3.112);// test 0 real part

Complex c5 = new Complex(); // test 0-arg constructor

System.out.println("c1 = " + c1); // test toString()

System.out.println("c2 = " + c2);

System.out.println("c3 = " + c3);

System.out.println("c4 = " + c4);

System.out.println("c5 = " + c5);

// test accessors

System.out.println("c1 is " + c1.getReal() +

" + " + c1.getImaginary() + "i");

c1.setComplex(20.0, 50.0); // test mutators

System.out.println("c1 = " + c1);

c1.setReal(9.2);

c1.setImaginary(5.9);

System.out.println("c1 = " + c1);

// test arithmetic

System.out.println("c1 + c2 = " + Complex.add(c1, c2));

System.out.println("c1 - c2 = " + Complex.subtract(c1, c2));

System.out.println("c1 * c2 = " + Complex.times(c1, c2));

System.out.println("c1 / c2 = " + Complex.divide(c1, c2));

} // main

}//TestComplex Class


If you look at the code, you can see that all the Complex class members are being tried out. The output from the test is:


c1 = 5.3 + 2.0i

c2 = -6.0 - 4.3i

c3 = 7.556

c4 = -3.112i

c5 = 0.0

c1 is 5.3 + 2.0i

c1 = 20.0 + 50.0i

c1 = 9.2 + 5.9i

c1 + c2 = 3.1999999999999993 + 1.6000000000000005i

c1 - c2 = 15.2 + 10.2i

c1 * c2 = -29.829999999999995 - 74.96000000000001i

c1 / c2 = -1.4786199302624334 + 0.0763442833547438i


Add Javadoc comments with appropriate tags (i.e. @version, @author, @param and @return) to your Complex.java file.Then use Javadoc utility (available from Eclipse, under Project Menu -> Generate Javadoc ..) to generate API documentation for Complex class.


Explanation / Answer


public class Complex {
    private double real   ;//stores the real part of this

    private double imaginary   ;//stores the coefficient of the imaginary part of this

    ;// constructor which initializes a new complex x + yi

    public Complex(double x, double y){
        this.real = x;
        this.imaginary = y;
    }
    //constructor which which initializes a new complex x + 0.0i
    public Complex(double x) {
        this.real = x;
        this.imaginary = 0;
    }
   
    ;//default constructor, which initializes a new complex 0.0 + 0.0i

    public Complex() {
        this.real = 0;
        this.imaginary = 0;
    }
    ;//returns a string representation of this complex number
    public String toString() {
        return this.real + "+i" + this.imaginary;
    }
    //returns true is this equals rhs, otherwise returns false
    public boolean equals(Complex rhs){
        if(this.real == rhs.real && this.imaginary == rhs.imaginary)
            return true;
        else
            return false;
    }

    public void setComplex(double x, double y){
        this.real = x;
        this.imaginary = y;
    }
    //sets the value of real to x
    public void setReal(double x) {
        this.real = x;
    }
    //sets the value of imaginary to y
    public void setImaginary(double y){
        this.imaginary = y;
    }
    //returns the value of real
    public double getReal() {
        return this.real;
    }
    //returns the value of imaginary
    public double getImaginary() {
        return this.imaginary;
    }
    //returns a new complex number representing c1 + c2
    public static Complex add(Complex c1, Complex c2){
        Complex result = new Complex();
        result.setReal( c1.real + c2.real);
        result.setImaginary(c1.imaginary + c2.imaginary);
        return result;
    }
    //returns a new complex number representing c1 - c2
    public static Complex subtract(Complex c1, Complex c2){
        Complex result = new Complex();
        result.setReal( c1.real - c2.real);
        result.setImaginary(c1.imaginary - c2.imaginary);
        return result;
    }
    //returns a new complex number representing c1 * c2
    public static Complex times(Complex c1, Complex c2) {
        Complex result = new Complex();
        result.setReal( (c1.real * c2.real - c1.imaginary * c2.imaginary) );
        result.setImaginary(c2.real * c1.imaginary + c1.real * c2.imaginary);
       
        return result;
    }
    //returns a new complex number representing c1 / c2
    public static Complex divide(Complex c1, Complex c2) {
        Complex result = new Complex();
        result.setReal( ((c1.real * c2.real + c1.imaginary * c2.imaginary) / (Math.pow(c2.real, 2) + Math.pow(c2.imaginary,2))) );
        result.setImaginary(((c2.real * c1.imaginary - c1.real * c2.imaginary)/ (Math.pow(c2.real, 2)+ Math.pow(c2.imaginary, 2))));
       
        return result;
       
       
    }

     
}




package complex;
public class TestComplex {

    public static void main(String[] args) {

    Complex c1 = new Complex(5.3, 2.0); // test 2-arg constructor

    Complex c2 = new Complex(-6.0, -4.3); // test negative

    Complex c3 = new Complex(7.556); // test 1-arg constructor

    Complex c4 = new Complex(0.0, -3.112);// test 0 real part

    Complex c5 = new Complex(); // test 0-arg constructor

    System.out.println("c1 = " + c1); // test toString()

    System.out.println("c2 = " + c2);

    System.out.println("c3 = " + c3);

    System.out.println("c4 = " + c4);

    System.out.println("c5 = " + c5);

    // test accessors

    System.out.println("c1 is " + c1.getReal() +

    " + " + c1.getImaginary() + "i");

    c1.setComplex(20.0, 50.0); // test mutators

    System.out.println("c1 = " + c1);

    c1.setReal(9.2);

    c1.setImaginary(5.9);

    System.out.println("c1 = " + c1);

    // test arithmetic

    System.out.println("c1 + c2 = " + Complex.add(c1, c2));

    System.out.println("c1 - c2 = " + Complex.subtract(c1, c2));

    System.out.println("c1 * c2 = " + Complex.times(c1, c2));

    System.out.println("c1 / c2 = " + Complex.divide(c1, c2));

    } // main

    }//TestComplex Class


output:

c1 = 5.3+i2.0
c2 = -6.0+i-4.3
c3 = 7.556+i0.0
c4 = 0.0+i-3.112
c5 = 0.0+i0.0
c1 is 5.3 + 2.0i
c1 = 20.0+i50.0
c1 = 9.2+i5.9
c1 + c2 = 3.1999999999999993+i1.6000000000000005
c1 - c2 = 15.2+i10.2
c1 * c2 = -29.829999999999995+i-74.96000000000001
c1 / c2 = -1.4786199302624334+i0.0763442833547438


to generate javadoc from eclipse goto project->click on generate javadoc option and set command to javadoc.exe. This generates the java doc.


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