Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Variable less than one

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 02-13-2008, 10:11 PM PM User | #1 cowbell1 New to the CF scene   Join Date: Feb 2008 Posts: 6 Thanks: 1 Thanked 0 Times in 0 Posts Variable less than one Hello there, I am currently enrolled in a computer science class at my school, and I need some help with a program. My teacher started up a rocket club at our school with great success, but he needs some help with minor problems. He uses parachutes to bring his rockets safely back to the ground, but he wanted to know how much PSI a gram of gun powder produces. He wants me to create a program in java to determine the amount. I got everything working, but I can't seem to get an output if the amount of gunpowder is less than one gram. I currently have the grams of powder set as a float variable and my equation for finding the PSI is set as : Code: `psi = (grams * 100) / (0.04f * (rdia * rdia) * rlen);` (rdia is the radius of the rocket and rlen is the length of the rocket, both in inches.) Also my output command is set as Code: `outputLabel.setText("Your PSI is " + PSI);` I know that the equation is right as I have tried other programs out there to see if it works, but I can't get anything under 1 gram to print a result.
02-13-2008, 10:33 PM   PM User | #2
Rohan_Shenoy
New Coder

Join Date: Nov 2007
Location: Thane-Vashi,Mumbai,India.
Posts: 44
Thanks: 3
Thanked 2 Times in 2 Posts
Quote:
 He wants me to create a program in java to determine the amoun
Java is not same as Javascript!

02-13-2008, 11:50 PM   PM User | #3
sobrien79
Regular Coder

Join Date: Jan 2008
Location: Willow Grove, PA
Posts: 169
Thanks: 1
Thanked 27 Times in 27 Posts
Quote:
 Originally Posted by Rohan_Shenoy Java is not same as Javascript!
I think he is working with Java and not JavaScript.

cowbell1, can you provide more of the code where you read in the vars and where you declare psi? The first thing that comes to mind is that psi is declared as as int or long.

 02-14-2008, 12:23 AM PM User | #4 cowbell1 New to the CF scene   Join Date: Feb 2008 Posts: 6 Thanks: 1 Thanked 0 Times in 0 Posts Code: ```public class Gunpowder extends Applet implements ActionListener { // declare and construct variables int rlen; float grams, rdia, psi;```
 02-14-2008, 05:25 AM PM User | #5 Aradon Moderator-san     Join Date: Jun 2005 Location: USA Posts: 734 Thanks: 0 Thanked 20 Times in 19 Posts Try making 100 100.0 and casting rlen into a float. also, is it psi or PSI? __________________ "To iterate is human, to recurse divine." -L. Peter Deutsch
 02-19-2008, 02:25 PM PM User | #6 cowbell1 New to the CF scene   Join Date: Feb 2008 Posts: 6 Thanks: 1 Thanked 0 Times in 0 Posts Turns out that it will not even calculate any number that has a decimal. I don't really know why this is, but I will post the rest of the code soon.
 02-19-2008, 07:22 PM PM User | #7 cowbell1 New to the CF scene   Join Date: Feb 2008 Posts: 6 Thanks: 1 Thanked 0 Times in 0 Posts Code: ```import java.applet.*; import java.awt.*; import java.awt.event.*; public class Gunpowder extends Applet implements ActionListener { // declare and construct variables float psi, rdia, grams, rlen; // construct components Label createrLabel = new Label("THE BEN PARRISH & JOE SCHMIDT PSI GUN POWDER"); Label rlenLabel = new Label("Enter your rockets Length: "); TextField rlenField = new TextField(10); Label rdiaLabel = new Label("Enter your rockets Diameter: "); TextField rdiaField = new TextField(10); Label gramsLabel = new Label("Enter your grams of Gun Powder: "); TextField gramsField = new TextField(10); Button calcButton = new Button("Calculate The PSI"); Label outputLabel = new Label("Click the Calculate buttons calculate the PSI."); Label outputaLabel = new Label (""); public void init() { setForeground(Color.black); add(createrLabel); add(rlenLabel); add(rlenField); add(rdiaLabel); add(rdiaField); add(gramsLabel); add(gramsField); add(calcButton); calcButton.addActionListener(this); add(outputLabel); add(outputaLabel); } public void actionPerformed(ActionEvent e) { // Get values used in calculation rlen = Integer.parseInt(rlenField.getText()); rdia = Integer.parseInt(rdiaField.getText()); grams = Integer.parseInt(gramsField.getText()); // Do calculations psi = (grams * 100) / (0.04f * (rdia * rdia) * rlen); // Provide user with result of calculation outputLabel.setText("Your PSI is " + psi); } }```
 02-19-2008, 09:30 PM PM User | #8 sobrien79 Regular Coder   Join Date: Jan 2008 Location: Willow Grove, PA Posts: 169 Thanks: 1 Thanked 27 Times in 27 Posts I might recommend using Float.parseFloat instead of Integer.parseInt if you are looking to get values that are not whole numbers. Code: ```public static void main(String[] args) { float psi, rdia, grams, rlen; // Get values used in calculation rlen = Float.parseFloat("2.3"); rdia = Float.parseFloat("2.3"); grams = Float.parseFloat("2.3"); // Do calculations psi = grams * 100 / (0.04f * rdia * rdia * rlen); // Provide user with result of calculation System.out.println("Your PSI is " + psi); }``` Printed: Your PSI is 472.58984
 Users who have thanked sobrien79 for this post: cowbell1 (02-20-2008)
 02-20-2008, 12:47 AM PM User | #9 cowbell1 New to the CF scene   Join Date: Feb 2008 Posts: 6 Thanks: 1 Thanked 0 Times in 0 Posts Awesome thanks alot. Thats one thing we never got around to learning yet, so that makes it alot easier. If only java was as easy as VB. Once again thanks.
 02-20-2008, 03:13 PM PM User | #10 sobrien79 Regular Coder   Join Date: Jan 2008 Location: Willow Grove, PA Posts: 169 Thanks: 1 Thanked 27 Times in 27 Posts Good time to learn it. There are objects for each of the primitive types long --> Long boolean --> Boolean etc. They are definitely worth knowing. The number objects are all similar.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 06:36 AM.