CS111

Lab 10: Objects

Wed November 06

Making your own objects: Animals

Today you will: Download the lab10_programs folder on your Desktop, and save all your files in that folder.

Task 1: The Animal Class

After downloading the lab10_programs folder onto your Desktop, start DrJava. Within DrJava, create a file called Animal.java. Make sure the file starts with a capital "A". Save Animal.java in your lab10 folder.

Step 1: Define your instance variables

Although there are many properties that one might choose to describe an animal, today we will stick with these:

Step 2: Write a constructor for your Animal class

The job of a constructor is to initialize the values of each instance variables. For your first constructor, you can assume that there are 5 parameters, one for each of the instance variables. For example, here are a couple sample invocations of your constructor:
   Animal doggy = new Animal("dog","Rover","bark",false,4);
   Animal deer  = new Animal("reindeer","Rudolph","hi",true,4);
   

Step 3: Write your "getter" methods

Getter methods simply return the value of each instance variable. For your Animal class, you will need getType(), getName(), getVoice(), getCanFly(), and getNumber_legs().

Step 4: Write your "setter" methods

Parallel to your "getter" methods, your setter methods allow you to change the values of your instance variables. Setter methods are always void. Each setter method will take a parameter with the value to assign to a particular instance variable. For example, the setType method will take a String parameter and the setCanFly method will take a boolean parameter.

Step 5: Write a main method

Create a static main method so that you can create Animal objects and test out your instance methods and make sure everything is working correctly. The header of the main method always looks exactly the same:
public static void main (String [] args) 

Step 6: Write a toString() method

We always recommend writing a toString() method. This allows you to see the current state of your object and is really useful when debugging. Write your toString method so that produces something similar (doesn't have to be exact) to this:
Code in main() Text produced in DrJava Interactions Pane
System.out.println(doggy.toString()); Rover is a dog with 4 legs that cannot fly that says bark.
System.out.println(deer);
// Note: toString() call is implicit
Rudolph is a reindeer with 4 legs that can fly that says hi.

Step 7: Create some animals and test what you have written so far

Here is some sample testing in the main method. Feel free to make up your own test cases.
	Animal duck1 =  new Animal("duck","daffy","quack",true,2);
	Animal spidey =  new Animal("spider","charlotte", " ",false,8);
	Animal froggy = new Animal("frog","bill","ribbit",false,4);

	System.out.println(duck1.getType());   // duck 
	System.out.println(duck1.getName());   // daffy 

	System.out.println(duck1.getVoice());  // quack 

	System.out.println(duck1.getCanFly()); // true

	System.out.println(duck1.getNumber_legs()); // 2 


	System.out.println(froggy);
        //bill is a frog with 4 legs that cannot fly that says ribbitt

	System.out.println(froggy.getType());   // frog 
	froggy.setType("prince");
	System.out.println(froggy.getType());  // prince 
	froggy.setNumber_legs(2); 
	froggy.setName("Charming");

	System.out.println(froggy);
        //Charming is a prince with 2 legs that cannot fly that says ribbitt


	System.out.println(spidey);
        //Charlotte is a spider with 8 legs that cannot fly that says 

Step 8: Write some more instance methods (and test them!)

Step 9: Write a second constructor, with fewer parameters

Write another constructor that requires that the animal's type and name are provided, but supplies default values as follows: voice is empty, number of legs is 4, and canFly is false.

 Animal a3 = new Animal("dog","lassie");
 System.out.println(a3);
 // lassie is a dog with 4 legs that cannot fly that says 

 Animal a4 = new Animal("eagle","ralph");
 System.out.println(a4);
 // ralph is a eagle with 4 legs that cannot fly that says 



Creating a Zoo (a collection of animals)

Now, create a new file containing a new class called Zoo.java. In this class, we'll use a list to hold a collection of Animal objects. We will use the ObjectList implementation of a list.

Step 1: Create your Zoo instance variables

Your Zoo class will contain these two instance variables:

private ObjectList animalCollection;  // linked list to hold the animals
private int num_animals; // how many animals are in the zoo

Step 2: Other methods to define

Some sample testing output:

	Zoo brooklyn = new Zoo();
	System.out.println(brooklyn);	
	Animal inch = new Animal("worm","Pete"," ",false,0);
	Animal hedgey = new Animal("hedgehog", "Mary", "grr",false,4);
        // note use of different constructor
	Animal pig1 = new Animal("pig","Wilbur");
	Animal pig2 = new Animal("pig","Wendy","oinky",true,4);
	Animal bird1 = new Animal("duck","Janet","quack",true,2);
	Animal spider1 = new Animal("spider","Charlotte","Amazing",false,8);
	// add the 6 animals to the brooklyn zoo
	brooklyn.addAnimal(inch);
	brooklyn.addAnimal(hedgey);
	brooklyn.addAnimal(pig1);
	brooklyn.addAnimal(pig2);
	brooklyn.addAnimal(bird1);
	brooklyn.addAnimal(spider1);
	System.out.println(brooklyn);	
	brooklyn.printFlyingAnimals();
	brooklyn.printAnimalsWithLegs(2);
	brooklyn.printAnimalsWithLegs(4);
	brooklyn.printAnimalsWithLegs(8);

produces this in DrJava's interaction pane:

This zoo contains the following 0 animals: [ ]
This zoo contains the following 6 animals: [Charlotte is a spider with 8 legs 
 that cannot fly that says Amazing, Janet is a duck with 2 legs that can 
 fly that says quack, Wendy is a pig with 4 legs that can fly that says oinky,
 Wilbur is a pig with 4 legs that cannot fly that says oink, Mary is a 
 hedgehog with 4 legs that cannot fly that says grr, Pete is a worm with 0 
 legs that cannot fly that says  ]
Here are the flying animals:
  Janet is a duck with 2 legs that can fly that says quack
  Wendy is a pig with 4 legs that can fly that says oinky
Here are the animals with 2 legs
  Janet is a duck with 2 legs that can fly that says quack
Here are the animals with 4 legs
  Wendy is a pig with 4 legs that can fly that says oinky
  Wilbur is a pig with 4 legs that cannot fly that says oink
  Mary is a hedgehog with 4 legs that cannot fly that says grr
Here are the animals with 8 legs
  Charlotte is a spider with 8 legs that cannot fly that says Amazing