CS 1063
Introduction to Computer Programming Project 1
The Pet Class
Objectives
- Construct and use Objects
- Design the public interface of a class
- Determine the instance variables necessary for a class
- Implement the shell of a class using stubs with comments
- Implement constructors
- Implement simple methods
- Access instance fields and local variables
Hand-in Requirements
All projects and laboratories will be submitted electronically through
webCT. Zip up your entire project directory to submit as the source.
(Right click on the project folder and follow the SentTo link.)
The project directory should include the following:
- Public Interface as a word document
- Pet.java
- PetTest.java
Details
1. Write a class named Pet that has the
following
data fields:
- myName The
myName
field is a String
object that holds the name of the pet.
- myAnimal The myAnimal field
is a String
object that holds the type of animal that a pet is.
Example “Dog”, “Cat” etc
- myAge The myAge field is
an int
that holds the age of the animal.
2. In addition, the class
should have the following methods:
- Constructor The constructor should accept the animals
name, kind and age. These values should be
assigned to the object’s myName, myAnimal and myAge.
- Write appropriate methods to get the values stored in an object’s myName, myAnimal and myAge. (3 methods)
- void ageAnimal The ageAnimal method
should add 1 to the age field everytime it is called.
- void setName
The setName(String
name) method takes a name and
stores it in the name field.
- String toString
Returns the state of the object as a String
3. Test your class by doing
each of the following:
- Instantiate a Pet object p1 using
“Willie”, “Dog” and 4
- Print all of the values stored in
the private data with a label
- Call the method ageAnimal 2 times
- Print the age of p1 with a label
- Set the name of the dog to “Willy”
- Print the name of p1 with a label
Last revision: February
6, 2004 at 4:55 pm