Variables operating in more than one method

Asked

Viewed 543 times

3

I tried to make a simple console calculator, only I wanted to create several methods to make it well organized.

The problem is that in the method of storing the variables, it stores, but when it goes to the part of calculating it loses the values of the variables obtained in the past code.

Follow the code below:

public class Calculadora {

    //Declaracao de variaveis
    double num1, num2;

    double resultadoSoma, resultadoSub, resultadoMulti, resultadoDivi;
    Scanner sc = new Scanner(System.in);

    public static void main(String [] args) {
        new Calculadora().run();
    }

    public void perguntarValores() {
        System.out.println("Digite o primeiro valor: ");
        num1 = sc.nextDouble();

        System.out.println("Ok, digite o segundo valor: ");
        num2 = sc.nextDouble();
    }

    public void calcular() {
        resultadoSoma = num1 + num2;
        resultadoSub = num1 - num2;
        resultadoMulti = num1 * num2;
        resultadoDivi = num1 / num2;
    }

    public void exibirResultados() {
        System.out.println("O resultado em soma foi: " + resultadoSoma);
        System.out.println("O resultado em subtracao foi: " + resultadoSub);
        System.out.println("O resultado em multiplicao foi: " + resultadoMulti);
        System.out.println("O resultado em divisao foi: " + resultadoDivi);
    }

        public void run() {
        new Calculadora().perguntarValores();
        new Calculadora().calcular();
        new Calculadora().exibirResultados();
    }
}

4 answers

10

The problem is because you are instantiating the class for each method call. This way, java will create a different object for each call within the method run, change to this form:

 public void run() {
        Calculadoda calc = new Calculadora();
        calc.perguntarValores();
        calc.calcular();
        calc.exibirResultados();
    }
  • Ahh, thank you so much man!! D worked. I have a lot to learn yet :)

  • 1

    @Froks can improve, example, leave the calculator only to carry out the mathematical operations, getting 2 parameters, then you capture the numbers and displays in main.

9

The problem is that it is creating a new instance in each operation. I would do it another way, but if you’re going to do it that way, at least always use the same instance to preserve the values. As it stands, each operation is done on top of new data.

public void run() {
    Calculadora calc = new Calculadora();
    calc.perguntarValores();
    calc.calcular();
    calc.exibirResultados();
}

I put in the Github for future reference.

In this specific case it does not even need to have this instance created, it is that the class is confused.

3

Your mistake is in this excerpt:

  public void run() {
        new Calculadora().perguntarValores();
        new Calculadora().calcular();
        new Calculadora().exibirResultados();
    }

Here you create an instance to take the value, another to calculate and another to display the data.

To make it clearer:

  public void run() {
       Calculadora a =  new Calculadora().perguntarValores();
       Calculadora b = new Calculadora().calcular();
       Calculadora c= new Calculadora().exibirResultados();
    }

Each Calculator object performs a task!

To correct change to:

public void run() {
        perguntarValores();
        calcular();
        exibirResultados();
    }
  • Thiago, but the methods are not static, needs an object instance.

  • 1

    The run is also not! It is in the same context as the other methods! It belongs to the same class. When you create the object in main , this has the run method and the others

  • True! This xD detail went unnoticed

2

Well, you don’t have to instantiate the class again every time you access a method of it, you know when you give a "new calculator();" you’re creating another instance of that object in memory, with the values all zeroed, the right way to do it would be:

Calculadora calculadora = new Calculadora();
calculadora.perguntarValores();
calculadora.calcular();
calculadora.exibirResultados();

This way, in a single instance of your calculator class, you access all public methods of the same.

As I said earlier, for every "NEW" you make, it’s a different session from that object in memory. then if you give three "New Calculator();", it’s like you have three different calculators in your hand, obviously what you calculate on one calculator, not this on the other.

Browser other questions tagged

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