Find that Cylinder's Volume!

| Posted in , , | Posted on 12:41 AM

Easy.... as.... pi!

/*
* This is a basic application meant to calculate the volume
* of a cylinder given its radius and length
*/

package cylinder_volume;
import javax.swing.JOptionPane;

/*
* @author Neil
*/
public class Main {


public static void main(String[] args) {

// Prompt user for radius of cylinder and store it as variable
String stringRadius = JOptionPane.showInputDialog(null, "Enter the"
+ " radius of the cylinder.", "Find that Volume!",
JOptionPane.QUESTION_MESSAGE);
// Convert string value to a double variable
double radius = Double.parseDouble(stringRadius);

// Prompt user for the length of the cylinder and store it as a variable
String stringLength = JOptionPane.showInputDialog(null, "Enter the"
+ " length of the cylinder.", "Find that Volume!",
JOptionPane.QUESTION_MESSAGE);
// Convert string value to a double variable
double length = Double.parseDouble(stringLength);

/*
* Calculate the volume of the cylinder by first finding the area
* of the base and then multiplying it by the length.
*/

// Declare area, calculate it, and then store it
double area = (radius * radius) * 3.141593;

// Declare volume, calculate it, and then store it
double volume = area * length;

// Display result of calculation
JOptionPane.showMessageDialog(null, "A cylinder that is " + length +
" long and " + radius + " in radius has a volume of " + volume,
"Find that Volume!", JOptionPane.QUESTION_MESSAGE);
}

}

Comments Posted (0)

Post a Comment