Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    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.

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

  • #3
    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 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.

  • #4
    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;

  • #5
    Regular Coder Aradon's Avatar
    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

  • #6
    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.

  • #7
    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);
    
    	}
    
    }

  • #8
    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-19-2008)

  • #9
    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.

  • #10
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •