check in settter - java method

Asked

Viewed 41 times

-1

I am doing an exercise in java that asks to calculate the area of a rectangle, if the usurer type some number smaller or equal that "0.0" should appear an error message, but this verification must be done in the settter method. Is that possible? If yes, how can I do it ?

1 answer

1


Yes, it is possible to do this type of check in the set method

public void setValor(double valor) {
    if(valor > 0.0) {
         this.valor = valor;
    }

    else {
        // Mensagem de erro aqui
    }
}

Browser other questions tagged

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