Ecosystem Simulator in Java

UM CSCI 152- Java

Assignment Requirements

  • At least three classes or interfaces
  • At least two object relationships (inheritance, composition, etc.)
  • A Creature class with:
    • die()
    • reproduce()
  • A World class with:
    • createCreature()
    • spawnFood(), etc.
  • Support for multiple creatures in an environment
  • Simulation loop with chance-based behaviors

Project Structure

  • Species.java – Abstract superclass for all lifeforms
  • SagebrushVole.java, FerruginousHawk.java, CarrionBeetle.java – Creature subclasses
  • Plant.java – Non-mobile, non-replicating entity
  • Ecosystem.java – Acts as the "world", holds population, manages simulation
  • EcoSim.java – Main driver class

Sample Code: Ecosystem Loop


public void step() {
    timeStep++;
    System.out.println("\n--- Time Step " + timeStep + " ---");

    List<Species> newBorns = new ArrayList<>();
    List<Species> toRemove = new ArrayList<>();

    for (Species s : new ArrayList<>(population)) {
        if (s.isAlive()) {
            s.step(this);
            Species baby = s.reproduce();
            if (baby != null) newBorns.add(baby);
        } else {
            toRemove.add(s);
        }
    }

    population.addAll(newBorns);
    population.removeAll(toRemove);

    if (timeStep % 2 == 0) growPlants(1);
}
                

Sample Code: Creature Behavior (Carrion Beetle)


@Override
public void step(Ecosystem eco) {
    if (!isAlive()) return;

    ageUp();
    energy--;

    if (energy <= 0) {
        die();
        return;
    }

    if (Math.random() < 0.2) {
        energy += 1;
        System.out.println(name + " found some decay! Energy is now " + energy);
    }
}

@Override
public Species reproduce() {
    if (canReproduce()) {
        energy -= 3;
        System.out.println(name + " has laid eggs!");
        return new CarrionBeetle();
    }
    return null;
}
                

Technologies Used

  • Java
  • Object-Oriented Programming (inheritance, polymorphism)
  • Random name generation from names.txt
  • Recursion for reproduction and lifespan handling

Reflection

This project built a flexible base for future simulations like AI agents or genetic algorithms. It helped reinforce concepts like class design, random behavior modeling, and modular growth of simulation logic.

Next Steps

  • Expand energy dynamics (e.g., food chains)
  • Implement starvation, vision radius, or predator-prey logic
  • Add visual grid or graphical representation of the ecosystem