"Hi, Amigo. A couple more tasks for you:"
Cat's finalize method
It's difficult to accidentally lose an object: as long as you have even one reference to an object, it remains alive. But if not, then the object is approached by the finalize method, an unpredictable assassin that works for the Java machine. Let's create this method ourselves: protected void finalize() throws Throwable. The last two words will become clear a little later.
Zombie cats, zombie dogs
The Grim Reaper has come to CodeGym. In the previous level, we often "constructed" cats and dogs (using the constructors of the Cat and Dog classes) and created new instances of cats and dogs (objects). Now it's time to destroy objects that no one is referencing. In the Cat and Dog classes, write a finalize method, which displays text about the destruction of objects.
Cat and Dog objects: 50,000 each
If you have not yet been touched by the beauty of loops in programming, try finishing this task without them. Of course, this task isn't so much about loops as it is about the destruction of unused objects. Anyway, create 50,000 each of Cat and Dog objects in a loop. Doing this will force the Java machine to call the finalize method at least a few times.
There is balance in the universe. Stuff arrives somewhere and departs from somewhere else. It's not entirely clear what is flowing through our galaxy or where it is going. But it doesn't work this way with a computer and its discrete memory. Accordingly, you need to edit a couple of important things in this program. In the Cat class constructor, we will increment a counter by 1. Then in the finalize method, we'll decrement it.
Even and odd digits
Let's determine how many even digits and how many odd digits are in a number entered from the keyboard. If a number is divisible by 2 without a remainder (i.e. the remainder is zero), then it is even. And we'll increase the even digit counter (static variable even) by 1. Otherwise, the number is odd, so we'll increase the odd digit counter (static variable odd).