Just a Java Code that needs to be coded. Your given the design, just needs the methods and such to be fixed. This is for a Complex Number class.
/**
* ComplexNumber models a complex number expressed
* in rectangular form (real and imaginary parts).
* It is an immutable object.
*
*/
public class ComplexNumber {
//Instance variable declarations
/**
* Construct a ComplexNumber given its
* real and imaginary parts.
* @param re The real component
* @param im The imaginary component
*/
public ComplexNumber(double re, double im) {
//Initialize the instance variables
}
/**
* Returns the real component.
* @return the real
*/
public double getReal() {
return 0.0; //A stub: to be fixed
}
/**
* Returns the imaginary component.
* @return the imaginary
*/
public double getImaginary() {
return 0.0; //A stub: to be fixed
}
/**
* Returns a new ComplexNumber number that is
* the negative of this. Note: the
* original ComplexNumber is NOT
* modified.
* @return -this
*/
public ComplexNumber negate() {
return null; //A stub: to be fixed
}
/**
* Returns a new ComplexNumber that is the
* sum of this and z.
* Note: the original ComplexNumber is
* NOT modified.
* @param z
* @return this + z
*/
public ComplexNumber add(ComplexNumber z) {
return null; //A stub: to be fixed
}
/**
* Returns a new ComplexNumber that is the
* difference of this and z.
* Note: the original ComplexNumber is
* NOT modified.
* @param z
* @return this + z
*/
public ComplexNumber subtract(ComplexNumber z) {
return null; //A stub: to be fixed
}
/**
* Returns a new ComplexNumber that is the
* product of this and z.
* Note: the original ComplexNumber is
* NOT modified.
* @param z
* @return this * z
*/
public ComplexNumber multiply(ComplexNumber z) {
return null; //A stub: to be fixed
}
/**
* Returns a new ComplexNumber that is
* the reciprocal of this.
* Note: the original ComplexNumber is
* NOT modified.
* @return 1.0 / this
*/
public ComplexNumber reciprocal() {
return null; //A stub: to be fixed
}
/**
* Returns a new ComplexNumber that is
* this divided by z.
* Note: the original ComplexNumber is
* NOT modified.
* @param z
* @return this / z
*/
public ComplexNumber divide(ComplexNumber z) {
return null; //A stub: to be fixed
}
/**
* Returns a String representation of
* this in the format:
*
* real +-(optional) i imaginary
*
* If the imaginary part is zero, only the
* real part is converted to a String.
* A "+" or "-" is placed between the real
* and imaginary parts depending on the
* the sign of the imagrinary part.
*
* Examples:
*
* ..println(new ComplexNumber(0,0); -> "0.0"
* ..println(new ComplexNumber(1,1); -> "1.0 + i1.0"
* ..println(new ComplexNumber(1,-1); -> "1.0 - i1.0"
*
* @return the String representation.
*/
@Override
public String toString() {
//DO NOT MODIFY THIS CODE!
String str = "" + this.getReal();
if (this.getImaginary() == 0.0) {
return str;
}
if (this.getImaginary() > 0) {
return str + " + i" + this.getImaginary();
} else {
return str + " - i" + -this.getImaginary();
}
}
}