Function only returns zero

Asked

Viewed 194 times

1

Good afternoon!! I would like your help in the following question: I have the following function that returns me as a result 0 only:

      public double calorEspecifico(){
            double cp;

            cp = (fruto.getCp1() + fruto.getCp2())*fruto.getCondicao().getteorAgua();
            return cp;

        }

I have debugged the variables to see if they are null, but everything is ok! Below follows the FXMLController, where I call the function, see:

   @FXML
    public void btSimularAction(ActionEvent evt) {


        System.out.println("Calor Específico "+  calculoThompson.calorEspecifico());


    }

Fruit Class

/*
 * 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 Model;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.sun.istack.internal.NotNull;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import Enum.EquacoesTeorUm;

/**
 * Tabela de identificação de produto, coeficientes e modelos a serem utilizados
 * pelo PROGRAMA COMPUTACIONAL DE SIMULAÇÃO DA TRANSFERÊNCIA DE CALOR E MASSA DE
 * FRUTOS DO CERRADO
 *
 * <p>
 * O <code>Fruto</code> é um objeto persistido no banco de dados, por isso a
 * classificamos como <strong>Entidade</strong>.</p>
 *
 * <p>
 * As funcionalidades desse sistema demonstração são concentradas no cadastro
 * (CRUD) de frutos.</p>
 *
 * @author Sara Fernandes
 */
@Entity
@Table(name = "tblFruto")
public class Fruto implements Serializable {

    /**
     * Chave primária da entidade <code>Fruto</code>. O valor gerado pelo banco
     * de dados.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    //@FieldColumn(labelValue = "id", width = 50)
    @Column(name = "FRUTOID")
    private Integer idFruto;


    @OneToOne(cascade=CascadeType.ALL)
     Condicao condicao = new Condicao();
    //private Condicao condicao;

    @Column(name = "FRUTO")
    @NotNull
    private String fruto;
    @Column(name = "R1")
    @NotNull
    private double r1;// Razão de Conversão
    @Column(name = "Cp1")
    @NotNull
    private double cp1;// Calor específico
    @Column(name = "Cp2")
    @NotNull
    private double cp2;// Calor específico
    @Column(name = "HFG1")
    @NotNull
    private double hfg1;
    @Column(name = "HFG2")
    @NotNull
    private double hfg2;
    @Column(name = "HFG3")
    @NotNull
    private double hfg3;
    @Column(name = "HFG4")
    @NotNull
    private double hfg4;// Calor latente

    @Column(name = "EqUMD")
    //private String xe;
    private EquacoesTeorUm eqTeorUmi;
    /* Coeficientes a serem utilizados pe Equação de Teor de Áqua de Equilibrio (Thompson, Cavalcanti Mata, Hederson, Oswin)*/
    private double xe1;
    private double xe2;
    private double xe3;
    private double xe4;
    private double xe5;

    /* Coeficientes a serem utilizados nas Equações encadeada utilizadas nas equaçoes de tempo equivalente e, em sequancia na equação de Camada Fina*/
    private double a1;
    private double a2;
    private double a3;
    private double a4;
    private double a5;
    private double a6;
    private double a7;
    private double a8;

    private double b1;
    private double b2;
    private double b3;
    private double b4;
    private double b5;
    private double b6;
    private double b7;
    private double b8;

//    @OneToOne
//    @JoinColumn(name = " mod_id ")
//    private Modelos modelo;
    // private Equacoes equacoes;
    public Fruto() {
    }

    //Esse método formata o nome corretamente
    private String setName(String n) {

        n = n.toLowerCase();
        n = n.substring(0, 1).toUpperCase() + n.substring(1);
        return n;
    }

