So far we have interacted with the pre-written Cat class without looking at its source code. Let's now view the source code to see why the cat was meowing when we called the listen method (if only we could do this with real cats!). To open the source code for the Cat class, simply double-click on the Cat class (alternatively: right-click and select ). That will open up the Java source code editor for Cat in a new window.
One of BlueJ's distinctive editor features is the scope highlighting: the green, yellow and purple boxes drawn around different parts of the source code. We think this helps with reading the code. You can adjust the strength of the highlighting in BlueJ's preferences (or even turn it off) if you wish.
If you scroll down to find the listen() method, you'll see that there is a branching if statement inside. The cat meows if not fed, but purrs if it has been fed. However, there's no way to feed it! So, in the blank space between the yellow boxes of the methods, add this new method:
public void feed()
{
fed = true;
}
Once you have done this, switch back to the main BlueJ window. You'll see that the Cat class now has diagonal stripes across it. This means that it has been modified, but has not been compiled. There's several different ways to compile, either in the main window or in the editor window:
Do one of the above, and the slashes on the Cat class should disappear, indicating that it compiled successfully. Because you have changed the class, the object bench will have been cleared. We will need to make a new Cat object -- but this time we will do it using a different feature of BlueJ, in the next part.