There is a cat's weight, there is a maximum and minimum possible weight, when the cat goes beyond the allowable weight, the cat dies. How to freeze the variable "weight", i.e. so that the weight of the dead cat could not be changed?
I tried to use final, but in my opinion it only works when declaring a new variable. Or it was not necessary to write an if condition in the constructor, although when writing in a class, everything gets worse ...
public Cat() { weight = 1500.0 + 3000.0 * Math.random(); originWeight = weight; minWeight = 1000.0; maxWeight = 9000.0; count++; if(weight < minWeight || weight > maxWeight) { this.weight = final(); } } Hmm, what's wrong with a setter with a check? ru.wikipedia.org/wiki/Setter#Java
Something is already there. But I do not understand how to fix the weight?
public String getStatus() { if(weight < minWeight) { count--; return "Dead"; } else if(weight > maxWeight) { count--; return "Exploded"; } else if(weight > originWeight) { return "Sleeping"; } else { return "Playing"; } }