// File: Animal.java from the package edu.colorado.simulations
// Complete documentation is available from the Animal link in
// http://www.cs.colorado.edu/~main/docs/
package edu.colorado.simulations;
/******************************************************************************
* An Animal
is an Organism
with extra methods that
* deal with eating.
*
* Java Source Code for this class:
*
* http://www.cs.colorado.edu/~main/edu/colorado/simulations/Animal.java
*
*
* @author Michael Main
* (main@colorado.edu)
*
* @version Feb 10, 2016
*
* @see Organism
* @see Herbivore
* @see Plant
******************************************************************************/
public class Animal extends Organism
{
private double needEachWeek; // Amount of food needed (in ounces per week)
private double eatenThisWeek; // Ounces of food eaten so far this week
/**
* Construct an Animal
with a specified size, growth rate, and
* weekly eating need.
* @param initSize
* the initial size of this Animal
, in ounces
* @param initRate
* the initial growth rate of this Animal
, in ounces
* @param initNeed
* 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.
* @exception IllegalArgumentException
* Indicates that initSize
, initRate
, or
* initNeed
violates the precondition.
**/
public Animal(double initSize, double initRate, double initNeed)
{
super(initSize, initRate);
if (initNeed < 0)
throw new IllegalArgumentException("initNeed is negative");
needEachWeek = initNeed;
// eatenThisWeek will be given its default value of zero.
}
/**
* Have this Animal
eat a given amount of food.
* @param 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 void eat(double amount)
{
if(amount < 0)
throw new IllegalArgumentException("amount is negative");
eatenThisWeek += amount;
}
/**
* Determine the amount of food that this Animal
needs each
* week.
* @return
* the total amount of food that this Animal
needs to survive
* one week (measured in ounces)
**/
public double getNeed( )
{
return needEachWeek;
}
/**
* Set the current growth weekly food requirement of this Animal
.
* @param 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
.
* @exception IllegalArgumentException
* Indicates that newNeed
is negative.
**/
public void setNeed(double newNeed)
{
if(newNeed < 0)
throw new IllegalArgumentException("newNeed is negative");
needEachWeek = newNeed;
}
/**
* Simulate the passage of one week in the life of this 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.
**/
public void simulateWeek( )
{
super.simulateWeek( );
if (eatenThisWeek < needEachWeek)
expire( );
eatenThisWeek = 0;
}
/**
* Determine the amount of food that this Animal
still needs to
* survive this week.
* @return
* the ounces of food that this Animal
still needs to survive
* this week (which is the total need minus the amount eaten so far).
**/
public double stillNeed( )
{
if (eatenThisWeek >= needEachWeek)
return 0;
else
return needEachWeek - eatenThisWeek;
}
}