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

}

Fahrenheit 2 Celsius

| Posted in , | Posted on 11:53 PM

This is considerably simpler than my banking interest calculation but then again it's late, a weekend, and I just wanted to get some practice code in even if it's nothing spectacular. It's clean and I'm happy with that.

It's a small, short, and sweet program that takes a temperature (Fahrenheit) and converts it to Celsius.


/*
* This is a simple program meant to convert a fahrenheit
* temperature to celsius. It's drawn from Chap 2, Ex 1
*/

package fahrenheit_2_celcius;

// pull in a javax class for gui message boxes
import javax.swing.JOptionPane;

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

public static void main(String[] args) {

// prompt user for a fahrenheit temperature and store it in variable
String stringFahrenheit = JOptionPane.showInputDialog (null,
"Enter your temperature" + " in fahrenheit", "Fahrenheit 2 "
+ "Celsius Calculator", JOptionPane.QUESTION_MESSAGE);

// convert stringFahrenheit into double variable
double fahrenheit = Double.parseDouble(stringFahrenheit);

// convert fahrenheit into celcius and store it in variable
double celsius = (5.0/9.0)*(fahrenheit-32.0);

// display converted temperature

JOptionPane.showMessageDialog(null, fahrenheit + " degrees fahrenheit"
+ " is equal to " + celsius + " degrees celsius.", "Fahrenheit 2 "
+ "Celsius Calculator", JOptionPane.QUESTION_MESSAGE);

}

}