Making your own objects: Animals
Today you will:- write your own class in java, which includes:
- instance variables
- constructor(s)
- instance methods
- a main method (for testing)
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:-
type
: the type of animal (e.g. horse, frog, dog, etc) -
name
: the name of the animal (e.g. Tonto, Froggy, Spot, etc) -
voice
: the sound the animal makes (e.g. neigh, ribbitt, bark) -
canFly
: indicates if the animal can fly (true) or not (false) -
number_legs
: the number of legs the animal has
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 yourAnimal
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, thesetType
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 createAnimal
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 atoString()
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 themain
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!)
-
tookFlyingLessons()
: sets canFly to be true -
caughtLaryngitis()
: sets voice to be silent (empty string) -
likesToImitateCats()
: sets voice to be a cat sound -
speak()
: Prints out the animal's voice three times (for example: meow meow meow) -
conversation(Animal a2)
: Returns a String that contains a conversation between two animals. For example:Animal a1 = new Animal("dog","lassie","ruff",false,4); Animal a2 = new Animal("cow","helga","moooo",false,4); System.out.println(a1.conversation(a2)); a1.setVoice("everyone dance!"); System.out.println(a1.conversation(a2));
A conversation between lassie and helga: ruff moooo ruff moooo A conversation between lassie and helga: everyone dance! moooo everyone dance! moooo
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 calledZoo.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
YourZoo
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
- a constructor for the
Zoo()
class. Note that the constructor for theZoo
class has no parameters. It simply creates an empty Zoo with no animals in it. All Zoos are created the same way (much like the Buggle constructor). -
toString()
that will print out the whole collection of Animals instances. Remember that you can use the toString method from the Animal class -
main()
for testing each method as you write it
-
addAnimal(Animal a)
adds the given Animal to the animalCollection -
printFlyingAnimals()
prints out all the animals who can fly. Note that this should be a void method, it merely prints.
-
printAnimalsWithLegs(int legs)
prints out all the animals who have the number of legs specified in the legs parameter. This method is void, it merely prints the animals that have legs number of legs.
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