How can I create a method to change the value of an attribute?

-2

Write a class called MiNumero in Java. The methods of the class should be:

  • Constructor without parameters (will set the number to zero)
  • Constructor with integer parameter (will assign that value to the number)
  • Method cambiaNumero(int) : allows you to assign a new value to the number

The code:

public class MiNumero {
    private int Valor;
    private int camValor;

    public int getCamValor() {
        return camValor;
    }
    public void setCamValor(int camValor) {
        this.camValor = camValor;
    }
    public int getValor() {
        return Valor;
    }
    public void setValor(int Valor) {
        this.Valor = Valor;
    }
    public int cambiaNumero(int camValor) {
        return Valor = camValor;
    }
    
asked by Vampy95 21.10.2017 в 23:16
source

4 answers

0
public class miNumero {

public miNumero() {
    valor=0;
}

public int valor;

/**
 * Get the value of valor
 *
 * @return the value of valor
 */
public int getValor() {
    return valor;
}

/**
 * Set the value of valor
 *
 * @param valor new value of valor
 */
public void setValor(int valor) {
    this.valor = valor;
}

  public int cambiarNumero;

/**
 * Get the value of cambiarNumero
 *
 * @return the value of cambiarNumero
 */
public int getCambiarNumero() {
    return cambiarNumero;
}

/**
 * Set the value of cambiarNumero
 *
 * @param cambiarNumero new value of cambiarNumero
 */
public void setCambiarNumero(int cambiarNumero) {
    this.valor = cambiarNumero;
    this.cambiarNumero=valor;
}

}

According to the explanation, this would be like this.

    
answered by 22.10.2017 в 00:05
0

I think this is what you mean

public class MiNumero {

private int Valor;
private int camValor;

public MiNumero() {         //Constructor sin parametros
    this.camValor = 0;      //Asignando valor de 0
}

public MiNumero(int numero) {    //Constructor con parametros
    this.camValor = numero;      //camValor recibe el valor del parametro numero
}

public int getCamValor() {
    return camValor;
}

public void setCamValor(int camValor) {
    this.camValor = camValor;
}

public int getValor() {
    return Valor;
}

public void setValor(int Valor) {
    this.Valor = Valor;
}

public int cambiaNumero(int camValor) {
    return Valor = camValor;
}

}

and then in the main class you only change the JOptionpane for the scanners

public class Principal1 {

public static void main(String[] args) {
    MiNumero miNumero = new MiNumero(); //Aqui se le asigna el valor de 0 a camValor        

    int num = Integer.parseInt(JOptionPane.showInputDialog("Escriba el valor"));

    MiNumero miNumero2 = new MiNumero(num);//Aqui se le asigna el valor de num a el constructor con parametro

    int nuevoNumero = Integer.parseInt(JOptionPane.showInputDialog("Escriba el nuevo valor"));

    miNumero.setCamValor(nuevoNumero);//se le envia el valor del nuevo numero a camValor
}

}

    
answered by 22.10.2017 в 00:28
-1

I do not know if your question is about the variable camValor, but if so, it's simple.

public static void Main(String args []){
Mi numero miNum = new MiNum();
miNum.setCamValor(valor);
}

That should change the value of your variable.

But if you are working with Scanner or JOption:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String numero=br.readLine();
miNum.setCamValor(Integer.parseInt(numero));

String numero=JOptionPane.showInputDialog("Inserte Numero");
miNum.setCamValor(Integer.parseInt(numero));
    
answered by 21.10.2017 в 23:34
-1

You have it there, the 'set' methods that you have created allow you to change the value of your attributes.

I give you an example of its application:

//Creacion del objeto de la clase
MiNumero numero = new MiNumero();
//Puesto que no tienes constructor con paramétros, los atributos valor y camValor se inicializan a 0.

numero.setValor(5); //Esto cambia el valor del atributo valor de 0 a 5
numero.setCamValor(10); //Esto cambia el valor del atributo camValor de 0 a 10
    
answered by 21.10.2017 в 23:36