CS230

 

Solutions

 

Arrays of Objects

Goals

Practice with:

  1. user-defined classes in java
  2. overriding methods (toString(), equals())
  3. arrays, and in particular arrays of Objects
  4. think carefully about good testing
  5. practice with writing javadoc
  6. work with command-line arguments

[PRELAB]

Task 1: The Animal Class - new version

Study this newer version of the Animal class. In particular, pay attention to an extra instance variable, named goodHealth and its getter and setter. Also, notice a new instance method, named equals(). This is an important method for our purposes of maintaining a collection of Animal objects! What does it mean for two animal instances to be "equal" according to the way this method is defined?

[END of PRELAB]

Set up

Create a new folder on the Desktop, and name it lab4_userName1_userName2. In BlueJ, create a new project, name it lab4Project and save it in the newly created folder. Add a class, named Animal to the project.

Open this link Animal class definition in a new tab. Then Copy and Paste the whole web page's content in the Animal class.

Task 2: The Zoo Class

In this task you will write a class from scratch, namely the class 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.

Task 2-1: The instance variables and the constructor

Use an array as the container for the animals in the zoo. Start this array with a certain capacity (of your choice). Keep in mind that, in general during execution, the array will not be full, but it will have available spots to accept more animals. (When it becomes full, it can be expanded so that 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 the animals it contains! That's why you need a variable -let's call it size- to hold the actual number of animals the array contains 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 in 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(). This method should return a string representation of a Zoo instance, including the number of Animal instances it contains, as well as a description of each animal instance 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-3: Command-line arguments

In the main() method, arrange for the program to take one command-line argument: the initial capacity of the Zoo. Think, how can you test this setup?

Command-line arguments means that your program will expect input from the command-line at the time the program is executed.

When command-line arguments are provided, they are stored in the String[] args, which is the input to the main method:

 public static void main (String[ ] args)

This means the first argument is stored in args[0], the second in args[1], and so on.

A good resource on command-line arguments can be found here:

Java's Command-line Arguments tutorial

Test your code to verify that the array was indeed created with the command-line inputted capacity!

Task 2-4: Adding animals to our zoo

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 javadoc comments, any assumptions you made in the process of defining this (and for that matter, any other) method.

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

Define a method, getNumOfAnimals() to return the number of all animals in the zoo.

Define a method, getNumOfHealthyAnimals() to return the number of healthy animals in the zoo.

Define a method, getNumOfSickAnimals() to return the number of animals in the zoo that are not healthy.

Finally, define a method, getHealthyAnimals() to return an array with all the animals in the zoo whose health is good. This is a trickier method because you need to return an array of animals, not just an integer. How big should this array be? Is there a method that you have already written that can tell that?

Task 2-6: Finding animals in the zoo

Define a method, findAnimal(), which 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.

Make sure to test this method method thoroughly.

==================================================

(As time permits) Task 2-7: Expand Zoo capacity

Define a method expandZoo() to expand the Zoo capacity. Make it so that the capacity of the Zoo doubles after calling this method. When would such a method be called? What kind of tests can you run to make sure this method works as expected?

Task 2-8: The removeAnimal() method

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? Can we invoke one of the existing methods in our design of this method?

Optional Task 2-9: Add some javadoc

Take another look at the following resource(s):

javadoc from Oracle

Also, look at the Animal.java you downloaded earlier.

Add some good 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: Good work! Don't forget to turn in your lab (electronically)!