Think carefully about good testing
Practice with writing javadoc
Download lab3 from your download directory. In there you will find a new version of the
Animal class you worked with in previous lab. Create a new BlueJ project, name it
Lab3Project_partner1_partner2, and add the Animal class to it.
Animal class you downloaded. In particular,
pay attention to the extra instance variable named
goodHealth, its getter and setter. Also, notice a new instance method, named
In the task you will write a program,
Zoo.java to create and maintain a Zoo. An instance of the Zoo class has animals, and the user should be able to
interact with it. For example, the user should be able to create a zoo,
to add animals to it, to remove animals, etc.
Use an array to hold the animals in the zoo. Start this array to
contain a number (any number) of Animal instances. Keep in mind that,
in general during execution, this container will not be full, but it will have available spots to accept more animals. (At the point it becomes full, it should be expanded so it can accommodate more animals. More on this later)
Therefore, the length of the array, in general, will not be the same as the number of animals in the zoo. That's why you need a variable (let's call it
size) to hold the size of your zoo, i.e the actual number of animals in there, at any point.
Now you should be able to set the instance variables in the
The next step is to define the constructor of the
Zoo class. In this method create the array, i.e allocate the necessary memory space, and initialize the instance variable
Define the method
toString() according to the way we have done so in previous classes. (Check the
Animal class, if you need a reminder.) This method should return a string representation of a
Zoo instance, that will contain the number of
Animal instances it contains, as well as the string representation of each animal in the zoo. Please make sure you use the
toString() method in the
Animal class, that has already been defined!
Time to add the
main() method to your class, to start testing your code. Create an (empty) Zoo instance, and print it. Is everything as expected?
addAnimal() method, to do exactly that: add an animal to the this zoo. Does this method need an input (typical) parameter? What type? Should this method return anything?
How should this method behave if there is no more available space in the array to hold the new animal?
Any other special case(s) that you might be thinking regarding the behavior of this method?
Make sure you state, in the top of the method comments, any assumptions you made in the process of defining this (and for that matter, any other) method.
removeAnimal() method, to remove an animal from this zoo. Does this method need an input (typical) parameter? Should it return anything? What would be the return type?
Actually, before you move on with the definition of the removeAnimal() method, define the
findAnimal() method. It can help you with the definition of the removeAnimal(), to keep its code more high-level and readable.
findAnimal() method should return the index in the array the animal was found at. Does this method need an input? What should it return if the animal was not found in the array? You will need to call the
equals() method here,defined in the Animal class.
Now you are in a great position to define the
Define a method,
getHealthyAnimals() to return an array with all the animals in the zoo whose health is good.
Define a method,
getNumOfHealthyAnimals() to return the number of healthy animals in the zoo.
Finally, define a method,
getNumOfSickAnimals() to return the number of sick animals in the zoo.
Take a look at the following resource(s):
javadoc from DrJava
javadoc from Oracle
Also, look at the
Animal.java you downloaded earlier.
Based on what you read, add some javadoc to your program.
Then, in the BlueJ Editor window, from the drop down menu, in the upper right corner, choose "Documentation". What do you see? Then open the folder that contains your project. Do you see anything new there?
Submit your work into your
lab03folder to submit. Control-click on the
lab03_userName1_userNamne2folder, and choose "Compress" from the pop-up menu.
Cyberduckto navigate back to your home directory, and then go to