CS230 Lab: Arrays of Objects



Lab: Arrays of Objects


Practice with:

  1. user-defined classes in java
  2. arrays, and in particular arrays of Objects

Think carefully about good testing

Practice with writing javadoc

Set up

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.

[PRELAB] Task 1: The Animal Class - new version

Study the 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 equals().

Task 2: The Zoo Class

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.

[PRELAB] Task 2-1: The instance variables and the constructor

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 Zoo class!

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 size.

Task 2-2: The toString() and main() methods

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?

Task 2-2: The addAnimal() method

Define the 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.

Task 2-3: The removeAnimal() and findAnimal() methods

Define the 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.

The 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.

Test the findAnimal() method.

Now you are in a great position to define the removeAnimal() method.

Task 2-4: Do we have a vet in the house?

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.

Task 2-4: Add some javadoc

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?

Task 3: Turn in your lab (electronically)

Submit your work into your cs230-labs folder.

  • Compress your lab03 folder to submit. Control-click on the lab03_userName1_userNamne2 folder, and choose "Compress" from the pop-up menu.
  • Use Fetch or Cyberduck to navigate back to your home directory, and then go to cs230/cs230-labs.
  • Drag the .zip file from your Desktop into this folder.
  • Make sure you upload the lab into both partners accounts.