Quadratic (Java)

From LiteratePrograms

Jump to: navigation, search

This program is a code dump.
Code dumps are articles with little or no documentation or rearrangement of code. Please help to turn it into a literate program. Also make sure that the source of this code does consent to release it under the MIT or public domain license.


This is a Quadratic Equation program I wrote for my CSS-101 class. Its written in Java and it is probably messy. It imports information from the user(using the scanner class) to find a, b, and c.

As the author of this code, I give my full permission that anyone may freely used and edit this code how ever they see fit. If you use it for something all I ask is that you cite it and don't claim it as your own work.



This is the QuadEq class

<<QuadEq.java>>=
/**
 * Takes user input a,b, and c to calculate a quadratic equation
 * 
 * @author M. McManus
 * @version Oct-3-2008
 */
public class QuadEq
{
    private double a,b,c;
    public QuadEq(double ina, double inb, double inc)
    {    
        a = ina;
        b = inb;
        c = inc;
    }
    public double findDiscrim()
    {
        return (b * b - 4 * a * c);
    }
    public double findRoot1()
    {
        return (-b + Math.sqrt(b * b - 4 * a * c))/(2 * a );
    }
    public double findRoot2()
    {
        return (-b - Math.sqrt(b * b - 4 * a * c))/(2 * a );
    }
}



This is test class for QuadEq

<<QuadEqTest.java>>=
/**
 * Gets input from the user to test the QuadEq
 * 
 * @author M. McManus 
 * @version oct-03-2008
 */
import java.util.Scanner;
public class QuadEqTest
{
    public static void main (String[] args)
    {
        double a,b,c;
        Scanner myScanner = new Scanner(System.in);
        System.out.println("Welcome to Root-o-Matic");
        System.out.print("Input a: ");
        a = myScanner.nextDouble();
        System.out.print("Input b: ");
        b = myScanner.nextDouble();
        System.out.print("Input c: ");
        c = myScanner.nextDouble();
        QuadEq myQuadEq = new QuadEq(a,b,c);
        if (myQuadEq.findDiscrim() < 0)
            System.out.println("The roots are complex");
          else
          {
              System.out.println("Root 1 is : " + myQuadEq.findRoot1());
              System.out.println("Root 2 is : " + myQuadEq.findRoot2());
          }
    }
}
Download code
Views