Constructor and Description |
---|
Animal(double initSize,
double initRate,
double initNeed)
Construct an
Animal with a specified size, growth rate, and
weekly eating need. |
Modifier and Type | Method and Description |
---|---|
void |
eat(double amount)
Have this
Animal eat a given amount of food. |
double |
getNeed()
Determine the amount of food that this
Animal needs each
week. |
void |
setNeed(double newNeed)
Set the current growth weekly food requirement of this
Animal . |
void |
simulateWeek()
Simulate the passage of one week in the life of this
Animal . |
double |
stillNeed()
Determine the amount of food that this
Animal still needs to
survive this week. |
public Animal(double initSize, double initRate, double initNeed)
Animal
with a specified size, growth rate, and
weekly eating need.initSize
- the initial size of this Animal
, in ouncesinitRate
- the initial growth rate of this Animal
, 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 Animal
has been initialized. The value returned from
getSize()
is now initSize
, the value
returned from getRate()
is now initRate
, and
this Animal
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 eat(double amount)
Animal
eat a given amount of food.amount
- the amount of food for this Animal
to eat (in ounces)
Precondition:
amount >= 0.
Postcondition:
The given amount (in ounces) has been added to the amount of food that
this Animal
has eaten this week.
throw IllegalArgumentException
Indicates that amount
is negative.public double getNeed()
Animal
needs each
week.Animal
needs to survive
one week (measured in ounces)public void setNeed(double newNeed)
Animal
.newNeed
- the new weekly food requirement for this Animal
(in ounces)
Precondition:
newNeed >= 0.
Postcondition:
The weekly food requirement for this Animal
has been set to
newNeed
.java.lang.IllegalArgumentException
- Indicates that newNeed
is negative.public void simulateWeek()
Animal
.
Postcondition:
The size of this Animal
has been changed by its current
growth rate. If the new size is less than or equal to zero, then
expire
is activated to set both size and growth rate to
zero. Also, if this Animal
has eaten less than its need
over the past week, then expire
has been activated.simulateWeek
in class Organism
public double stillNeed()
Animal
still needs to
survive this week.Animal
still needs to survive
this week (which is the total need minus the amount eaten so far).