// File : hammurabi0/Kingdom.java // Purpose: Represents a "kingdom". Starting version. // Author : Fred Swartz // Source : www.roseindia.net/java/java-tips/oop/q-hammurabi/q-pr-hammurabi-1.shtml // License: MIT // Date : 07 Oct 2005 // TODO : * Add population instance variable. Default value 100. // * Add a getter method for population. // * Compute the new population based on food in simulation.. class kingdom { //========================================================== constants private final static int MIN_GRAIN_TO_SURVIVE = 20; private final static int MAX_LAND_FARMABLE_PER_PERSON = 15; private final static int SEED_REQUIRED_PER_ACRE = 2; //================================================= instance variables private int myGrain = 4000; // Bushels of grain in storage. private int myArea = 1500; // Area of kingdom.in acres. Note that // myArea isn't used yet, but will be if // you add a check for the total amount // of land that can be planted. private int myYear = 0; // Years since founding of kingdom. private int myHarvest = 0; // Last harvest in bushels. //=========================================================== getGrain public int getGrain() { return myGrain; } //============================================================ getYear public int getYear() { return myYear; } //=========================================================== toString public String toString() { // TODO: Don't forget to add population here too. return "Kingdom status at year " + myYear + ", last harvest = " + myHarvest + ", total grain = " + myGrain; } //==================================================== simulateOneYear public void simulateOneYear(int food, int seed) { //TODO: Need to calculate new population.based on food. //... Reduce grain stockpile by amount used for food and seed myGrain = myGrain - food - seed; //... Calculate new harvest // 1. How many acres can be planted with seed. // 2. The yield per acre is random (2-6) // 3. Harvest is yield * area planted. int acresPlanted = seed / SEED_REQUIRED_PER_ACRE; // TODO: Check that there are enough people and there is // enough land to actually plant that number of // acres. int yieldPerAcre = 2 + (int)(5 * Math.random()); myHarvest = yieldPerAcre * acresPlanted; //... Compute new amount of grain in storage. myGrain += myHarvest; // New amount of grain in storage. myYear++; // Another year has passed. } }