How can I perform several operations on the same attribute?

0
public class MiNumero {
private int Valor;
private int NumSuma;
private int NumResta;

public int getNumResta() {
    return NumResta;
}

public void setNumResta(int numResta) {
    NumResta = numResta;
}

public int getValor() {
    return Valor;
}

public void setValor(int valor) {
    Valor = valor;
}

public int getNumSuma() {
    return NumSuma;
}

public void setNumSuma(int numSuma) {
    NumSuma = numSuma;
}

public MiNumero() {
    this.Valor = 0;
}

public MiNumero(int Valor) {
    this.Valor = 10;
}

public int suma(int NumSuma) {
    return Valor + NumSuma;
}

public int resta(int NumResta) {
    return Valor - NumResta;
 }
}
import java.util.Scanner;
public class MiNumeroApp {
public static void main(String[]args) {
    MiNumero a = new MiNumero();
    Scanner sc = new Scanner(System.in);
    System.out.println(a.getValor());
    System.out.println("Introduce un numero para sumar:");
    a.setNumSuma(sc.nextInt());     
    System.out.println(a.suma(a.getNumSuma()));
    System.out.println("Introduce un numero para restar:");
    a.setNumResta(sc.nextInt());
    System.out.println(a.resta(a.getNumResta()));
 }
}

How can I do this?

Write a class in Java called MyNumber

  • Sum method (int): add a quantity to the number
  • Subtract method (int): subtract a quantity from the number
  • Method getValor () returns the current value of the number (the result of having made a sum and then a subtraction)

    
asked by Vampy95 22.10.2017 в 15:28
source

1 answer

2

Solution

When performing the operations you do not get the values you want since in the methods suma() and resta() you are only returning the result of the operation, but you are not storing that value in any variable. That is why you always get the initial value of the variable Value, since at no time do you assign the value of addition and subtraction. To achieve what you want, store the value of the operations in the variable Valor , that way you will get the result you want.

public int suma(int NumSuma) {
    // Realizas la operación y le asignas el resultado
    // a la variable Valor
    return Valor = Valor + NumSuma;
}

public int resta(int NumResta) {
    // Realizas la operación y le asignas el resultado
    // a la variable Valor
    return Valor = Valor - NumResta;
}

Logical errors

In your code you have several logic errors and you are not applying the best practices. It cost me to find the constructors of your class MiNumero , since you did not declare them at the beginning. The constructor must be the first or the first methods that you declare in your class, remember that the constructor is the input method of an object, the first to be executed.

Builders

Another thing, in the constructor that receives the parameter Valor , you are not assigning it the value it receives to anything, but that variable Valor you are assigning a static value, I do not know if you do it intentionally or if it really is a mistake. It does not have logic that you declare a constructor that receives a parameter and the value of that parameter does not assign it to anything. Also, it is not necessary to initialize the variable Valor with 0 , the default value of a variable of type int is 0 .

Incorrect ❌

// El parámetro que recibe no se lo estas 
// asignando a nada
public MiNumero(int Valor) {    
    this.Valor = 0;
}

Correct ✔

// El parámetro que recibe se lo asignas a 
// la variable Valor 
public MiNumero(int Valor) {
    this.Valor = Valor;
}

Variables

By convention the names of the variables must start with lowercase and here you are declaring all variables with a capital letter. Besides that it is incorrect, it is uncomfortable to read and generates confusion.

Incorrect ❌

private int Valor;
private int NumSuma;

Correct ✔

private int valor;
private int numSuma;

Finally, your code should look like this:

 public class MiNumero {

    private int valor;
    private int numSuma;
    private int numResta;

    public MiNumero() {}

    public MiNumero(int valor) {
        this.valor = valor;
    }

    // METODO GET Y SET
    ...

    public int suma(int numSuma) {
        return valor = valor + numSuma;
    }

    public int resta(int numResta) {
        return valor = valor - numResta;
    }
}
    
answered by 22.10.2017 / 18:03
source