    public Fruto(Integer idFruto, String fruto, double r1, double cp1, double cp2,
            double hfg1, double hfg2, double hfg3, double hfg4, String xe,
            double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8,
            double b1, double b2, double b3, double b4, double b5, double b6, double b7, double b8,
            double xe1, double xe2, double xe3, double xe4, double xe5, double xe6, double xe7, double xe8) {

        this.idFruto = idFruto;
        this.fruto = fruto;
        this.r1 = r1;
        this.cp1 = cp1;
        this.cp2 = cp2;
        this.hfg1 = hfg1;
        this.hfg2 = hfg2;
        this.hfg3 = hfg3;
        this.hfg4 = hfg4;

        this.a1 = a1;
        this.a2 = a2;
        this.a3 = a3;
        this.a4 = a4;
        this.a5 = a5;
        this.a6 = a6;
        this.a7 = a7;
        this.a8 = a8;

        this.b1 = b1;
        this.b2 = b2;
        this.b3 = b3;
        this.b4 = b4;
        this.b5 = b5;
        this.b6 = b6;
        this.b7 = b7;
        this.b8 = b8;

        this.xe1 = xe1;
        this.xe2 = xe2;
        this.xe3 = xe3;
        this.xe4 = xe4;
        this.xe5 = xe5;

    }

    public Integer getIdFruto() {
        return idFruto;

    }

    public void setIdFruto(Integer idFruto) {
        this.idFruto = idFruto;
    }

    public String getFruto() {
        return fruto;
    }

    public void setFruto(String fruto) {
        this.fruto = fruto;
    }

    public double getR1() {
        return r1;
    }

    public void setR1(double r1) {
        this.r1 = r1;
    }

    public double getCp1() {
        return cp1;
    }

    public void setCp1(double cp1) {
        this.cp1 = cp1;
    }

    public double getCp2() {
        return cp2;
    }

    public void setCp2(double cp2) {
        this.cp2 = cp2;
    }

    public double getHfg1() {
        return hfg1;
    }

    public void setHfg1(double hfg1) {
        this.hfg1 = hfg1;
    }

    public double getHfg2() {
        return hfg2;
    }

    public void setHfg2(double hfg2) {
        this.hfg2 = hfg2;
    }

    public double getHfg3() {
        return hfg3;
    }

    public void setHfg3(double hfg3) {
        this.hfg3 = hfg3;
    }

    public double getHfg4() {
        return hfg4;
    }

    public void setHfg4(double hfg4) {
        this.hfg4 = hfg4;
    }

    public EquacoesTeorUm getEqTeorUmi() {
        return eqTeorUmi;
    }

    public void setEqTeorUmi(EquacoesTeorUm eqTeorUmi) {
        this.eqTeorUmi = eqTeorUmi;
    }

    //    public String getXe() {
//        return xe;
//    }
//
//    public void setXe(String xe) {
//        this.xe = xe;
//    }

//    public Modelos getModelo() {
//        return modelo;
//    }
//
//    public void setModelo(Modelos modelo) {
//        this.modelo = modelo;
//    }
    public double getA1() {
        return a1;
    }

    public void setA1(double a1) {
        this.a1 = a1;
    }

    public double getA2() {
        return a2;
    }

    public void setA2(double a2) {
        this.a2 = a2;
    }

    public double getA3() {
        return a3;
    }

    public void setA3(double a3) {
        this.a3 = a3;
    }

    public double getA4() {
        return a4;
    }

    public void setA4(double a4) {
        this.a4 = a4;
    }

    public double getA5() {
        return a5;
    }

    public void setA5(double a5) {
        this.a5 = a5;
    }

    public double getA6() {
        return a6;
    }

    public void setA6(double a6) {
        this.a6 = a6;
    }

    public double getA7() {
        return a7;
    }

    public void setA7(double a7) {
        this.a7 = a7;
    }

    public double getA8() {
        return a8;
    }

    public void setA8(double a8) {
        this.a8 = a8;
    }

    public double getB1() {
        return b1;
    }

    public void setB1(double b1) {
        this.b1 = b1;
    }

    public double getB2() {
        return b2;
    }

    public void setB2(double b2) {
        this.b2 = b2;
    }

    public double getB3() {
        return b3;
    }

    public void setB3(double b3) {
        this.b3 = b3;
    }

    public double getB4() {
        return b4;
    }

    public void setB4(double b4) {
        this.b4 = b4;
    }

    public double getB5() {
        return b5;
    }

    public void setB5(double b5) {
        this.b5 = b5;
    }

    public double getB6() {
        return b6;
    }

    public void setB6(double b6) {
        this.b6 = b6;
    }

    public double getB7() {
        return b7;
    }

    public void setB7(double b7) {
        this.b7 = b7;
    }

    public double getB8() {
        return b8;
    }

    public void setB8(double b8) {
        this.b8 = b8;
    }

    public double getXe1() {
        return xe1;
    }

    public void setXe1(double xe1) {
        this.xe1 = xe1;
    }

    public double getXe2() {
        return xe2;
    }

    public void setXe2(double xe2) {
        this.xe2 = xe2;
    }

    public double getXe3() {
        return xe3;
    }

    public void setXe3(double xe3) {
        this.xe3 = xe3;
    }

    public double getXe4() {
        return xe4;
    }

    public void setXe4(double xe4) {
        this.xe4 = xe4;
    }

    public double getXe5() {
        return xe5;
    }

    public void setXe5(double xe5) {
        this.xe5 = xe5;
    }

    public Condicao getCondicao() {
        return condicao;
    }

    public void setCondicao(Condicao condicao) {
        this.condicao = condicao;
    }

    public boolean eIgual(Object object) {
        //TODO: Atenção - este método não funciona no caso os campos de identificação não são definidos
        if (!(object instanceof Fruto)) {
            return false;
        }
        Fruto other = (Fruto) object;
        if ((this.idFruto == null && other.idFruto != null) || (this.idFruto != null && !this.idFruto.equals(other.idFruto))) {
            return false;
        }
        return true;
    }

}

Condition class

/*
 * 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.

Sobre os parâmetros da anotação @OneToOne:

“cascade” – define ações automatizadas no relacionamento, ex.: Ao apagar um Customer, apagar também um Usuário. Veremos sobre “cascade” em um post futuro.
“optional” – você não será obrigado a ter um User ao persistir um Customer, ou seja, você não tem que criar um usuário para satisfazer essa condição (você poderá buscar o Customer no banco de dados, mas Customer.getUser() terá null como resposta). Com o valor igual a false, ao se cadastrar um Customer, é obrigatória a presença de um User. Pode ser um User recém ainda não persistido.
“fetch” – o valor padrão é EAGER. Ou seja, ao carregar o Customer já será feita a consulta relacionada ao User de modo automático. Iremos ver sobre esse assunto em um post futuro.
“orphanRemoval” – define que uma entidade dependente, caso não tenha relacionamento, será removida do banco de dados (em nosso modelo, User depende de Customer). Caso exista um User sem Customer, esse user será removido.
Existem também duas anotações sendo que uma delas está comentada:

// @JoinColumn(name=”USER_ID”, nullable=false) – define qual é a coluna mapeada para fazer a união na consulta. É indicado o nome da coluna através do parâmetro “name” e que esse campo não pode ser nulo pelo parâmetro “nullable”.
@PrimaryKeyJoinColumn – essa anotação indica ao JPA que, para encontrar um objeto User basta procurar um registro com o mesmo ID do Customer. Ou seja, indica que um User vai ter o mesmo ID que seu Customer.

 */
package Model;

import com.sun.istack.internal.NotNull;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Tabela de identificação das condições de secagem. Relaciona-se à tabela
 * Fruto
 *
 * @author Sara Fernandes
 */
@Entity
@Table(name = "tblCondicao")
public class Condicao implements Serializable {
   /**
     * Chave primária da entidade <code>Fruto</code>. O valor gerado pelo banco
     * de dados.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    //@FieldColumn(labelValue = "id", width = 50)
    @Column(name = "CONDICAOID")
    private Integer idCondicao;

//    @Column(name = "FrutoID")
//    @OneToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER, orphanRemoval = true)
//    // @JoinColumn(name="USER_ID", nullable=false)
//    @PrimaryKeyJoinColumn
//    private Fruto fruto;

    @Column(name = "UR")
    @NotNull
    private double ur;// Umidade relativa do Ar
    //Atributos para Condições de Secagem
    @Column(name = "Date_Hora")//precisa  estudar um pouco de sql .....Então o eero esta no SQL? voce colocou um '&' no nome da coluna hibernate não faz milagre = huahuahua :P
    @Temporal(TemporalType.TIMESTAMP)
    private Date dataEhoraDaSimulacao;

    @Column(name = "TempFruto")
    @NotNull
    private int tempeFruto;

    @Column(name = "TempSgm")
    @NotNull
    private int tempeSgm;

    @Column(name = "NumCamadas")
    private int numCamadas;

    @Column(name = "PAtm")
    @NotNull
    private double pAtm;

    @Column(name = "TeorAgua")
    @NotNull
    private double teorAgua;

    @Column(name = "TeorAguaInl")
    @NotNull
    private double tAguaInl;

    @Column(name = "TeorAguaEqui")
    @NotNull
    private double tAguaEqui; 

    @Column(name = "TeorAguaFnl")
    @NotNull
    private double tAguaFnl;

    @Column(name = "FxoArSgm")
    @NotNull
    private double fxoArSgm;

    @Column(name = "AltCmdFruto")
    private double altCmdFruto;

    @Column(name = "IntTempo")// INTERVALO DE TEMPO
    private int intTempo;

    @Column(name = "VolEspAr")
    @NotNull
    private double volEspAr;

    public Condicao() {
        this.dataEhoraDaSimulacao = new Date();
    }

    public Condicao(Integer idCondicao, double ur,int tempeFruto, int tempeSgm, int numCamadas,
                    double pAtm,double teorAgua, double tAguaInl, double tAguaEqui,double tAguaFnl,double fxoArSgm,
                    double altCmdFruto,int intTempo, double volEspAr) {

        this.idCondicao = idCondicao;
        this.ur = ur;
        this.tempeFruto = tempeFruto;
        this.tempeSgm = tempeSgm;
        this.numCamadas = numCamadas;
        this.pAtm = pAtm;
        this.teorAgua =teorAgua;
        this.tAguaInl = tAguaInl;
        this.tAguaEqui = tAguaEqui;
        this.tAguaFnl = tAguaFnl;
        this.fxoArSgm = fxoArSgm;
        this.altCmdFruto = altCmdFruto;
        this.intTempo = intTempo;
        this.volEspAr = volEspAr;


    }



    public Integer getIdCondicao() {
        return idCondicao;
    }

    public void setIdCondicao(Integer idCondicao) {
        this.idCondicao = idCondicao;
    }

    public Date getDataEhoraDaSimulacao() {
        return dataEhoraDaSimulacao;
    }

    public void setDataEhoraDaSimulacao(Date dataEhoraDaSimulacao) {
        this.dataEhoraDaSimulacao = dataEhoraDaSimulacao;
    }

    public int getTempeFruto() {
        return tempeFruto;
    }

    public void setTempeFruto(int tempeFruto) {
        this.tempeFruto = tempeFruto;
    }

    public int getTempeSgm() {
        return tempeSgm;
    }

    public void setTempeSgm(int tempeSgm) {
        this.tempeSgm = tempeSgm;
    }


    public double getpAtm() {
        return pAtm;
    }

    public void setpAtm(double pAtm) {
        this.pAtm = pAtm;
    }

    public double gettAguaInl() {
        return tAguaInl;
    }

    public void settAguaInl(double tAguaInl) {
        this.tAguaInl = tAguaInl;
    }

    public double gettAguaFnl() {
        return tAguaFnl;
    }

    public void settAguaFnl(double tAguaFnl) {
        this.tAguaFnl = tAguaFnl;
    }

    public double getFxoArSgm() {
        return fxoArSgm;
    }

    public void setFxoArSgm(double fxoArSgm) {
        this.fxoArSgm = fxoArSgm;
    }

    public double getAltCmdFruto() {
        return altCmdFruto;
    }

    public void setAltCmdFruto(double altCmdFruto) {
        this.altCmdFruto = altCmdFruto;
    }

    public int getIntTempo() {
        return intTempo;
    }

    public void setIntTempo(int intTempo) {
        this.intTempo = intTempo;
    }

    public double getVolEspAr() {
        return volEspAr;
    }

    public void setVolEspAr(double volEspAr) {
        this.volEspAr = volEspAr;
    }

    public int getNumCamadas() {
        return numCamadas;
    }

    public void setNumCamadas(int numCamadas) {
        this.numCamadas = numCamadas;
    }


    public double getUr() {
        return ur;
    }

    public void setUr(double ur) {
        this.ur = ur;
    }

    public double getteorAgua() {
        return teorAgua;
    }

    public void settAgua(double teorAgua) {
        this.teorAgua = teorAgua;
    }

    public double gettAguaEqui() {
        return tAguaEqui;
    }

    public void settAguaEqui(double tAguaEqui) {
        this.tAguaEqui = tAguaEqui;
    }

    public double razaoTeorAqua(){
        double rx;

        rx = (this.teorAgua - this.tAguaEqui)/(this.tAguaInl - this.tAguaEqui);
        return rx;


    }



}

Class where I do the function calculations

/*
 * The MIT License
 *
 * Copyright 2015 Sara Fernandes.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package algoritmos;

import Model.Condicao;
import Model.Fruto;

/**
 *
 * @author Sara Pereira Fernandes
 */
public class ThompsonCal {


