// File: Organism.java from the package edu.colorado.simulations
// Complete documentation is available from the Organism link in
// http://www.cs.colorado.edu/~main/docs/
package edu.colorado.simulations;
/******************************************************************************
* An Organism
object simulates a growing organism such as a
* plant or animal.
*
* Java Source Code for this class:
*
* http://www.cs.colorado.edu/~main/edu/colorado/simulations/Organism.java
*
*
* @author Michael Main
* (main@colorado.edu)
*
* @version Feb 10, 2016
*
* @see Animal
* @see Plant
******************************************************************************/
public class Organism
{
private double size; // The current size of this Organism, in ounces
private double rate; // The current growth rate , in ounces per week
/**
* Construct an Organism
with a specified size and growth rate.
* @param initSize
* the initial size of this Organism
, in ounces
* @param initRate
* the initial growth rate of this Organism
, in ounces
* Precondition:
* initSize >= 0
. Also, if initSize
is zero, then
* initRate
must also be zero.
* Postcondition:
* This Organism
has been initialized. The value returned from
* getSize()
is now initSize
, and the value
* returned from getRate()
is now initRate
.
* @throws IllegalArgumentException
* Indicates that initSize
or initRate
violates
* the precondition.
**/
public Organism(double initSize, double initRate)
{
size = initSize;
rate = initRate;
}
/**
* Change the current size of this Organism
by a given amount.
* @param amount
* the amount to increase or decrease the size of this
* Organism
(in ounces)
* Postcondition:
* The given amount (in ounces) has been added to the size of this
* Organism
. If this new size is less than or equal to zero,
* then expire
is also activated.
**/
public void alterSize(double amount)
{
size += amount;
if (size <= 0)
expire( );
}
/**
* Set this Organism
’s size and growth rate to zero.
* Postcondition:
* The size and growth rate of this Organism
have been set
* to zero.
**/
public void expire( )
{
size = rate = 0;
}
/**
* Get the growth rate of this Organism
.
* @return
* the growth rate of this Organism
(in ounces per week)
**/
public double getRate( )
{
return rate;
}
/**
* Get the current size of this Organism
.
* @return
* the current size of this Organism
(in ounces)
**/
public double getSize( )
{
return size;
}
/**
* Determine whether this Organism
is currently alive.
* @return
* If this Organism
’s current current size is greater than
* zero, then the return value is true
; otherwise the return
* value is false
.
**/
public boolean isAlive( )
{
return (size > 0);
}
/**
* Set the current growth rate of this Organism
.
* @param newRate
* the new growth rate for this Organism
(in ounces per week)
* Precondition:
* If the size is currently zero, then newRate
must also be
* zero.
* Postcondition:
* The growth rate for this Organism
has been set to
* newRate
.
* @throws IllegalArgumentException
* Indicates that the size is currently zero, but the newRate
* is nonzero.
**/
public void setRate(double newRate)
{
if ((size == 0) && (newRate != 0))
throw new IllegalArgumentException
("newRate must be zero (because current size is zero");
rate = newRate;
}
/**
* Simulate the passage of one week in the life of this Organism
.
* Postcondition:
* The size of this Organism
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.
**/
public void simulateWeek( )
{
alterSize(rate);
}
}