Content
2.7
The
JavaZOO
2.7.2
Objects
In the first section of the JavaZOO, three
type of objects found in the zoo were described: zookeepers, cages, and
animals. Now the programmer will define the animal and cage objects.
For animal objects, every animal will
have a name. It is also necessary to know what type of animal it is. The
color and age of the animal should be included in the definition as
well. It is important to know whether or not an animal is hungry. When
an animal is hungry, a zookeeper can feed it. These requirements are the
definition for every animal that will be in this zoo:
Every ZooAnimal has a name.
Every ZooAnimal is of a certain type.
Every ZooAnimal has a color.
Every ZooAnimal has an age.
Every ZooAnimal is hungry or is not
hungry.
So, the definition of a ZooAnimal looks
like this:
ZooAnimal
name is ?
type is ?
color is ?
age is ?
isHungry is ?
Now that a ZooAnimal has been defined, it
must be housed in a cage. To design the definition of a cage, its
attributes must be determined. Every cage in this zoo has a certain
length, width, and height. Also, a cage will either be covered or
uncovered by a tarp. Sometimes a cage becomes dirty. If a cage is dirty,
a zookeeper should clean the cage.
So, the definition of a cage looks like this:
Cage
length is ?
width is ?
height is ?
IsCovered is ?
IsDirty is ?
Before continuing, a closer look at the
types of information stored in these definitions is in order. The three
types of information are words, numbers, and a value that is either
"true" or "false". Use Figure
to practice
associating attributes with data types.
For the JavaZoo, when a ZooAnimal is
created, its name will be a word like 'Maurice' or 'Sophia'. For the Cage,
its length will be a number like 20 or 57. But what about the
characteristic isCovered? A cage is either covered or it is not. The
characteristic isCovered is not a word or a number. Think of isCovered
as a question with an answer of true or false. Using these three types
of information, the definitions created above for ZooAnimal and Cage are
defined more clearly by including the type of information each
characteristic should be:
ZooAnimal
name is a word
type is a word
color is a word
age is a number
isHungry is true or false
Cage
length is a number
width is a number
height is a number
IsCovered is true or false
IsDirty is true or false
The basic instructions for each ZooAnimal and Cage object have been created. Although
each ZooAnimal created with this definition may have different names and
be of different types, all will be a ZooAnimal object. Additionally, the
cages may be of different sizes since a ZooAnimal of type elephant would
require a very large cage whereas a ZooAnimal of type mouse only
requires a small cage. Regardless if the cage is for an elephant or a
mouse, it is still a Cage object.
Now specific ZooAnimal and Cage objects
can be created. This is done in two steps. First, the ZooAnimal and Cage
each need a reference name. This is not to be confused with the name
attribute in the ZooAnimal. The name for the entire object is a way to
refer to it. For example, if there are many ZooAnimals created in the
JavaZOO, this name will serves as a means for telling them apart. This
name can be any word or multiple words joined without spaces, but it is
always a good idea to use something descriptive. The reference names
chosen below are "myFirstZooAnimal" and "myFirstCage".
Once the ZooAnimal and Cage have
reference names, look at the definitions and replace the types of
information with actual information to describe the ZooAnimal and Cage,
like "Pecanzo", "elephant", and "gray". Be
sure to provide words, numbers, and either a true or false for each
attribute:
ZooAnimal myFirstZooAnimal
name is Pecanzo
type is elephant
color is gray
age is 15
isHungry is false
Cage myFirstCage
length is 100
width is 240
height is 12
isCovered is false
In the above example, myFirstZooAnimal is
a gray elephant named Pecanzo. It is 15 years old and is not hungry.
Also, myFirstCage is an uncovered cage with dimensions of 100 m X 240 m
X 12 m.
This section of the JavaZoo created
definitions for the ZooAnimal and Cage objects. Then the
characteristics, such as name, length, or isHungry, that describe each
object were added. Next, each characteristic is assigned a type of
information. The three types of information, or data types, in this
example are word, number, and true or false (isHungry). Actual
information then replaces these data types so that a specific ZooAnimal
and Cage are created. A reference name is also assigned to distinguish
each individual object.
Now use Figure
to design
the definition of a ZooKeeper.
Wyszukiwarka
Podobne podstrony:
contentcontentcontentcontentcontentcontentcontentcontentcontentfunction domnode get contentcontentcontentcontentcontentcontentcontentwięcej podobnych podstron