Pages

Monday, October 15, 2012

Lab 4 Quadratic

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quadraticequation;

/**
 *
 * @author faridyusof727
 */
import java.util.Scanner;
public class QuadraticEquation {
   
    private double a;
    private double b;
    private double c;

    public QuadraticEquation (double a, double b, double c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
       
    }
    public double getA()
    {
        return a;
    }
    public double getB()
    {
        return b;
    }
    public double getC()
    {
        return c;
    }
    public double getDiscriminant()
    {
        double x;
        x = (b*b) - (4*a*c);
        return x;
       
    }
    public double getR1()
    {
        double x;
        x = (-(b) + Math.sqrt(getDiscriminant()))/(2*a) ;
        return x;
       
    }
    public double getR2()
    {
       double x;
       x =  (-(b) - Math.sqrt(getDiscriminant()))/(2*a) ;
       return x;
       
    }
   
    public static void main(String[] args) {
       
       
        Scanner scan = new Scanner(System.in);
        double a1, b1,c1;
           
       
           System.out.println("Value a:");
        a1 = scan.nextDouble();
        System.out.println("Value b:");
        b1 = scan.nextDouble();
        System.out.println("Value c:");
        c1 = scan.nextDouble();
               
        QuadraticEquation calc = new QuadraticEquation(a1, b1, c1);
       
        if (calc.getDiscriminant() < 0) {
            System.out.println("\nThe equation has no root.");
        }
        else if (calc.getDiscriminant() > 0) {
            System.out.println("\nThe first root is "+calc.getR1()+" and the second root is "+calc.getR2()+".");
        }
        else if (calc.getDiscriminant() == 0) {
            System.out.println("\nBoth root is "+calc.getR1());
        }
       
     
    }
}

No comments:

Post a Comment