Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-13-2005, 04:43 PM   PM User | #1
punx
New Coder

 
Join Date: Aug 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
punx is an unknown quantity at this point
[Java]

Hello once again. I am trying to round a double to the nearest Integer, and display the answer in a DoubleField for a GUI application. I've tried to cast the variable being used to type int, and tried using the Format.justify method. Here's the code, any help would be appriciated. Thanks.
Code:
/**
*Calculates the population growth
**/

import javax.swing.*;
import BreezySwing.*;
import BreezySwing.Format;
public class PopulationGrowth extends GBFrame
{
	private JLabel initialOrganismsLabel;
	private JLabel growthRateLabel;
	private JLabel hoursLabel;
	private JLabel growthHoursLabel;
	private DoubleField initialOrganismsField;
	private DoubleField growthRateField;
	private DoubleField hoursField;
	private DoubleField growthHoursField;
	private DoubleField totalOrganismsField;
	private JButton calculateButton;
   
//*************************Constructor Method***********************************
   public PopulationGrowth()
   {
   	initialOrganismsLabel = addLabel  ("Initial Organisms" ,1,1,1,1);
   	initialOrganismsField = addDoubleField 	(0.0 ,1,2,1,1);
   	growthRateLabel = addLabel  ("Growth Rate" ,2,1,1,1);
   	growthRateField = addDoubleField  (0.0 ,2,2,1,1);
   	hoursLabel = addLabel ("Hours to achieve this rate" ,3,1,1,1);
   	hoursField = addDoubleField	(0.0 ,3,2,1,1);
   	growthHoursLabel 	= addLabel  ("Hours to grow" ,4,1,1,1);
   	growthHoursField 	= addDoubleField 	(0.0 ,4,2,1,1);
   	totalOrganismsField = addDoubleField 	(0 ,5,1,2,2);
   	calculateButton = addButton ("Calculate Population" ,6,2,1,1);
   }
//******************************************************************************

   public void buttonClicked (JButton buttonObj)
   {
   		double 	initialOrganisms,
   	  			growthRate,
   	  			hours,
   	  			growthHours,
   	  			growPeriod,
   	  			totalOrganisms = 0;
   	  			
   		if (buttonObj == calculateButton)
	  	{
	  		initialOrganisms= initialOrganismsField.getNumber();
	  		growthRate = growthRateField.getNumber();
	  		hours = hoursField.getNumber();
	  		growthHours = growthHoursField.getNumber();
	  		growPeriod = hours / growthHours;
	  		totalOrganisms = (int)initialOrganisms * Math.pow(growthRate,growPeriod);
	 		double temp = Format.justify('l' ,totalOrganisms, 25, 0);
	  		totalOrganismsField.setNumber(temp);
	  	}
   }
   				
	public static void main (String[] args)
   	{
      PopulationGrowth GUI = new PopulationGrowth();							//Instantiate the GUI window
      GUI.setSize (500, 400);													//Set the size of the program
      GUI.setVisible (true);													//Shows the program
   	}
}
punx is offline   Reply With Quote
Old 10-13-2005, 05:30 PM   PM User | #2
squirellplaying
Regular Coder

 
Join Date: Jan 2004
Location: Maryland
Posts: 468
Thanks: 0
Thanked 0 Times in 0 Posts
squirellplaying is an unknown quantity at this point
Have you tried adding .5 then converting to an Int?
Code:
totalOrganisms = (int)((initialOrganisms * Math.pow(growthRate,growPeriod))+.05);
This method will do the math first, then round. Since converting to an Int from a double just cuts off everything after the decimal, adding .5 will "round up" correctly.
squirellplaying is offline   Reply With Quote
Old 10-13-2005, 06:38 PM   PM User | #3
TheShaner
Senior Coder

 
TheShaner's Avatar
 
Join Date: Sep 2005
Location: Orlando, FL
Posts: 1,125
Thanks: 2
Thanked 40 Times in 40 Posts
TheShaner will become famous soon enoughTheShaner will become famous soon enough
Requires the import of java.lang.Math:

http://java.sun.com/j2se/1.4.2/docs/...#round(double)

Squirrel's implementation will work too, except you should cast it as a long instead of an int, because depending on how big your number gets, you may end up truncating the value big time.

-Shane
TheShaner is offline   Reply With Quote
Old 10-14-2005, 03:52 PM   PM User | #4
punx
New Coder

 
Join Date: Aug 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
punx is an unknown quantity at this point
Thanks guys, that helped out alot.
punx is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:39 AM.


Advertisement
Log in to turn off these ads.