CS 1063, Fall 2005
QuadraticEquation class

The lectures for Week 10 had you work on an QuadraticEquation class. Just for practice, I'm showing a QuadraticEquationTest class that inputs doubles without using Java 1.5.

File: QuadraticEquation.java
// QuadraticEquation: calculate roots of a quadratic eq
public class QuadraticEquation {
   private double myA;    // the 'a' in equation
   private double myB;    // the 'b' in equation
   private double myC;    // the 'c' in equation
   private double myDisc; // b*b - 4*a*c
   
   // constructor
   public QuadraticEquation(double a, double b, double c) {
      myA = a;
      myB = b;
      myC = c;
      calculateDisc();
   }
   
   // calculate first root
   public double calculateSolution1() {
      return (-myB + Math.sqrt(myDisc))/(2.0*myA);
   }
   
   // calculate second root
   public double calculateSolution2() {
      return (-myB - Math.sqrt(myDisc))/(2.0*myA);
   }
   
   // check if there are real roots
   public boolean hasSolution() {
      if (myDisc < 0) return false;
      else return true;
   }
   
   // calculate discriminant
   private void calculateDisc() {
      myDisc = myB*myB - 4.0*myA*myC;
   }
}

Here we are inputting the three coefficients of a quadratic equation without using Java 1.5.

File: QuadraticEquationTest.java
// QuadraticEquationTest: test with several inputs
import java.io.*;

public class QuadraticEquationTest {

   public static void main(String[] args) throws IOException {
      // Set up for input
      BufferedReader in =
         new BufferedReader( new InputStreamReader(System.in));
         
      // input three doubles for a, b, and c
      System.out.print("Type value for 'a' --->");
      String input = in.readLine();
      double a = Double.parseDouble(input);
      
      System.out.print("Type value for 'b' --->");
             input = in.readLine();
      double b = Double.parseDouble(input);
      
      System.out.print("Type value for 'c' --->");
             input = in.readLine();
      double c = Double.parseDouble(input);
      
      // print the three inputs
      System.out.println("a = " + a +
         ", b = " + b + ", c = " + c);
         
      // create a Quadratic Equation object
      QuadraticEquation quad =
         new QuadraticEquation(a, b, c);
         
      // calculate roots
      // first check a, so we don't divide by 0
      if (a == 0)
         System.out.println("Not a quadratic equation");
      // next check if disc < 0, using hasSolution()
      else if (quad.hasSolution() == false)
         System.out.println("No real solutions");
      // finally calculate and print solutions
      else {
         double solution1 = quad.calculateSolution1();
         double solution2 = quad.calculateSolution2();
         System.out.println("Solution 1 = " + solution1 +
            ", Solution 2 = " + solution2);
      }
   }
}

Here is the resulting output.

Results of multiple runs
Type value for 'a' --->1.0
Type value for 'b' --->-4.0
Type value for 'c' --->4.0
a = 1.0, b = -4.0, c = 4.0
Solution 1 = 2.0, Solution 2 = 2.0

Type value for 'a' --->1.0
Type value for 'b' --->-4.0
Type value for 'c' --->-12.0
a = 1.0, b = -4.0, c = -12.0
Solution 1 = 6.0, Solution 2 = -2.0

Type value for 'a' --->1.0
Type value for 'b' --->-6.0
Type value for 'c' --->25.0
a = 1.0, b = -6.0, c = 25.0
No real solutions

Type value for 'a' --->0.5
Type value for 'b' --->0.0
Type value for 'c' --->-3.0
a = 0.5, b = 0.0, c = -3.0
Solution 1 = 2.449489742783178, Solution 2 = -2.449489742783178

Type value for 'a' --->3.5
Type value for 'b' --->-4.0
Type value for 'c' --->-2.75
a = 3.5, b = -4.0, c = -2.75
Solution 1 = 1.6260587900166714, Solution 2 = -0.48320164715952857

Type value for 'a' --->1.0
Type value for 'b' --->-3.0
Type value for 'c' --->3.0
a = 1.0, b = -3.0, c = 3.0
No real solutions

Type value for 'a' --->0.0
Type value for 'b' --->2.0
Type value for 'c' --->4.0
a = 0.0, b = 2.0, c = 4.0
Not a quadratic equation