001 package classwork;
002 /**
003 * Models a quadratic equation in the form ax^2 + bx + c = 0.
004 * @author Tom James
005 *
006 */
007 public class QuadtraticEquation {
008 /**
009 * Constructs a quadratic equation by taking three complex numbers in the forms a+bi, c+di, and e+fi.
010 * @param a The real part of the first complex number.
011 * @param b The imaginary part of the first complex number.
012 * @param c The real part of the second complex number.
013 * @param d The imaginary part of the second complex number.
014 * @param e The real part of the third complex number.
015 * @param f The imaginary part of the third complex number.
016 */
017 public QuadtraticEquation(double a,double b,double c,double d,double e,double f) {
018 this.a = a;
019 this.b = b;
020 this.c = c;
021 this.d = d;
022 this.e = e;
023 this.f = f;
024 ComplexNumber cn1 = new ComplexNumber(a,b);
025 ComplexNumber cn2 = new ComplexNumber(c,d);
026 ComplexNumber cn3 = new ComplexNumber(e,f);
027 this.cn1 = cn1;
028 this.cn2 = cn2;
029 this.cn3 = cn3;
030 }
031 /**
032 * @return The first solution to the quadratic equation.
033 */
034 public ComplexNumber getSolution1()
035 {
036 ComplexNumber cn4 = new ComplexNumber(cn2.multiply(cn2));
037 ComplexNumber cn5 = new ComplexNumber(cn1.multiply(cn3));
038 ComplexNumber cn6 = new ComplexNumber(cn1);
039 ComplexNumber cn7 = new ComplexNumber(cn2);
040 ComplexNumber cn8 = new ComplexNumber(cn3);
041 cn5.multiply(4);
042 cn4 = cn4.subtract(cn5);
043 cn4.squareRoot();
044 cn7.multiply(-1);
045 cn7 = cn7.subtract(cn4);
046 cn6.multiply(2);
047 cn7 = cn7.divide(cn6);
048 return cn7;
049 }
050 /**
051 * @return The second solution to the quadratic equation.
052 */
053 public ComplexNumber getSolution2()
054 {
055 ComplexNumber cn4 = new ComplexNumber(cn2.multiply(cn2));
056 ComplexNumber cn5 = new ComplexNumber(cn1.multiply(cn3));
057 ComplexNumber cn6 = new ComplexNumber(cn1);
058 ComplexNumber cn7 = new ComplexNumber(cn2);
059 ComplexNumber cn8 = new ComplexNumber(cn3);
060 cn5.multiply(4);
061 cn4 = cn4.subtract(cn5);
062 cn4.squareRoot();
063 cn7.multiply(-1);
064 cn7 = cn7.add(cn4);
065 cn6.multiply(2);
066 cn7 = cn7.divide(cn6);
067 return cn7;
068 }
069 /**
070 * The real part of the first complex number.
071 */
072 private double a;
073 /**
074 * The imaginary part of the first complex number.
075 */
076 private double b;
077 /**
078 * The real part of the second complex number.
079 */
080 private double c;
081 /**
082 * The imaginary part of the second complex number.
083 */
084 private double d;
085 /**
086 * The real part of the third complex number.
087 */
088 private double e;
089 /**
090 * The imaginary part of the third complex number.
091 */
092 private double f;
093 /**
094 * The first complex number that is inputted.
095 */
096 private ComplexNumber cn1;
097 /**
098 * The second complex number that is inputted.
099 */
100 private ComplexNumber cn2;
101 /**
102 * The third complex number that is inputted.
103 */
104 private ComplexNumber cn3;
105 }