public class Herbivore extends Animal
Herbivore
is an Animal
with extra methods that
allow it to eat Plant
objects.
Java Source Code for this class:
http://www.cs.colorado.edu/~main/edu/colorado/simulations/Herbivore.java
Constructor and Description |
---|
Herbivore(double initSize,
double initRate,
double initNeed)
Construct an
Herbivore with a specified size, growth rate, and
weekly eating need. |
Modifier and Type | Method and Description |
---|---|
void |
nibble(Plant meal)
Have this
Herbivore eat part of a Plant . |
eat, getNeed, setNeed, simulateWeek, stillNeed
public Herbivore(double initSize, double initRate, double initNeed)
Herbivore
with a specified size, growth rate, and
weekly eating need.initSize
- the initial size of this Herbivore
, in ouncesinitRate
- the initial growth rate of this Herbivore
, in ouncesinitNeed
- the initial weekly eating requirement of this Animal
, in
ounces per week
Precondition:
initSize >= 0
and initNeed >= 0
.
Also, if initSize
is zero, then
initRate
must also be zero.
Postcondition:
This Herbivore
has been initialized. The value returned from
getSize()
is now initSize
, the value
returned from getRate()
is now initRate
, and
this Herbivore
must eat at least initNeed
ounces
of food each week to survive.java.lang.IllegalArgumentException
- Indicates that initSize
, initRate
, or
initNeed
violates the precondition.public void nibble(Plant meal)
Herbivore
eat part of a Plant
.meal
- the Plant
that will be partly eaten
Postcondition:
Part of the Plant
has been eaten by this Herbivore
,
by activating both eat(amount)
and
meal.nibbledOn(amount)
. The amount
is usually
half of the Plant
, but it will not be more than 10% of
this Herbivore
’s weekly need nor more than the amount that
this Herbivore
still needs to eat to survive this week.