The code for the Pet class is contained in Pet.java. A demo program that uses the Pet class is in PetDemo.java.
This lab is a little hard to follow, so read it closely.
1. Seeing How it Works
Download the two source files into your labs folder. Compile and run the program PetDemo.java. Do what it says. Enter a reasonable name for the dog object, and reasonable numbers. Try out all the menu choices, and satisfy yourself that you know what the program is doing. You may also want to read through PetDemo.java to see that it works the way you expected it to.
2. Creating and Using an Object
Right now, all the code is in place for a dog.
myDog is an instance of the Pet class. We want to add a cat, in addition to the dog.
To do this, add code to PetDemo.java to create an instance of the Pet object called myCat. Look at all the uses of myDog in that file and copy them, change the instance name to Cat. Now we've got cats and dogs.
(So, for example add an assignment statement like myDog.kindOfPet = "Dog" which reads myCat.kindOfPet = "Cat".)
Run the program again. Make sure you invoke the speak() method for myCat as well as myDog. Do you get a reasonable output?
3. Modifying a Method
In the file Pet.java, modify the method speak() so that a "Bird" says "Tweet". You will just be adding a branch to the if statement. Then, in the file PetDemo.java, add code to create a Bird object (i.e., change all instances of Dog in the original code to Bird). As above, run it again, and satisfy yourself that it is running as expected. (So now we've got just cats and birds.)
4. Understanding Local Variables and Parameters
Add the line
5. Public versus Private Instance Variables
Modify Pet.java so that the instance variable kindOfPet is private rather
than
public. That is, the line that originally had,
public String kindOfPet = "Unknown";
should now read,
private String kindOfPet = "Unknown";
Try compiling the program now. What happens? Can you guess why this happens?
Make the instance variable name private in the same way.
6. Writing New Methods
In order to get at those private instance variables, we need methods. Some space (and documentation) for these new methods are contained near the end of the file Pet.java. The method headers are there; you just fill in the statement in each of the methods. Take a look.
Write an "accessor" method for name. The name of the method should be getName (with no parameters), and it should return a String equal to whatever is stored in the instance variable name. The code in this method consists of one line, which is a simple return statement!
Similarly, write a method setKind that stores a string in the instance variable kindOfPet. The method setKind should have one String parameter. The code is only one line again!
Now modify PetDemo so that it works again. You will have to
replace the references to the instance variables myBird.name and
myBird.kindOfPet
with invocations of the right methods. For getting at the name, the method
invocation you should have is,
myBird.getName()
while for setting the kind of pet, it should be,
myBird.setKind("Bird");
See if the program compiles now, and if it does, run and test it.
Back to Labs
7. Finish. Be sure to sign the attendance sheet, and turn in the form, source code and a run (for part 6).
This lab designed by: Frederic Green, fgreen@clarku.edu