viernes, 16 de enero de 2015

Ecuaciones de segundo grado. Ejercicio 79

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ejercicio79;

/**
 *
 * @author juan
 */
public class EcuacionSegundoGrado {

    private int a;
    private int b;
    private int c;

    public EcuacionSegundoGrado(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }


    public EcuacionSegundoGrado() {
    }

    public void setCoeficientes(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }

    public boolean averigua() {

        /*
         boolean retorno=true;
         if (a==0)  retorno=false;
         else if (Math.pow(b, 2)-(4*a*c))<0 retorno = false;
         return retorno;
     
     
         if (a==0) return false;
         else if (Math.pow(b, 2)-(4*a*c))<0 return false;
         return true;
     
         boolean retorno=true;
         if ((a==0) || (Math.pow(b, 2)-(4*a*c))<0) retorno=false;
         return retorno;
     
         */
        return (a != 0 && (Math.pow(b, 2) - (4 * a * c) >= 0));
    }

    public double obtenerSolucion1() {
        double solucion;

        if (this.averigua()) {
            solucion = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
        } else {
            solucion = Double.NaN;
        }
        return solucion;
    }

    public double obtenerSolucion2() {
        double solucion;

        if (this.averigua()) {
            solucion = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
        } else {
            solucion = Double.NaN;
        }
        return solucion;
    }

    @Override
    public String toString() {
        return "EcuacionSegundoGrado{" + "a=" + a + ", b=" + b + ", c=" + c + ", Solucion1="+ this.obtenerSolucion1()+ ", Solución2="+this.obtenerSolucion2()+'}';
    }
 
 
}





public class Ejercicio79 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EcuacionSegundoGrado uno,dos;

uno = new EcuacionSegundoGrado(1,-5,6);
dos = new EcuacionSegundoGrado(1,1,1);

System.out.println(uno);
System.out.println(dos);

}

}

No hay comentarios: