Results 1 to 6 of 6
Thread: why isnt it sending value
- 10-23-2008, 03:46 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
why isnt it sending value
this is the part that is giving me problemsJava 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 setUpButtons(); setUpPanels(); // 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); setLocation(SCREEN_OFFSET,SCREEN_OFFSET); setTitle(TITLE); // Finish setting up the frame. Exit the application when the user closes the window. pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * 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()); txtVegetationCount.setText(""+(int)vegetation.getCount()); } // Clear the graph if the user clicks the clear button if (e.getSource() == btnClear) { pnlSimulation.getGraphics().clearRect(0, 0, GRAPH_WIDTH, GRAPH_HEIGHT); repaint(); } } /** * 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 predator.setCount(predatorCount); //---Set counts for prey and vegetation prey.setCount(preyCount); vegetation.setCount(veggieCount); } } /** * 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() { pnlButtons.add(btnStart); pnlButtons.add(btnClear); 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(lblPredatorCount); pnlInput.add(txtPredatorCount); pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER))); pnlInput.add(lblPredatorSearchEfficiency); pnlInput.add(txtPredatorSearchEfficiency); //--- Add the remaining labels, text fields, and spacers to the panel pnlInput.add(lblPredatorFoodConversion); pnlInput.add(txtPredatorFoodConversion); pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER))); pnlInput.add(lblPredatorDeathRate); pnlInput.add(txtPredatorDeathRate); pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER))); pnlInput.add(lblPreyCount); pnlInput.add(txtPreyCount); pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER))); pnlInput.add(lblPreySearchEfficiency); pnlInput.add(txtPreySearchEfficiency); pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER))); pnlInput.add(lblPreyFoodConversion); pnlInput.add(txtPreyFoodConversion); pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER))); pnlInput.add(lblVegetationCount); pnlInput.add(txtVegetationCount); pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER))); pnlInput.add(lblVegetationGrowthRate); pnlInput.add(txtVegetationGrowthRate); pnlInput.add(Box.createRigidArea(new Dimension(0, SPACER))); pnlInput.add(lblSeedDispersal); pnlInput.add(txtSeedDispersal); pnlSimulation.setPreferredSize(new Dimension(GRAPH_WIDTH,GRAPH_HEIGHT)); pnlSimulation.setBackground(Color.white); } /** Private method. Add listeners to the buttons */ private void setUpButtons(){ btnClear.addActionListener(this); btnStart.addActionListener(this); } /** * 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.setColor(o.getColor()); g.drawLine(0, height, LEGEND_LENGTH, height); g.setColor(Color.black); 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(); g.setColor(c); g.fillOval(x,y,2,2); } }
//---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){ super(count,Color.green,name); this.growthRate=growthRate; this.seedGermanition=seedGermanition; System.out.println(seedGermanition); } public double getProjectedCount(Organism predator, Organism prey){ double one=((Animal)predator).getFoodSearchEfficencyFactor()*predator.getCount()*getCount(); double deaths; double two=getCount()- MIN_VEGETATION; if(one<two){ deaths = one; } else{ deaths = two; } double births=(growthRate* getCount())+seedGermanition; double nextcount =getCount()+births-deaths; System.out.println(growthRate+" "+getCount()+ " "+seedGermanition); if(nextcount<=MIN_VEGETATION) nextcount=MIN_VEGETATION; return nextcount; } }Last edited by snitdesne; 10-23-2008 at 04:45 AM.
-
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.
- 10-23-2008, 01:44 PM #3
What does that mean?when i go to the plant class in the constructor it gives a value of 0 instead of seedDispersal
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.
- 10-23-2008, 10:37 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
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
- 10-24-2008, 07:02 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 10-24-2008, 07:24 PM #6
Similar Threads
-
sending SMS between PC and mobile
By Halayas in forum Sun Java Wireless ToolkitReplies: 6Last Post: 03-18-2010, 11:45 AM -
Sending emails using jms
By rrkumar in forum Advanced JavaReplies: 0Last Post: 10-15-2008, 03:14 PM -
Sending out UDP pockets
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:07 PM -
[SOLVED] Why import isnt needed whn ref is used without storing to local variable
By N_i_X in forum Advanced JavaReplies: 2Last Post: 03-31-2008, 05:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks