Public with 2 variables does not return value

Asked

Viewed 90 times

2

I have 3 publics that should return a calculation value for me to present on the Activity screen. The first, volume_agua_mehta(), returns normally, but the second, massa_brita_mehta(), and the third, agua_material_cimentício_mehta(), return NaN in TextView.

I wish I knew what I’m doing wrong.

Note: All these publics are in the same class and I recovered the right variables in the right way.

public double volume_agua_mehta(){
    return resultado = 221.91 * exp((-0.005)*resistencia_concreto);
}

public double massa_brita_mehta(){
    return resultado = (130 * 5 + 0.319 * Math.log(resistencia_concreto) - 3.332)) * massa_especifica_sss_brita;
}

public double agua_material_cimentício_mehta(){
    Calculo_Mehta Calculo_Mehta = new Calculo_Mehta();

    double volume_agua_mehta = Calculo_Mehta.volume_agua_mehta();
    double massa_cimento_mehta = Calculo_Mehta.massa_cimento_mehta();
    double massa_aditivo_mineral1_mehta = Calculo_Mehta.massa_aditivo_mineral1_mehta();
    double massa_aditivo_minera2_mehta = Calculo_Mehta.massa_aditivo_minera2_mehta();

    return resultado = volume_agua_mehta/(massa_cimento_mehta+massa_aditivo_mineral1_mehta+massa_aditivo_minera2_mehta);
} 

In class Dosagem_Dados_Gerais:

public EditText txt_resistencia_concreto;
public EditText txt_massa_especifica_sss_brita;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dosagem__dados__gerais);

    txt_resistencia_concreto=(EditText)findViewById(R.id.txt_resistencia_concreto);
    txt_massa_especifica_sss_areia = (EditText)findViewById(R.id.txt_massa_especifica_sss_areia);


    final Button btn_proximo_dados_gerais = (Button) findViewById(R.id.btn_proximo_dados_gerais);
    btn_proximo_dados_gerais.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //AQUI SÃO FEITAS VERIFICAÇÕES SE OS EDITTEXTS ESTÃO PREENCHIDOS, VOU PULAR ESSA PARTE E IR DIRETO AO PONTO

            Intent it_mehta = new Intent(Dosagem_Dados_Gerais.this, Calculo_Mehta.class);

            it_mehta.putExtra("resistencia_concreto", txt_resistencia_concreto.getText().toString());
            it_mehta.putExtra("massa_especifica_sss_brita", txt_massa_especifica_sss_brita.getText().toString());
            startActivity(it_mehta);

        }
    }

Class Calculo_Mehta:

public double resistencia_concreto;
public double massa_especifica_sss_brita;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String txt_resistencia_concreto = getIntent().getStringExtra("resistencia_concreto"); //Recuperar na string
    resistencia_concreto=Double.parseDouble(txt_resistencia_concreto); //Passando para double

    String txt_massa_especifica_sss_brita= getIntent().getStringExtra("massa_especifica_sss_brita");
    massa_especifica_sss_brita=Double.parseDouble(txt_massa_especifica_sss_brita);

}

After this comes those publics I showed at the beginning of the post.

Class Resultado:

public TextView resultado_agua;
public TextView resultado_brita;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_resultado);

    resultado_agua = (TextView)findViewById(R.id.txt_resultado_agua);
    resultado_brita = (TextView)findViewById(R.id.txt_resultado_brita);

    final Button btn_calcular_dosagem = (Button) findViewById(R.id.btn_calcular_dosagem);
    btn_calcular_dosagem.setOnClickListener(new View.OnClickListener() {

        volume = Double.parseDouble(resultado_volume.getText().toString());

        public void onClick (View View) {

            Calculo_Mehta Calculo_Mehta = new Calculo_Mehta();

            //ESSE PRIMEIRO FUNCIONA E MOSTRA NO EDIT TEXT PERFEITAMENTE
            double massa_agua_ponto_saturacao_superplastificante_mehta = Calculo_Mehta.volume_agua_mehta()*volume; //Massa água
            String stg_resultado_agua_mehta = Double.toString(massa_agua_ponto_saturacao_superplastificante_mehta);
            resultado_agua.setText(stg_resultado_agua_mehta);

            //ESSE NÃO FUNCIONA
            double massa_brita_mehta = (Calculo_Mehta.massa_brita_mehta(Calculo_Mehta.resistencia_concreto,Calculo_Mehta.massa_especifica_sss_brita))*volume; //Massa brita
            String stg_brita_mehta_mehta = Double.toString(massa_brita_mehta);
            resultado_brita.setText(stg_brita_mehta_mehta);


        }
    }
}
  • the variables of the 'agua_material_cimentício_mehta' method are filled in correctly?

2 answers

0

First tip is to check if the values are being filled in correctly. For example in your first method volume_agua_mehta(), check whether the variable resistencia_concreto is receiving the desired value. I created a method based on your but with fixed values importing the java.lang.Math.exp and get a proper return:

public double volume_agua_mehta(){
   return 221.91 * exp((-0.005)*234);
}

Return:

68.8735

You can also create a method by passing a parameter resistencia_concreto. Behold:

public double volume_agua_mehta(double resistencia_concreto){
        return 221.91 * exp((-0.005)*resistencia_concreto);
}

See also how the method would look agua_material_cimentício_mehta() passing as parameter the class Calculo_Mehta:

public double agua_material_cimenticio_mehta(Calculo_Mehta Calculo_Mehta) {
    return Calculo_Mehta.volume_agua_mehta() / (Calculo_Mehta.massa_cimento_mehta() 
            + Calculo_Mehta.massa_aditivo_mineral1_mehta() 
            + Calculo_Mehta.massa_aditivo_minera2_mehta());
}

So, in calling the TextView you must do this way:

Calculo_Mehta cal = new Calculo_Mehta();
String valueDouble= Double.toString(agua_material_cimenticio_mehta(cal));
textview.setText(valueDouble);

Obs.: You must define all values within Calculo_Mehta to make it work properly.

  • Got it!! This part of putting the double resistencia_concrete inside the parentheses of the method I had already thought about, but what about the others? This is my biggest doubt, because the others have more than one variable.

  • With respect to text view, it is returning right: double massa_agua_ponto_saturacao_superplastificante_mehta = Calculo_Mehta.volume_agua_mehta()*volume; //Massa água
 String stg_resultado_agua_mehta = Double.toString(massa_agua_ponto_saturacao_superplastificante_mehta);
 resultado_agua.setText(stg_resultado_agua_mehta);

  • I am correctly returning the variables to the textviews, so much so that volume_agua_mehta() appears in the textview. If the others do not appear, it is because there is an error in the public part. Got it?!?! There I am wanting to understand this part.

  • @Anawaldila I made a change in the answer, see. One thing you have to look at is how you are defining the values within the Calculo_mehta class. If everything is ok, the return method should work correctly.

  • @Anawaldila actually makes it difficult to reproduce his mistake because he is not providing the whole code. So that’s why I’m asking you to check if you’re setting all the values correctly. If by any chance any method is returning error, will really give way.

  • I added more to my code, not all because there is so much more. Can you identify the error? I’ve looked at it so much I can’t see anything else.

  • @Anawaldila Did you check if it is a question of logic? Maybe you are making a calculation that is not possible.

  • @Anawaldila second method I put a fixed value in place of the variable and returned normally: return (130 * 5 + 0.319 * Math.log(32) - 3.332) * 23;

  • @Anawaldila Again I’ll point out, you have to check in the log if your variables are being assigned correctly.

  • @Anawaldila also noticed that when you change from one screen to another, you are passing only 2 variables through the extra, however your method has 4 methods for 4 different variables. Give me one more thought, later I’ll come back to help you.

  • Yes yes, I put part of the code, I did not put all because it is a little extensive, but the other variables I passed yes.

Show 6 more comments

0


The second method does not "work" because it uses attributes Calculo_Mehta.resistencia_concreto and Calculo_Mehta.massa_especifica_sss_brita, of the class Calculo_mehta, which are only initialized in the method onCreate().

You are using an Activity as if it were a "normal class".
In doing Calculo_Mehta Calculo_Mehta = new Calculo_Mehta(); is creating a Calculo_mehta object but the method onCreate() is not called.

An Activity should always be created (launched) via an Intent.

I can’t give you an alternative because I don’t understand/know the structure of the app, nor am I sure what you want to do.

However one thing is certain, this way is wrong and will not "work".

  • I understand. So for this case Do you find it interesting to put the accounts in Reply Activity even? I do not know if this question is appropriate, since I have not presented the layout here

  • That part of Oncreate() I used in Calculo_mehta then is wrong?

  • "Accounts" may be where they are used or in a class of their own, if they are used in more than one Activity they must be in a class of their own. An Activity should not be instantiated for its methods to be called by another.

  • I get it. Thank you very much!!!

Browser other questions tagged

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