Answer to Question #261254 in Java | JSP | JSF for bikki

Question #261254

Write a Java class Complex for dealing with complex number. Your class must have the following features:

  • Instance variables :
  • realPart for the real part of type double
  • imaginaryPart for imaginary part of type double.
  • Constructor:

o   Having no parameter – for setting values to zero or null.

o   Having two parameters for assigning values to both data members.

o   Overload the above constructor and use this keyword to set the values of data members

  • Instance methods:
  • public void setRealPart (double realPart): Used to set the real part of this complex number.
  • public void setImaginaryPart (double realPart): Used to set the imaginary part of this complex number.
  • public double getRealPart(): This method returns the real part of the complex number
  • public double getImaginaryPart(): This method returns the imaginary part of the complex number
  • public String toString(): This method allows the complex number to be easily printed out to the screen
1
Expert's answer
2021-11-05T00:34:10-0400
public class Complex {
    private double realPart;
    private double imaginaryPart;

    public Complex() {
        this(0, 0);
    }

    public Complex(double realPart, double imaginaryPart) {
        this.realPart = realPart;
        this.imaginaryPart = imaginaryPart;
    }

    public void setRealPart(double realPart) {
        this.realPart = realPart;
    }

    public void setImaginaryPart(double imaginaryPart) {
        this.imaginaryPart = imaginaryPart;
    }

    public double getRealPart() {
        return realPart;
    }

    public double getImaginaryPart() {
        return imaginaryPart;
    }

    @Override
    public String toString() {
        return realPart + " " + imaginaryPart;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS