Why do not you let me do a casting?

0

How about, I'm trying to represent a function in java and create a class to represent sine, cos etc ... that is a child of the class Term, then I store each term in an ArrayList. The problem is that when executing send me this exception

Exception in thread "main" java.lang.ClassCastException: metodo.biseccion.Termino cannot be cast to metodo.biseccion.TerminoTrig f(x)= at metodo.biseccion.Funcion.mostrarFuncion(Funcion.java:36) at metodo.biseccion.MetodoBiseccion.main(MetodoBiseccion.java:24) C:\Users\Gerardo\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

I do not understand why he will not let me do the casting, can someone help me? I leave the code of my classes

Fucnion Class: This is where the exception marks me, in the line where I do the casting

package metodo.biseccion;

import java.util.ArrayList;


public class Funcion {
private int grado;
private ArrayList<Termino> polinomio;

public Funcion(int grado){
    this.grado=grado;
    polinomio = new ArrayList<>();
}
public void agregaTermino (double coeficiente, double exponente){
    Termino t = new Termino(coeficiente,exponente);
    polinomio.add(t);
}
public void agregaTerminoTrig(double coeficiente, double exponente, int tipo){
    TerminoTrig t = new TerminoTrig(coeficiente,exponente,tipo);
    polinomio.add(t);
}
public void mostrarFuncion(){
    System.out.print("f(x)=");
    for(int i=0;i<polinomio.size();i++){
        double exp,coef;
        Termino t=(Termino)polinomio.get(i);
        if(t instanceof Termino){
            TerminoTrig t2 = (TerminoTrig)t;
            switch (t2.getTipo()){
                case 1:
                    exp=t.getExponente();
                    coef=t.getCoeficiente();
                    System.out.print(coef+" Sen"+"^"+exp+"(x) +");
                    break;
                case 2:
                    exp=t.getExponente();
                    coef=t.getCoeficiente();
                    System.out.print(coef+" Cos"+"^"+exp+"(x) +");
                    break;
                case 3:
                    exp=t.getExponente();
                    coef=t.getCoeficiente();
                    System.out.print(" "+coef+"Tan^"+exp+"(x) +");
                     break;
            }
        }
        else{
            exp=t.getExponente();
            coef=t.getCoeficiente();
            System.out.print(" "+coef+"x^"+exp+" +");
        }
    }
}
}

Term Class (Parent)

package metodo.biseccion;
import java.math.*;

public class Termino {
protected double coeficiente;
protected double exponente;

public double getCoeficiente() {
    return coeficiente;
}

public double getExponente() {
    return exponente;
}

public Termino (double coeficiente, double exponente){
        this.coeficiente=coeficiente;
        this.exponente=exponente;
}

public double obtenerValor(double x){
    return (Math.pow(x, exponente)*coeficiente);
}
}

Son Class

public class TerminoTrig extends Termino{
private final int tipo;

public TerminoTrig(double coeficiente, double exponente, int tipo) {
    super(coeficiente, exponente);
    this.tipo=tipo;
}

/**
 *
 * @return
 */
public int getTipo() {
    return tipo;
}

public double ObtenerValor(double x){
    switch (tipo){
        case 1: 
            return (Math.pow(Math.sin(x),exponente))*coeficiente;
        case 2:
            return (Math.pow(Math.cos(x),exponente))*coeficiente;
        case 3:
            return (Math.pow(Math.tan(x),exponente))*coeficiente;
        default:
            System.out.println("El tipo de funcion es incorrecto");
            return 0;
    }
} 
}

I would like to clarify that I have not even done tests with the data type sen, cos, so, in fact if I comment is part runs very well, so the error goes without even putting data.

    
asked by Gerardo 17.01.2018 в 23:12
source

2 answers

0

I already found the problem, apparently it is a misuse of my part of the instanceof operator, it must be t instanceof TerminoTrig. Anyway, thanks to the person who answered

    
answered by 18.01.2018 в 01:24
-1

The problem may be with your constructors, since you have a class Termino without constructor, take the default one that is

public Termino() {

}

Now in TerminoTrig that extends from term to the moment of creating the object can not be given that you can only create it with the parameters that the constructor needs and that the finished object does not have them

public TerminoTrig(double coeficiente, double exponente, int tipo) {
    super(coeficiente, exponente);
    this.tipo=tipo;
}

The solution would be to create another generic constructor where the parameters are not necessary

public TerminoTrig() {

}

With this I should be able to let you do the cast

    
answered by 18.01.2018 в 00:24