001    package Numbers;
002    /**
003     * Models a complex number, in the form "a + bi".
004     * @author Chris Hamner
005     *
006     */
007    public class ComplexNumber
008    {
009            /**
010             * Constructs a new complex number.
011             * @param a - The real part of the complex number.
012             * @param b - The imaginary part of the complex number.
013             */
014            public ComplexNumber(double a, double b)
015            {
016                    this.real=a;
017                    this.imag=b;
018            }
019            /**
020             * Constructs a complex number based on another complex number.
021             * @param cn - The other complex number.
022             */
023            public ComplexNumber(ComplexNumber cn)
024            {
025                    this.real=cn.getReal();
026                    this.imag=cn.getImag();
027            }
028            /**
029             * Sets the real part of the complex number.
030             * @param a - value for real part.
031             */
032            public void setReal(double a)
033            {
034                    this.real = a;
035            }
036            /** Returns the real part of the complex number. */
037            public double getReal()
038            {
039                    return this.real;
040            }
041            /**
042             * Sets the imaginary part of the complex number.
043             * @param a - value for imaginary part.
044             */
045            public void setImag(double a)
046            {
047                    this.imag = a;
048            }
049            /** Returns the imaginary part of the complex number. */
050            public double getImag()
051            {
052                    return this.imag;
053            }
054            /**
055             * Multiplies 2 complex numbers.
056             * @param cn1 - the second complex number.
057             * @return - the solution.
058             */
059            public ComplexNumber multiply(ComplexNumber cn1)
060            {
061                    double a, b, c, d, e, f;
062                    a = this.real;
063                    b = this.imag;
064                    c = cn1.real;
065                    d = cn1.imag;
066                    e = (a*c - b*d);
067                    f = (a*d + b*c);
068                    ComplexNumber answer = new ComplexNumber(e,f);
069                    return answer;
070            }
071            /**
072             * Divides 2 complex numbers.
073             * @param cn1 - the second complex number.
074             * @return - the solution.
075             */
076            public ComplexNumber divide(ComplexNumber cn1)
077            {
078                    double a, b, c, d, e, f;
079                    a = this.real;
080                    b = this.imag;
081                    c = cn1.real;
082                    d = cn1.imag;
083                    e = (a*c + b*d)/(c*c + d*d);
084                    f = (b*c - a*d)/(c*c + d*d);
085                    ComplexNumber answer = new ComplexNumber(e,f);
086                    return answer;
087            }
088            /**
089             * Subtracts 2 complex numbers.
090             * @param cn1 - the second complex number.
091             * @return - the solution.
092             */
093            public ComplexNumber subtract(ComplexNumber cn1)
094            {
095                    double a, b, c, d, e, f;
096                    a = this.real;
097                    b = this.imag;
098                    c = cn1.real;
099                    d = cn1.imag;
100                    e = (a-c);
101                    f = (b-d);
102                    ComplexNumber answer = new ComplexNumber(e,f);
103                    return answer;
104            }
105            /**
106             * Adds 2 complex numbers.
107             * @param cn1 - the second complex number.
108             * @return - the solution.
109             */
110            public ComplexNumber add(ComplexNumber cn1)
111            {
112                    double a, b, c, d, e, f;
113                    a = this.real;
114                    b = this.imag;
115                    c = cn1.real;
116                    d = cn1.imag;
117                    e = (a+c);
118                    f = (b+d);
119                    ComplexNumber answer = new ComplexNumber(e,f);
120                    return answer;
121            }
122            /**
123             * Multiplies a complex number by a regular number.
124             * @param x - regular number by which the complex number will be multiplied.
125             */
126        public void multiply(double x) 
127        {
128            this.setReal(this.getReal() * x);
129            this.setImag(this.getImag() * x);
130        }
131        /**
132         * Takes the square root of a complex number.
133         * Tom wrote this method, I did not. Thank you Tom. All credit goes to Tom.
134         * Did I mention that Tom wrote this method?
135         * Thanks Tom.
136         */
137        public void squareRoot()
138        {
139            double a = this.getReal();
140            double b = this.getImag();
141            if (b == 0) 
142            {
143            this.setReal(Math.sqrt(a));
144            }
145            else 
146            {
147            double y = Math.sqrt((Math.sqrt(a*a+b*b)-a)/2);
148            double x = b/(2*y);
149            this.setReal(x);
150            this.setImag(y);
151            }
152        }
153            /**
154             * A string which returns the real and imaginary parts of the complex number.
155             */
156            public String toString()
157            {
158                    return "The real part of the complex number is " + getReal() + ", and the imaginary part is " + getImag() +".";
159            }
160            /** real part of the complex number */
161            private double real;
162            /** imaginary part of the complex number */
163            private double imag;
164    }