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 02-13-2008, 09: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
cowbell1 is an unknown quantity at this point
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.
cowbell1 is offline   Reply With Quote
Old 02-13-2008, 09: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
Rohan_Shenoy is an unknown quantity at this point
Quote:
He wants me to create a program in java to determine the amoun
Java is not same as Javascript!
Rohan_Shenoy is offline   Reply With Quote
Old 02-13-2008, 10: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
sobrien79 is an unknown quantity at this point
Quote:
Originally Posted by Rohan_Shenoy View Post
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.
sobrien79 is offline   Reply With Quote
Old 02-13-2008, 11:23 PM   PM User | #4
cowbell1
New to the CF scene

 
Join Date: Feb 2008
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
cowbell1 is an unknown quantity at this point
Code:
public class Gunpowder extends Applet implements ActionListener
{
	// declare and construct variables
	int rlen;
	float grams, rdia, psi;
cowbell1 is offline   Reply With Quote
Old 02-14-2008, 04:25 AM   PM User | #5
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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
Aradon is offline   Reply With Quote
Old 02-19-2008, 01: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
cowbell1 is an unknown quantity at this point
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.
cowbell1 is offline   Reply With Quote
Old 02-19-2008, 06: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
cowbell1 is an unknown quantity at this point
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);

	}

}
cowbell1 is offline   Reply With Quote
Old 02-19-2008, 08: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
sobrien79 is an unknown quantity at this point
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
sobrien79 is offline   Reply With Quote
Users who have thanked sobrien79 for this post:
cowbell1 (02-19-2008)
Old 02-19-2008, 11:47 PM   PM User | #9
cowbell1
New to the CF scene

 
Join Date: Feb 2008
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
cowbell1 is an unknown quantity at this point
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.
cowbell1 is offline   Reply With Quote
Old 02-20-2008, 02: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
sobrien79 is an unknown quantity at this point
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.
sobrien79 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 02:52 AM.


Advertisement
Log in to turn off these ads.