    Fruto fruto = new Fruto();

    public Fruto getFruto() {
        return fruto;
    }

    public void setFruto(Fruto fruto) {
        this.fruto = fruto;
    }

    public double razaoConversao(){
        double rc;

           rc = fruto.getR1()*fruto.getCondicao().getAltCmdFruto()*fruto.getCondicao().getVolEspAr()/
                   fruto.getCondicao().getFxoArSgm()*fruto.getCondicao().getIntTempo()*fruto.getCondicao().getNumCamadas()*60;

           return rc;

       }

       public double calorEspecifico(){
        double cp;

        cp = fruto.getCp1() + fruto.getCp2()*fruto.getCondicao().getteorAgua();
        return cp;

    }
    public double soma(){

        double soma;

        soma = fruto.getCp1() + fruto.getCp2();
        return soma;
    }


//    public double teorAgua(){
//        double x;
//
//        x = fruto.getCondicao().gettAguaFnl()- fruto.getCondicao().gettAguaFnl();
//        return x;
//    }

    public double calorLatenteDeVaporAguaNoFruto(){
        double hfg = 0;

        hfg = ((fruto.getHfg1() - fruto.getHfg2()) * fruto.getCondicao().getTempeFruto())
                *(fruto.getHfg3()*Math.exp( -fruto.getHfg4()*fruto.getCondicao().getteorAgua()));

        return hfg;
    }



}

Classa Fxmlcontroller

    public void setarCampos() {
        tfNomeFruto.setText(fruto.getFruto());
        tfRconversao.setText(Double.toString(fruto.getR1()));
        tfCalorEs1.setText(Double.toString(fruto.getCp1()));
        tfCalorEs2.setText(Double.toString(fruto.getCp2()));
        tfCalorLat1.setText(Double.toString(fruto.getHfg1()));
        tfCalorLat2.setText(Double.toString(fruto.getHfg2()));
        tfCalorLat3.setText(Double.toString(fruto.getHfg3()));
        tfCalorLat4.setText(Double.toString(fruto.getHfg4()));

        tfTempeFruto.setText(Integer.toString(fruto.getCondicao().getTempeFruto()));
        tfUmidRelativa.setText(Double.toString(fruto.getCondicao().getUr()));
        tfTAguaInl.setText(Double.toString(fruto.getCondicao().gettAguaInl()));
        tfPAtm.setText(Double.toString(fruto.getCondicao().getpAtm()));
        tfTempeSgm.setText(Integer.toString(fruto.getCondicao().getTempeSgm()));
        tfTAguaFnl.setText(Double.toString(fruto.getCondicao().gettAguaFnl()));
        tfIntTempo.setText(Integer.toString(fruto.getCondicao().getIntTempo()));
        tfTAguaEqui.setText(Double.toString(fruto.getCondicao().gettAguaEqui()));
        tfFluxoArSgm.setText(Double.toString(fruto.getCondicao().getFxoArSgm()));
        tfTeorAgua.setText(Double.toString(fruto.getCondicao().getteorAgua()));
        tfVolEspAr.setText(Double.toString(fruto.getCondicao().getVolEspAr()));
        tfAltCmdFruto.setText(Double.toString(fruto.getCondicao().getAltCmdFruto()));



        tfA1.setText(Double.toString(fruto.getA1()));
        tfA2.setText(Double.toString(fruto.getA2()));
        tfA3.setText(Double.toString(fruto.getA3()));
        tfA4.setText(Double.toString(fruto.getA4()));
        tfA5.setText(Double.toString(fruto.getA5()));
        tfA6.setText(Double.toString(fruto.getA6()));
        tfA7.setText(Double.toString(fruto.getA7()));
        tfA8.setText(Double.toString(fruto.getA8()));

        tfB1.setText(Double.toString(fruto.getB1()));
        tfB2.setText(Double.toString(fruto.getB2()));
        tfB3.setText(Double.toString(fruto.getB3()));
        tfB4.setText(Double.toString(fruto.getB4()));
        tfB5.setText(Double.toString(fruto.getB5()));
        tfB6.setText(Double.toString(fruto.getB6()));
        tfB7.setText(Double.toString(fruto.getB7()));
        tfB8.setText(Double.toString(fruto.getB8()));



        }


