// FILE: Pondlife.java // A simple simulation program to model the fish and weeds in a pond import edu.colorado.simulations.*; // Provides Organism, Plant, Herbivore classes import java.util.Vector; /****************************************************************************** * The PondLife Java application runs a simple simulation that * models the fish and weeds in a pond. * *

The simulation is currently set up to use these values: *

* *

Java Source Code for this class: * * http://www.cs.colorado.edu/~main/applications/PondLife.java * * * @author Michael Main * (main@colorado.edu) * * @version Feb 10, 2016 * * @see edu.colorado.simulations.Organism * @see edu.colorado.simulations.Plant * @see edu.colorado.simulations.Herbivore ******************************************************************************/ public class PondLife { // Number of weeds in the pond public static final int MANY_WEEDS = 2000; // Initial size of each weed, in ounces public static final double WEED_SIZE = 15; // Growth rate of weeds, in ounces/week public static final double WEED_RATE = 2.5; // Initial number of fish in the pond public static final int INIT_FISH = 300; // Fish size, in ounces public static final double FISH_SIZE = 50; // A fish must eat FRACTION times its size during one week, or it will die. public static final double FRACTION = 0.5; // Average number of weeds nibbled by a fish over a week public static final int AVERAGE_NIBBLES = 30; // At the end of each week, some fish have babies. The total number of new // fish born is the current number of fish times the BIRTH_RATE // (rounded down to an integer). public static final double BIRTH_RATE = 0.05; // Number of weeks to simulate public static final int MANY_WEEKS = 38; /** * Run the simulation, using the values indicated in the documentation. * @param args * not used in this implementation **/ public static void main(String[ ] args) { Vector fish = new Vector(INIT_FISH); // A Vector of our fish Vector weeds = new Vector(MANY_WEEDS); // A Vector of our weeds int i; // Loop control variable // Initialize the bags of fish and weeds for (i = 0; i < INIT_FISH; i++) fish.addElement(new Herbivore(FISH_SIZE, 0, FISH_SIZE * FRACTION)); for (i = 0; i < MANY_WEEDS; i++) weeds.addElement(new Plant(WEED_SIZE, WEED_RATE)); System.out.println("Week \tNumber \tPlant Mass"); System.out.println(" \tof \t(in ounces)"); System.out.println(" \tFish"); // Simulate the weeks for (i = 1; i <= MANY_WEEKS; i++) { pondWeek(fish, weeds); System.out.print(i + "\t"); System.out.print(fish.size( ) + "\t"); System.out.print((int) totalMass(weeds) + "\n"); } } /** * Simulate one week of life in the pond, using the values indicated in the * documentation. * @param fish * Vector of fish that are in the pond at the start of the week * @param weeds * Vector of weeds that are in the pond at the start of the week **/ public static void pondWeek(Vector fish, Vector weeds) { int i; int manyIterations; int index; Herbivore nextFish; Plant nextWeed; // Have randomly selected fish nibble on randomly selected plants manyIterations = AVERAGE_NIBBLES * fish.size( ); for (i = 0; i < manyIterations; i++) { index = (int) (Math.random( ) * fish.size( )); nextFish = fish.elementAt(index); index = (int) (Math.random( ) * weeds.size( )); nextWeed = weeds.elementAt(index); nextFish.nibble(nextWeed); } // Simulate the weeks for the fish i = 0; while (i < fish.size( )) { nextFish = fish.elementAt(i); nextFish.simulateWeek( ); if (nextFish.isAlive( )) i++; else fish.removeElementAt(i); } // Simulate the weeks for the weeds for (i = 0; i Organisms. * @param organisms * a Vector of Organism objects * @param * component type of the elements in the organisms Vector * Precondition: * Every object in organisms is an Organism. * @return * the total mass of all the objects in Organism (in ounces). **/ public static double totalMass(Vector organisms) { double answer = 0; for (Organism next : organisms) { if (next != null) answer += next.getSize( ); } return answer; } }