jueves, 5 de febrero de 2015

Tema 3 ejercicio 8: Creando vector de ecuaciones de segundo grado

package ejercicio8;
import java.util.Random;

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Random al = new Random();
        EcuacionSegundoGrado [] vector = new EcuacionSegundoGrado [5];
        int contIncorrectas;
        
        for (int i = 0; i < 5; i++) {
            
            
            vector[i]= new EcuacionSegundoGrado(al.nextInt(5)+1,al.nextInt(10)+1,al.nextInt(5)+1);
            contIncorrectas=0;
            while (!vector[i].averigua()) {
                vector[i].setCoeficientes(al.nextInt(5)+1,al.nextInt(10)+1,al.nextInt(5)+1);
                ++contIncorrectas;
            }
            System.out.println(vector[i]);
            System.out.println("Sol1: " + vector[i].obtenerSolucion1());
            System.out.println("Sol: " + vector[i].obtenerSolucion2());
            System.out.println("Incorrectas generadas: "+contIncorrectas);
            System.out.println("-----------------------------");
        }
        
    
    }
    
}

/*
 * 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()+'}';
    }
    
    
}