Java Basics

Programming: Hammurabi Game - Police extension

Add police to this working Kingdom class. You don't need to be concerned with the user interface class (Hammurabi) for this problem. You need to make the following modifications to the Kingdom class.

Because of increasing crime the crop yield is reduced by 30%. The ruler can hire police to reduce the crime.

  1. Keep track of how many police the Kingdom has.
  2. Write a "getter" method so the user interface can find out how many police there are.
  3. Update the toString() method so that it reports the number of police.
  4. Food for police. Add an extra parameter to the simulateOneYear method to indicate how much grain should be used to feed the police. Just as with the rest of the population (ie, the peasants), the number of police is computed from the amount of grain that is allocated to them. You have to feed each policeman twice as much as a peasant to hire him.
  5. Police don't farm (ie, no changes to farming computation).
  6. The maximum number of police you can have in the kingdom is 100. If more food is allocated to police than is necessary for 100 police, then it isn't used.
  7. To show the effect of the police, the crop yield (yieldPerAcre) should be replaced by the following to show the effect of crime / crime prevention. This formula uses p for the number of police, but you should replace p with the variable you use for the number of police.
        int yieldPerAcre   = 2 + (int)(5 * Math.random());
        yieldPerAcre = (int) ((0.70 + 0.30 * (p / 100.0)) * yieldPerAcre);

Make changes to this source code

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
// Purpose: Represents a "kingdom".
// Author : Fred Swartz  12 Dec 2005

class Kingdom {
    //========================================================== constants
    private final static int MIN_GRAIN_TO_SURVIVE = 20;
    private final static int LAND_FARMABLE_PER_PERSON = 15;
    private final static int SEED_REQUIRED_PER_ACRE = 2;

    //================================================= instance variables
    private int myGrain = 3000; // Bushels of grain in storage.
    private int myArea  = 1000; // Area of kingdom in acres. 
    private int myYear  = 0;    // Years since founding of kingdom.
    private int myHarvest = 0;  // Last harvest in bushels.
    private int myPopulation = 100;  // Number of peasants.
    
    //=========================================================== getters
    public int getPopulation() { return myPopulation; }
    public int getGrain()      { return myGrain;   }
    public int getArea()       { return myArea;    }
    public int getYear()       { return myYear;    }
    
    public int getRequiredFood() { 
        return myPopulation * MIN_GRAIN_TO_SURVIVE;
    }

    //=========================================================== toString
    public String toString() {
        return "Kingdom status at year " + myYear
               + ", last harvest = " + myHarvest
               + ", peasants = "     + myPopulation
               + ", total grain = "  + myGrain;
    }
    
    //==================================================== simulateOneYear
    public void simulateOneYear(int food, int seed) {
        //... Reduce grain by amount used for food and seed.
        myGrain = myGrain - food - seed;
        
        //... Compute new population that food can support.
        int neededFood = myPopulation * MIN_GRAIN_TO_SURVIVE;
        myPopulation = myPopulation * food / neededFood;
        
        //... The amount of land we can plant is the smallest of
        //    (1) the area we have,
        //    (2) the amount we have enough seed for
        //    (3) the amount that people can plant.
        int seedableArea = seed / SEED_REQUIRED_PER_ACRE;
        int humanWorkableArea = myPopulation * LAND_FARMABLE_PER_PERSON;
        int potentiallyPlantable = Math.min(seedableArea, humanWorkableArea);
        int acresPlanted = Math.min(myArea, potentiallyPlantable);
        
        //... Compute random yield because of weather in range 2 to 6
        int yieldPerAcre   = 2 + (int)(5 * Math.random());
        
        //... Update harvest so it can be reported.
        myHarvest = yieldPerAcre * acresPlanted;
        
        //... Another year has passed
        myYear++;
        
        //... Update the harvest from this years crops.
        myGrain += myHarvest;
    }
}