UM CSCI 152- Java
Creature class with:
die()reproduce()World class with:
createCreature()spawnFood(), etc.Species.java – Abstract superclass for all lifeformsSagebrushVole.java, FerruginousHawk.java, CarrionBeetle.java – Creature subclassesPlant.java – Non-mobile, non-replicating entityEcosystem.java – Acts as the "world", holds population, manages simulationEcoSim.java – Main driver class
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);
}
@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;
}
names.txtThis 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.