 @FXML
    public void btSimularAction(ActionEvent evt) {



        System.out.println("Razão Teor de àgua "+fruto.getCondicao().razaoTeorAqua());
        System.out.println("Calor Específico "+  calculoThompson.calorEspecifico());
        System.out.println("Teor de àgua:"+ calculoThompson.getFruto().getCondicao().getteorAgua());
        System.out.println("CP1:"+calculoThompson.getFruto().getCp1());


    }

2 answers

2


Can’t tell how you’re instantiating calculoThompson, you’d have to do something like

@FXML
public void btSimularAction(ActionEvent evt) {
    calculoThompson.setFruto(fruto); // fruto que está instanciando

    // dessa forma ele vai pegar o valor que está na sua instância ao invés de pegar o `new`
    System.out.println("Calor Específico "+  calculoThompson.calorEspecifico());


}

To make it easier you can create a constructor with parameter in ThompsonCal, example:

public ThompsonCal(Fruto fruto) {
    this.fruto = fruto;
}

And the part where you’re using it:

ThompsonCal calculoThompson = new ThompsonCal(fruto);

Have you done the part of recovering the typed values? Type what you did from setarCampos, but in case a getCampos.

// exemplo
public void getCampos() {
    fruto.setR1(Double.parseDouble(tfRconversao.getText()));
    fruto.setCp1(Double.parseDouble(tfCalorEs1.getText()));
    // assim por diante
}
  • 1

    Good evening Maicon Carraro, I want to thank you immensely for your help! Your response together with Victor Stafusa made it work perfectly :)!... Háaaa and the "calculoThompson" is being instantiated as follows -> Thompsoncal calculoThompson = new Thompsoncal(fruit);

  • @Sarafernandes My answer turned out to be more technical to solve your problem, but really you need to take into account the points cited by @VictorStafusa

  • 1

    Hello @Victorstafusa. Yes yes, too bad I can’t vote for both of them. By agreeing with both of them I managed to solve it! All the observations made by him are extremely correct.

2

In your class ThompsonCal there’s this:

Fruto fruto = new Fruto();

When the fruit is created, all its values will be initialized initially as zero!

Also, you are not calling any function or method that would change its values (the setarCampos() only shows on screen, does not change any value). In this way, when performing calculations on the values, you will only be adding zero to zero, multiplying zero by zero and doing things like this that will always result in zero. You must define the values that are in the Fruit before trying to make any calculation with the fruit.

Also, I would recommend that you study a little about immutability and validation and recommend knowing the design pattern Builder. His whole problem lies in being working on an object with an inconsistent internal state (the internal state of its variables when it is with several zeros, is a meaningless state). In fact, the object is already being born with an inconsistent state (which needs to be "repaired" after being created). Instead of creating an object in an inconsistent state to have to fix it later, the ideal is that you already create the object in a consistent state!

I recommend that you encode your program so that it never creates an inconsistent Fruit (or any other object), and one way to do this is to nay use the default constructor, and yes the other constructor with a lot of parameters. It is important that any code that will interact with instances of Fruit (except Fruit itself) can fill it only in a consistent state and that in the class Fruto, you add validations to prevent it from entering an inconsistent state.

I also recommend studying the difference between a rich object model (Rich Domain model) and a model of anemic objects (Anemic Domain model). Your model is anemic (which is bad).

  • setarCampos() does not return values for fruit, only displays on the screen :)

  • 1

    @Maiconcarraro Well observed. Edited.

  • 1

    Got a little confused her name right? aheauheuaeh at first I thought I was going to assign back to the fruit too

  • @Maiconcarraro Yes, the setarCampos() is actually a lerCampos().

  • 2

    Guys, thank you so much for your willingness to help me :)!! This problem was driving me crazy hahah!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.