/*
 *  ==========================================================================
 *  Quadratic.java : This Java 2 program prompts a user for the coefficients
 *  in a quadratic equation, and computes and prints the roots.
 *
 *  We format and print the roots to two decimal places of accuracy, using
 *  Java 2's constructing patterns.
 *
 *  Written By : Mark Austin                                      October 2005
 *  ==========================================================================
 */

import java.lang.Math;
import java.util.*;
import java.io.*;
import java.text.*;

class Quadratic {

    public static void main( String args[] ) {

    float fA,fB,fC;
    float fRoot1, fRoot2;
    float fDiscriminant;
    String sLine;

        // Print welcome message.

        System.out.println("Welcome to The Quadratic Equation Solver");
        System.out.println("----------------------------------------");

        // Prompt user for coefficients in quadratic equation.

        System.out.println("Please Enter coefficients for equation");
        System.out.println("a.x^2 + b.x + c");

        System.out.print("Coefficent a: ");
        sLine = getTextFromConsole();
        fA    = Float.valueOf(sLine).floatValue();

        System.out.print("Coefficent b: ");
        sLine = getTextFromConsole();
        fB    = Float.valueOf(sLine).floatValue();

        System.out.print("Coefficent c: ");
        sLine = getTextFromConsole();
        fC    = Float.valueOf(sLine).floatValue();

        // Print details of quadratic equation to screen.

        System.out.println("The equation you have entered is : ");
        System.out.println(+fA+".x^2 + "+fB+".x + "+fC);
    
        // Check for degenerate roots (i.e., fA = fB = zero).

        if ( fA==0 && fB==0 ) {
            System.out.println("Cannot solve " + fC +" = 0.0");
            return;
        } 

        if ( fA==0 && fB !=0 ) {
            fRoot1 = -fC/fB;
            System.out.println("Degenerate root : Root = "+ fRoot1);
            return;
        }

        // Compute discriminant of quadratic equation.

        fDiscriminant = fdiscriminant(fA,fB,fC);

        // Case for two real roots.

        if ( fDiscriminant >= 0.0 ) {

            // Compute : two real roots .....

            fRoot1 = (float)(-fB/2.0/fA-(float)Math.sqrt(fDiscriminant) /
                              2.0 / fA );
            fRoot2 = (float)(-fB/2.0/fA+(float)Math.sqrt(fDiscriminant) /
                              2.0 / fA);

            // Format output to two decimal places of accuracy ...

            DecimalFormat formatRoot = new DecimalFormat( "##.##" );
            String output1 = formatRoot.format( fRoot1 );
            String output2 = formatRoot.format( fRoot2 );

            // Print results to screen ....

            System.out.println("Two real roots : Root1 : " + output1 );
            System.out.println("                 Root2 : " + output2 ) ;
         } else {

            // Compute : two complex roots .....

            fRoot1 = (float) (-fB/2.0/fA);
            fRoot2 = (float) (Math.sqrt(-fDiscriminant)/2.0/fA);

            // Format output to two decimal places of accuracy ...

            DecimalFormat formatRoot = new DecimalFormat( "##.##" );
            String output1 = formatRoot.format( fRoot1 );
            String output2 = formatRoot.format( fRoot2 );

            System.out.println("Two complex roots");
            System.out.println("Root1 : " + output1 + "+" + output2 + "i");
            System.out.println("Root2 : " + output1 + "-" + output2 + "i");
         }
    }

    /* 
     *  =============================================================
     *  Method fdiscriminant() : compute discriminant of quadratic
     *
     *  Input  : fA, fB, and fC -- coefficients in quadratic equation
     *  Output : float fReturn  -- discriminant of quadratic equation
     *  =============================================================
     */ 

    static float fdiscriminant(float fA, float fB, float fC) {
    float fReturn;

       fReturn= (float)(fB*fB-4.0*fA*fC);
       return fReturn;
    }

    /* 
     *  ============================================================================
     *  Method getTextFromConsole(): Read line of text from console (keyboard input)
     * 
     *  Input  : None.
     *  Output : String sLine -- character string of keyboard input
     *  ============================================================================
     */

     static String getTextFromConsole() {
       String inLine = "";

       // Create buffered reader for keyboard input stream....

       BufferedReader inStream = new BufferedReader (
                                 new InputStreamReader(System.in));

       // Try to read input from keyboard ....

       try {
          inLine = inStream.readLine();
       } catch (IOException e) {
          System.out.println("IOException: " + e);
       }

       return inLine;
    }
}
