Results 1 to 6 of 6
  1. #1
    snitdesne is offline Member
    Join Date
    Oct 2008
    Rep Power

    Default why isnt it sending value

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
     * Three-species predator prey simulation. 
     * @author put your name here     
    public class EcosystemSimulator extends JFrame implements ActionListener , Simulator
        // Window and label strings
        private final static String TITLE = "3-Species Predator Prey Simulation";
        private final static String PREDATOR = "Canadian Lynx";
        private final static String PREY = "Snowshoe Hare";
        private final static String VEGETATION = "Vegetation";
        private final static String BLANKS = "      ";
        private final static JLabel lblPredatorCount = new JLabel(PREDATOR + " Count");
        private final static JLabel lblPredatorSearchEfficiency = new JLabel(BLANKS + PREDATOR + " Food Search Factor");
        private final static JLabel lblPredatorFoodConversion = new JLabel(BLANKS + PREDATOR + " Food Conversion Factor");
        private final static JLabel lblPredatorDeathRate = new JLabel(BLANKS + PREDATOR + " Death Rate");
        //--- Declare the remaining labels here 
        private final static JLabel lblPreyCount = new JLabel( PREY+ " Count");
        private final static JLabel lblPreySearchEfficiency = new JLabel(BLANKS+ PREY+ " Food Search Factor");
        private final static JLabel lblPreyFoodConversion= new JLabel(BLANKS+ PREY+ " Food Conversion Factor");
        private final static JLabel lblVegetationCount = new JLabel( VEGETATION+ " Count");
        private final static JLabel lblVegetationGrowthRate = new JLabel(BLANKS+ VEGETATION+ " Growth Rate");
        private final static JLabel lblSeedDispersal = new JLabel(BLANKS+  "Seed Dispersal Factor");
        // Initial values for text fields
        private final static String initPredatorCount = "10";
        //--- Declare the remaining initial values here 
        private final static String  initPredatorSearchEfficiency = ".02";
        private final static String  initPredatorFoodConversion = ".1";
        private final static String  initPredatorDeathRate = ".1";
        private final static String initPreyCount = "100";
        private final static String initPreySearchEfficiency = ".2";
        private final static String initPreyFoodConversion = ".005";
        private final static String initVegetationCount = "500";      
        private final static String initVegetationGrowthRate = ".4";
        private final static String initSeedDispersal = "75";
        // Size constants for the window and panels
        private final static int FRAME_WIDTH = 950;  // Application window width
        private final static int FRAME_HEIGHT = 600; // Application window height
        private final static int SPACER = 10;  // Spacer height to separate input fields and legend items
        private final static int GRAPH_WIDTH = 650;  // Simulation graph width
        private final static int GRAPH_HEIGHT = 500;  // Simulation graph height
        private final static int BORDER_OFFSET = 10;  // Padding around the graph, input area
        private final static int SCREEN_OFFSET = 100; // Placement of application window on screen
        private final static int TRUNCATION = 100;  // Truncate out-of-range simulation values
        private final static int LEGEND_LENGTH = 100; // Length of lines in graph legend
        private final static int LEGEND_HEIGHT =GRAPH_HEIGHT - BORDER_OFFSET - TRUNCATION + 20;
        // Buttons 
        private JButton btnStart = new JButton("Start"); // Start a new simulation
        private JButton btnClear = new JButton("Clear"); // Clears the current simulation drawing
        // Panels, Boxes, and Borders
        private JPanel pnlButtons = new JPanel(new FlowLayout());   // Holds application buttons
        private JPanel pnlInput = new JPanel();  // Holds labels and text input areas
        private JPanel pnlSimulation = new JPanel(); // Holds simulation graph
        // Fields for user input
        private JTextField txtPredatorCount = new JTextField(initPredatorCount);  // Current predator count
        //--- Declare the remaining fields here
        private JTextField txtPredatorSearchEfficiency =new JTextField(initPredatorSearchEfficiency);// Current predator food search factor
        private JTextField txtPredatorFoodConversion =new JTextField(initPredatorFoodConversion);// Current predator  food conversion factor
        private JTextField txtPredatorDeathRate =new JTextField(initPredatorDeathRate);// Current predator  death rate
        private JTextField txtPreyCount = new JTextField(initPreyCount);  // Current prey count
        private JTextField txtPreySearchEfficiency = new JTextField(initPreySearchEfficiency);  // Current prey food search factor
        private JTextField txtPreyFoodConversion = new JTextField(initPreyFoodConversion);  // Current prey food convwersion factor
        private JTextField txtVegetationCount = new JTextField(initVegetationCount);  // Current vegetation count
        private JTextField txtVegetationGrowthRate = new JTextField(initVegetationGrowthRate);  // Current vegetation growth rate
        private JTextField txtSeedDispersal = new JTextField(initSeedDispersal);  // Current vegetation  seed dispersal factor
        // Simulation bounds
        private final static int STEPS =1;  // Number of simulation steps
        // Predator, prey, vegetation
        Organism prey;
        Organism predator;
        Organism vegetation;
         * Creates the user interface for the simulation and runs it.
        public  void run() {
            // Initial work. Set up the buttons and the panels
            // Add the panels to the window. Set its size, location, title
            Container c = getContentPane();
            c.add(pnlButtons, BorderLayout.NORTH);
            c.add(pnlInput, BorderLayout.WEST);
            c.add(pnlSimulation, BorderLayout.CENTER);
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
            // Finish setting up the frame. Exit the application when the user closes the window.
         * Specifies actions to be performed when the user clicks a button.
         * @param e Source is the button clicked.
         public void actionPerformed(ActionEvent e) {
        // Run the simulation when the user clicks the start button
            if (e.getSource() == btnStart){
                // Read the info from the text boxes.
                double predatorCount =  Double.parseDouble(txtPredatorCount.getText());
                //---Declare additional local variables and initialize them with the text entered by the user
                double predatorSearchEfficency= Double.parseDouble(txtPredatorSearchEfficiency.getText());
                double predatorFoodConversion= Double.parseDouble(txtPredatorFoodConversion.getText());
                double predatorDeathRate= Double.parseDouble(txtPredatorDeathRate.getText());
                double preyCount = Double.parseDouble(txtPreyCount.getText());
                double preySearchEfficency= Double.parseDouble(txtPreySearchEfficiency.getText());
                double preyFoodConversion = Double.parseDouble(txtPreyFoodConversion.getText());
                double vegetationCount = Double.parseDouble(txtVegetationCount.getText());
                double vegetationGrowthRate = Double.parseDouble(txtVegetationGrowthRate.getText());
                double seedDispersal = Double.parseDouble(txtSeedDispersal.getText());
                //---Instantiate each Organism to be plotted on the graph
                  predator=new Predator(predatorCount,"Canadian Lynx",predatorSearchEfficency,predatorFoodConversion,predatorDeathRate);
                  prey = new Prey(preyCount,"Snowshoe Hare", preySearchEfficency,preyFoodConversion);
                  vegetation = new Plant(vegetationCount,"Vegetation",vegetationGrowthRate,seedDispersal);
                // Run the simulation
                simulate(predator, prey, vegetation);
                // Print the organism counts on their corresponding text fields
                txtPreyCount.setText("" + (int) prey.getCount());
                //---Print the counts for Predator and Vegetation\
                txtPredatorCount.setText("" + (int) predator.getCount());
            // Clear the graph if the user clicks the clear button
            if (e.getSource() == btnClear) {
                pnlSimulation.getGraphics().clearRect(0, 0, GRAPH_WIDTH, GRAPH_HEIGHT);
         * Run the simulation, plotting the populations on the graph
         * @param predator predator for the simulation
         * @param prey prey for the simulation
         * @param vegetation vegetation (grass) for the simulation
        private void simulate(Organism predator, Organism prey, Organism vegetation) {
            drawLegend(pnlSimulation, predator, LEGEND_HEIGHT + 4 * SPACER);
            drawLegend(pnlSimulation, prey, LEGEND_HEIGHT + 6 * SPACER);
            drawLegend(pnlSimulation, vegetation, LEGEND_HEIGHT + 8 * SPACER);
            // Run the simulation under the required number of steps
            for(int i = 1; i <= STEPS; i++) {
                // Find the count for each organism. Truncate if necessary to fit on the graph.
                int plotVegetation = truncateToGraph(vegetation.getCount());
                int plotPrey = truncateToGraph(prey.getCount());
                int plotPredator = truncateToGraph(predator.getCount());
                // Plot the current count for each organism on the graph
                plotPoint(pnlSimulation, i, LEGEND_HEIGHT - plotVegetation, vegetation.getColor());
                //---Plot the current counts for predator and prey
                plotPoint(pnlSimulation, i, LEGEND_HEIGHT -plotPrey, prey.getColor());
                plotPoint(pnlSimulation, i, LEGEND_HEIGHT - plotPredator, predator.getColor());
                //--- Compute the next counts for each organism. Use the value null as needed in calls to getProjectedCount()
                double predatorCount =predator.getProjectedCount(predator,prey);
                double preyCount= prey.getProjectedCount(null,prey);
                double veggieCount =vegetation.getProjectedCount(predator,null);
                // Set the counts for each organism as the final action in a step
                //---Set counts for prey and vegetation
         * Private method. Returns a number that is within a range of 0 to (GRAPH_HEIGHT - TRUNCATION).
         * @param number Any number
         * @return If number < 0, return 0; else if number > GRAPH_HEIGHT - TRUNCATION, return GRAPH_HEIGHT - TRUNCATION;
         *    else return number.
        private int truncateToGraph(double number) {
            if (number > GRAPH_HEIGHT - TRUNCATION)
                return GRAPH_HEIGHT - TRUNCATION;
            if (number < 0)
                return 0;
            return (int) number;
        /** Private method. Sets up the panels by specifying sizes and bordes and adding components.
        private void setUpPanels() {
            pnlInput.setLayout(new BoxLayout(pnlInput, BoxLayout.Y_AXIS));
            pnlInput.setBorder(new EmptyBorder(BORDER_OFFSET,BORDER_OFFSET,BORDER_OFFSET,BORDER_OFFSET));
            pnlInput.setPreferredSize(new Dimension(FRAME_WIDTH - GRAPH_WIDTH,FRAME_HEIGHT - 2 * SPACER));
            pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER)));
            pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER)));
            //--- Add the remaining labels, text fields, and spacers to the panel
            pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER)));
            pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER)));
            pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER)));
            pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER)));
            pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER)));
            pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER)));
            pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER)));
            pnlSimulation.setPreferredSize(new Dimension(GRAPH_WIDTH,GRAPH_HEIGHT));
        /** Private method. Add listeners to the buttons
        private void setUpButtons(){
         * Private method. Draws a line in the graph legend.
         * @param panel Panel where the legend is drawn.
         * @param o. Organism to be noted in this line.
         * @param height. Height of this line in the graph.
        private void drawLegend(JPanel panel, Organism o, int height) {
            Graphics g = panel.getGraphics();
            g.drawLine(0, height, LEGEND_LENGTH, height);
            g.drawString(o.getName(), LEGEND_LENGTH + SPACER, height);
         * Private method. Plots a point on a graph.
         * @param panel Panel where the graph is drawn.
         * @param x x-coordinate of the point
         * @param y y-coordinate of the point
         * @param c color of the point
        private void plotPoint(JPanel panel, int x, int y, Color c) {
            Graphics g = panel.getGraphics();
    this is the part that is giving me problems

    //---Instantiate each Organism to be plotted on the graph
    predator=new Predator(predatorCount,"Canadian Lynx",predatorSearchEfficency,predatorFoodConversi on,predatorDeathRate);
    prey = new Prey(preyCount,"Snowshoe Hare", preySearchEfficency,preyFoodConversion);
    vegetation = new Plant(vegetationCount,"Vegetation",vegetationGrowt hRate,seedDispersal);

    when i go to the plant class in the constructor it gives a value of 0 instead of seedDispersal
    Java Code:
    import java.awt.*;
    public class Plant extends Organism
        private double seedGermanition;
        private double growthRate;
        private final static double MIN_VEGETATION=10;
        public Plant(double count, String name, double growthRate,double seedGermination){
        public double getProjectedCount(Organism predator, Organism prey){
            double one=((Animal)predator).getFoodSearchEfficencyFactor()*predator.getCount()*getCount();
            double deaths;
            double two=getCount()- MIN_VEGETATION;
                deaths = one;
                 deaths = two;
             double births=(growthRate* getCount())+seedGermanition;
             double nextcount =getCount()+births-deaths;
              System.out.println(growthRate+"  "+getCount()+ "   "+seedGermanition);
             return nextcount;
    Last edited by snitdesne; 10-23-2008 at 04:45 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    That's a heck of a lot of unformatted code that you've posted there partner! I wish you much luck getting someone to read it all.
    Last edited by Fubarable; 10-23-2008 at 04:13 AM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    when i go to the plant class in the constructor it gives a value of 0 instead of seedDispersal
    What does that mean?
    Constructors do NOT return values. They initialize an instance of a class.

    Can seedDispersal have a value of 0? Then the answer you get could be correct.

    What is the code you've posted supposed to do? Add comments to the code describing what and why your doing it.

  4. #4
    snitdesne is offline Member
    Join Date
    Oct 2008
    Rep Power


    well if you read the code seed dispersal is 75 and i check the value of seed dispersal before i create an object of type plant and then in the constructor of plant it gives the value of 0

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power


    Please pay your attention on what Fubarable and Norm says. It's helpful to you to find a solution, because other members not make horrible to read such a long post.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    if you read the code seed dispersal is 75
    It might be at one time, but the value "75" is a String that is used to init a text field which can be changed before you get the value out. Add a println() to show what is in the text field before you get the value out and convert it to a Double.

Similar Threads

  1. sending SMS between PC and mobile
    By Halayas in forum Sun Java Wireless Toolkit
    Replies: 6
    Last Post: 03-18-2010, 11:45 AM
  2. Sending emails using jms
    By rrkumar in forum Advanced Java
    Replies: 0
    Last Post: 10-15-2008, 03:14 PM
  3. Sending out UDP pockets
    By Java Tip in forum
    Replies: 0
    Last Post: 04-07-2008, 08:07 PM
  4. Replies: 2
    Last Post: 03-31-2008, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts