Write a Java class Complex for dealing with complex number. Your class must have the following features:
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
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;
}
}
Comments
Leave a comment