001 package classwork;
002 /**
003 * Models a complex number.
004 * @author Tom James
005 *
006 */
007 public class ComplexNumber {
008 /**
009 * The real part of the complex number.
010 */
011 private double realPart;
012 /**
013 * The imaginary part of the complex number.
014 */
015 private double imagPart;
016
017 /**
018 * Constructs a complex number by taking a and b from a+bi.
019 * @param a The real part of the complex number.
020 * @param b The imaginary part of the complex number.
021 */
022 public ComplexNumber(double a,double b) {
023 this.realPart = a;
024 this.imagPart = b;
025 }
026
027 /**
028 * Constructs a complex number using another complex number.
029 * @param cn Another complex number.
030 */
031 public ComplexNumber(ComplexNumber cn) {
032 this.realPart = cn.getRealPart();
033 this.imagPart = cn.getImagPart();
034 }
035
036 /**
037 * @return The real part of the complex number.
038 */
039 public double getRealPart() {
040 return this.realPart;
041 }
042
043 /**
044 * @return The imaginary part of the complex number.
045 */
046 public double getImagPart() {
047 return this.imagPart;
048 }
049
050 /**
051 * @param a The real part of the complex number.
052 */
053 private void setRealPart(double a) {
054 this.realPart = a;
055 }
056
057 /**
058 * @param a The imaginary part of the complex number.
059 */
060 private void setImagPart(double a) {
061 this.imagPart = a;
062 }
063
064 /**
065 * @param cn1 Another complex number.
066 * @return The result of the addition.
067 */
068 public ComplexNumber add(ComplexNumber cn1) {
069 double a,b,c,d,e,f;
070 a = cn1.realPart;
071 b = cn1.imagPart;
072 c = this.realPart;
073 d = this.imagPart;
074 e = (a + c);
075 f = (b + d);
076 ComplexNumber result = new ComplexNumber(e,f);
077 return result;
078 }
079
080 /**
081 * @param cn1 Another complex number.
082 * @return The result of the subtration.
083 */
084 public ComplexNumber subtract(ComplexNumber cn1) {
085 double a,b,c,d,e,f;
086 a = cn1.realPart;
087 b = cn1.imagPart;
088 c = this.realPart;
089 d = this.imagPart;
090 e = (c - a);
091 f = (d - b);
092 ComplexNumber result = new ComplexNumber(e,f);
093 return result;
094 }
095
096 /**
097 * @param cn1 Another complex number.
098 * @return The result of the multiplication.
099 */
100 public ComplexNumber multiply(ComplexNumber cn1) {
101 double a,b,c,d,e,f;
102 a = cn1.realPart;
103 b = cn1.imagPart;
104 c = this.realPart;
105 d = this.imagPart;
106 e = (a*c - b*d);
107 f = (a*d + b*c);
108 ComplexNumber result = new ComplexNumber(e,f);
109 return result;
110 }
111
112 /**
113 * Multiplies the complex number by a constant.
114 * @param x The constant by which the complex number is multiplied.
115 */
116 public void multiply(double x) {
117 this.setRealPart(this.getRealPart() * x);
118 this.setImagPart(this.getImagPart() * x);
119 }
120
121 /**
122 * @param cn1 Another complex number.
123 * @return The result of the division.
124 */
125 public ComplexNumber divide(ComplexNumber cn1) {
126 double a,b,c,d,e,f;
127 c = cn1.realPart;
128 d = cn1.imagPart;
129 a = this.realPart;
130 b = this.imagPart;
131 e = (a*c + b*d)/(c*c + d*d);
132 f = (b*c - a*d)/(c*c + d*d);
133 ComplexNumber result = new ComplexNumber(e,f);
134 return result;
135 }
136
137 /**
138 * Takes the square root of the complex number.
139 */
140 public void squareRoot() {
141 double a = this.getRealPart();
142 double b = this.getImagPart();
143 if (b == 0) {
144 this.setRealPart(Math.sqrt(a));
145 }
146 else {
147 double y = Math.sqrt((Math.sqrt(a*a+b*b)-a)/2);
148 double x = b/(2*y);
149 this.setRealPart(x);
150 this.setImagPart(y);
151 }
152 }
153 }