View Full Version : Variable less than one

02-13-2008, 10:11 PM
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 :

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
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
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
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
public class Gunpowder extends Applet implements ActionListener
// declare and construct variables
int rlen;
float grams, rdia, psi;

02-14-2008, 05:25 AM
Try making 100 100.0 and casting rlen into a float.

also, is it psi or PSI?

02-19-2008, 02:25 PM
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
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()


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
I might recommend using Float.parseFloat instead of Integer.parseInt if you are looking to get values that are not whole numbers.

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

02-20-2008, 12:47 AM
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. :thumbsup:

02-20-2008, 03:13 PM
Good time to learn it. :)

There are objects for each of the primitive types

long --> Long
boolean --> Boolean

They are definitely worth knowing. The number objects are all similar.