Complete the ComplexNumber class we began in the past week's lectures, according to the UML Class diagram shown below. Note that you need to implement a new static method that computes the magnitude of a ComplexNumber (recall from you math courses that the magnitude of a complex number is simply the square root of the sum of the squares of the real and imaginary components).
Solution:
package msoe.se1011.complex; /** * This class represents a Complex Number * @author hornick * */ public class ComplexNumber { /** the real component of a complex number */ private double r; /** the imaginary component of a complex number */ private double i; /** * Constructor * @param r the real component of the complex number * @param i the imaginary component */ public ComplexNumber(double r, double i) { this.r = r; this.i = i; } /** * Add a real value to this complex number, and return the complex sum * @param r the real value to add to this complex number * @return the sum. Note: The original complex number is not modified. */ public ComplexNumber add( double r) { // create a new complex number, initially equal to this ComplexNumber, that will contain the sum ComplexNumber temp = new ComplexNumber(this.r, this.i); temp.r += r; return temp; } /** * Add a complex conjugate to this complex number, and return the complex sum * @param r the real value to be added * @param i the imaginary value to be added * @return the sum. */ public ComplexNumber add( double r, double i) { // create a new complex number, initially equal to this ComplexNumber, that will contain the sum ComplexNumber temp = new ComplexNumber(this.r,this.i); temp.i += i; // add the arguments temp.r += r; return temp; } /** * Adds a ComplexNumber to this ComplexNumber * @param x reference to the ComplexNumber to add to this ComplexNumber * @return a reference to a ComplexNumber that is the sum of the ComplexNumbers added. * Note: To assign the original ComplexNumber to the sum, use this approach: a = a.add(x); */ public ComplexNumber add( ComplexNumber x ) { ComplexNumber temp = new ComplexNumber(r,i); temp.r += x.r; temp.i += x.i; return temp; } /** * Computes the magnitude of a ComplexNumber * @param x the ComplexNumber whose magnitude is to be computed * @return the magnitude, computed as the square root of the sum of the squares of the real and imaginary components of the ComplexNumber */ public static double mag( ComplexNumber x ) { return Math.sqrt( x.r * x.r + x.i * x.i ); } @Override //this optional annotation indicates that this method is an override of a default implementation of toString() /** * override of the toString method * @return a String description of this ComplexNumber, in the format "(r,i)" */ public String toString() { return "[" + this.r + "," + this.i + "]"; } /** * Compare this ComplexNumber to another ComplexNumber for equality * @param x the ComplexNumber to compare this ComplexNumber against * @return true if both the real and imaginary components of the two ComplexNumbers are exactly equal */ public boolean equals( ComplexNumber x ) { return( this.r == x.r && this.i == x.i ); } }