//Quadratic.java /** * The Quadratic class represents an immutable quadratic equation in standard form. * * @author David Wills * @version 0.9 * @see any algebra book for the theory * */ import java.io.*; public class Quadratic implements Serializable { //********** static variables *************** private static int numberObjects=0; //********** instance variables ************* private double a; private double b; private double c; private double discriminant; private double x1; private double x2; private int numberSolutions; //********** constructors ******************* /** * Constructs a newly created Quadratic object with the parameters for the a, b, and c coefficients. * @param newA a * @param newB b * @param newC c */ /** * Constructs a newly created Quadratic object with the parameters for the a, b, and c coefficients. * @param newA a * @param newB b * @param newC c * */ public Quadratic(double newA, double newB, double newC) throws InvalidQuadraticException { if (newA == 0) throw new InvalidQuadraticException("a can not be 0"); a = newA; b = newB; c = newC; computeSolutions(); numberObjects++; } /** * Constructs a newly created Quadratic object as a copy of the parameter. * @param other source object * */ //shouldn't have to throw exception?? public Quadratic( Quadratic other) { a = other.a; b = other.b; c = other.c; computeSolutions(); numberObjects++; } /** * Constructs a newly created Quadratic object with the coefficients extracted from a String. * @param quadString a String parameter is 3 space-separated numbers * */ //incomplete...... public Quadratic( String quadString) throws InvalidQuadraticException { java.util.StringTokenizer st = new java.util.StringTokenizer(quadString); a = Double.parseDouble(st.nextToken()); b = Double.parseDouble(st.nextToken()); c = Double.parseDouble(st.nextToken()); computeSolutions(); numberObjects++; } //********** accessor methods *************** public static int getNumberObjects() { return numberObjects; } public double getA() { return a; } public double getB() { return b; } public double getC() { return c; } public double getDiscriminant() { return discriminant; } public double getx1() { //??what if has no value, i.e. numberSolutions==0 will return 0 return x1; } public double getx2() { //??what if has no value, i.e. numberSolutions<2 will return 0... return x2; } public int getNumberSolutions() { return numberSolutions; } public String toString() { return("a:" + a + " b:" + b + " c:" + c + " discriminant:" + discriminant + (numberSolutions==2 ? (" x1:" + x1 + " x2:" + x2) : (numberSolutions==1 ? (" x1:" + x1) : ""))); } public boolean equals( Object other ) { if ((other instanceof Quadratic) && ((((Quadratic)other).a==a) && (((Quadratic)other).b==b) && (((Quadratic)other).c==c))) return true; else return false; } public int hashCode() { return (int)(a + b + c) % 101; } //********** mutator methods *************** public static void clearNumberObjects() { numberObjects = 0; } private void computeSolutions() { discriminant = b*b - 4*a*c; if (discriminant > 0) { // calculate the two possible real values for x. x1 = (-b + Math.sqrt(discriminant)) / (2 * a); x2 = (-b - Math.sqrt(discriminant)) / (2 * a); numberSolutions = 2; } else if (discriminant == 0) { //one real root x1 = -b / (2*a); numberSolutions = 1; } else //no real roots numberSolutions = 0; } }