Make your own free website on Tripod.com

Back

// Matt Kaliszewski
// September 17, 2002

import java.io.*;
import java.text.NumberFormat;

class Quadratic
{
       // Variables
       private int a;
       private int b;
       private int c;

       // Default constructor
       public Quadratic() throws IOException
       {
              String str;

              // Crate the buffered reader to read the input from the keyboard
              BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

              System.out.println("Please make sure your quadratic equation");
              System.out.println("is in the form 'Ax^2 + Bx + C = 0'\n");

              // Get the values for the equation
              System.out.print("Enter the value of A - ");
              str = in.readLine();
              a = Integer.parseInt(str);

              System.out.print("Enter the value of B - ");
              str = in.readLine();
              b = Integer.parseInt(str);

              System.out.print("Enter the value of C - ");
              str = in.readLine();
              c = Integer.parseInt(str);
       }

       // Method to display the equation
       public void showEquation()
       {
              System.out.println("\n" + a + "x^2 + " + b + "x + " + c);
       }

       // Method to solve the equation
       public void Solve()
       {
              double discriminant;
              double x1;
              double x2;

              // Set the number format options
              NumberFormat formatter = NumberFormat.getNumberInstance();
              formatter.setMaximumFractionDigits(4);

              // Calculate the discriminant
              discriminant = (b * b) - (4 * a * c);

              if(a==0) // Undefined
              {
                     System.out.println("\nThis equation in not Quadratic!!!\n");
              }
              else if(discriminant>=0) // Equation has real roots
              {
                     // Calculate the values
                     x1 = (-b + Math.sqrt(discriminant))/(2*a);
                     x2 = (-b - Math.sqrt(discriminant))/(2*a);

                     // Display the answers
                     System.out.println("\nThis equation has Real roots:");
                     System.out.println("X1 = " + formatter.format(x1));
                     System.out.println("X2 = " + formatter.format(x2)+ "\n");
              }
              else // Equation has imaginary roots
              {
                     double realPart;
                     double imaginaryPart;

                     // Calculate the values
                     realPart = -b / (2 * a);
                     imaginaryPart = Math.sqrt(-discriminant) / (2 * a);

                     // Display the answers
                     System.out.println("\nThis equation has Imaginary roots:");
                     System.out.println("X1 = " + formatter.format(realPart) + " + " + formatter.format(imaginaryPart) + "i");
                     System.out.println("X2 = " + formatter.format(realPart) + " - " + formatter.format(imaginaryPart) + "i\n");
              }
       }
}

class testQuadratic
{
       public static void main(String args[]) throws IOException
       {
              // Create a new quadratic equation
              Quadratic myEquation = new Quadratic();

              // Display the equation
              myEquation.showEquation();

              // Solve the equation
              myEquation.Solve();
       }
}

/* Output follows

Please make sure your quadratic equation
is in the form 'Ax^2 + Bx + C = 0'

Enter the value of A - 2
Enter the value of B - 4
Enter the value of C - 2

2x^2 + 4x + 2

This equation has Real roots:
X1 = -1
X2 = -1

press any key to exit...

/////////////////////////////////////////////

Please make sure your quadratic equation
is in the form 'Ax^2 + Bx + C = 0'

Enter the value of A - 1
Enter the value of B - 5
Enter the value of C - 12

1x^2 + 5x + 12

This equation has Imaginary roots:
X1 = -2 + 2.3979i
X2 = -2 - 2.3979i

press any key to exit...

/////////////////////////////////////////////

Please make sure your quadratic equation
is in the form 'Ax^2 + Bx + C = 0'

Enter the value of A - 0
Enter the value of B - 4
Enter the value of C - 12

0x^2 + 4x + 12

This equation in not Quadratic!!!

press any key to exit...

*/

Back