Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Programming: Hammurabi II - Two Player Version

Extend the basic Hammurabi project

This is based on the Hammurabi I program, extending it to two players with a couple of small enhancements. This is intended to be a small assignment.

Start with the solution to the one-player version

You can start this problem either with your solution to Hammurabi I, or use the solution at .

Separate files. Each class is in a separate source file. This is typical, but multiple classes can be in the same source file. If they are in the same source file, there must be only one public class, which will be the basis for the source file name. One class per file is the best strategy.

The Hammurabi class

This class is used to run the simulation. It contains the main method and a utility method to display the results. It creates one Kingdom object which it happens to call samaria, but you might want to change that name.

  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 
// File   : hammurabi0/Hammurabi.java
// Purpose: Starting point for working on the Hammurabi program.
// Author : Fred Swartz
// Date   : 17 Apr 2005

import javax.swing.*;

public class Hammurabi {

    //========================================================= main
    public static void main(String[] args) {

        //... Initialization [TODO: set initial population]
        Kingdom samaria = new Kingdom();  // Create a new
        samaria.setGrain(3000);


        //... Run the simulation for 10 years or until everyone starves.
        //    TODO:
        while (samaria.getYear() <= 5  /* && samaria.getPopulation() > 0 */) {
            displayStatus(samaria);

            //TODO: Ask the Exalted Ruler how much to feed the people.
            int food = 0;  // This is in place of asking for input.

            String plantStr = JOptionPane.showInputDialog(null,
                    "How much of the remaining "
                    + (samaria.getGrain()-food) + " bushels should we plant?");
            int seeds = Integer.parseInt(plantStr);

            //... Update the food and population of this kingdom.
            samaria.simulateOneYear(food, seeds);
        }

        //... Show final state.
        displayStatus(samaria);

    }


    //==================================================== displayStatus
    // Shows the amount of grain and population for
    // and Kingdom.  TODO: Add population output.
    private static void displayStatus(Kingdom country) {
        JOptionPane.showMessageDialog(null,
                "Year = " + country.getYear()
                + "\nGrain = " + country.getGrain());
    }
}

The Kingdom class

This class represents a "kingdom". There are two portions to a class: the data and the methods. Currently, the only data (instance variables) in this class are the year and the amount of grain in storage. The population must be added. This also has some methods - some "getters" and "setters" to get and set the instance variables. The simulateOneYear method, which takes parameters which tell how much grain to use to plant then crop, and how much to feed the population. Getter and setter methods have to be added to work with the population, and the simulateOneYear method must be extended to update the population.

  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 
// File   : hammurabi0/Kingdom.java
// Purpose: Represents a "kingdom"
//          This version uses only year and grain.
//          TODO: Add population value and simulation.
// Author : Fred Swartz
// Date   : 17 Apr 2005

class Kingdom {
    //==================================================== Constants
    public static int SEED_REQUIRED_PER_ACRE = 2;

    //=========================================== Instance variables.
    private int myGrain;
    private int myYear = 1;

    //===================================================== setGrain
    public void setGrain(int grain) {
        myGrain = grain;
    }

    //===================================================== getGrain
    public int getGrain() {
        return myGrain;
    }

    //====================================================== getYear
    public int getYear() {
        return myYear;
    }

    //============================================== simulateOneYear
    public void simulateOneYear(int food, int seed) {
        myYear++;

        //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 number area planted * yield.

        int acresPlanted = seed / SEED_REQUIRED_PER_ACRE;
        int yieldPerAcre = 2 + (int)(5 * Math.random());
        int harvest      = yieldPerAcre * acresPlanted;

        myGrain += harvest;  // New amount of grain in storage.
    }
}
Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.