Variables that work in more than one method

4

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

The problem is that in the method of storing variables, it stores, but when it goes to the compute part it loses the values of the variables obtained in the last 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();
    }
}
    
asked by anonymous 09.03.2016 / 22:12

4 answers

11

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

 public void run() {
        Calculadoda calc = new Calculadora();
        calc.perguntarValores();
        calc.calcular();
        calc.exibirResultados();
    }
    
09.03.2016 / 22:21
7

The problem is that you are creating a new instance in each operation. I would do it in a different way, but if it is going to do this way, pleo least always use the same instance to preserve the values. As it is, every operation is done on top of new data.

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

In this specific case you do not even need to have this instance created, the class is confusing.

    
09.03.2016 / 22:22
3

Your error is in this section:

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

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

To get 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 fix change to:

public void run() {
        perguntarValores();
        calcular();
        exibirResultados();
    }
    
09.03.2016 / 22:22
2

Well, you do not need to instantiate the class again every time you access a method, understand when you give a "new Calculator ();" you are creating another instance of this object in memory, with the values all zeroed, the right way you would do would be:

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

In this way, in a single instance of your calculator class, you access all public methods of it.

As I said earlier, for every "NEW" you do, it's a different session than that object in memory. so if you give three "new Calculator ();" it's as if you have three different calculators in your hand, obviously what you calculate in one calculator, not in the other.

    
09.03.2016 / 22:24