Petoux
4th February 2007, 07:22 PM
The below code is supposed to "Write an application to calculate the diameter, circumference, and area of a circle with a radius input by the user. Assign the radius to a float (Why the fuck?) variable, and then output the radius with an appropriate message. Declare a named constant PI (or something similar) with the value of 3.14159. The application should output the diameter, circumference, and area, each on a separate line, with identifying labels. Be sure to include appropriate comments in your code, choose meaningful identifiers, and use indentation as we do in this chapter."
...
Also, am I missing anything? I don't think so personally, and everything works fine ... no errors, completely clean. I was just simply wondering if it could be polished or added to somehow more efficiently.
I am also tempted to make "float radius; // radius" a double because it makes more sense, but the book says "float" :/ ... Any ideas on that too?
Thanks.
^.^
/**
*
* @author me
*/
import javax.swing.JOptionPane; // use JOptionPane
public class circle
{
public static void main( String args[] )
{
String firstNumber; // first string entered by user
float radius; // radius
double diameter; // radius * 2
double area; // Pi * radius^2
double circumference; // 2 * Pi * radius
double pi;
firstNumber = JOptionPane.showInputDialog( "Enter radius of a circle" );
pi = 3.14159;
radius = Integer.parseInt( firstNumber );
diameter = radius * 2;
area= pi * radius * radius;
circumference = 2 * pi * radius;
JOptionPane.showMessageDialog( null, "The Diameter of your circle is: " + diameter );
JOptionPane.showMessageDialog( null, "The Circumference of your circle is: " + circumference );
JOptionPane.showMessageDialog( null, "The Area of your circle is: " + area );
System.exit( 0 ); // terminate application with window
} // end method main
} // end class Circle
...
Also, am I missing anything? I don't think so personally, and everything works fine ... no errors, completely clean. I was just simply wondering if it could be polished or added to somehow more efficiently.
I am also tempted to make "float radius; // radius" a double because it makes more sense, but the book says "float" :/ ... Any ideas on that too?
Thanks.
^.^
/**
*
* @author me
*/
import javax.swing.JOptionPane; // use JOptionPane
public class circle
{
public static void main( String args[] )
{
String firstNumber; // first string entered by user
float radius; // radius
double diameter; // radius * 2
double area; // Pi * radius^2
double circumference; // 2 * Pi * radius
double pi;
firstNumber = JOptionPane.showInputDialog( "Enter radius of a circle" );
pi = 3.14159;
radius = Integer.parseInt( firstNumber );
diameter = radius * 2;
area= pi * radius * radius;
circumference = 2 * pi * radius;
JOptionPane.showMessageDialog( null, "The Diameter of your circle is: " + diameter );
JOptionPane.showMessageDialog( null, "The Circumference of your circle is: " + circumference );
JOptionPane.showMessageDialog( null, "The Area of your circle is: " + area );
System.exit( 0 ); // terminate application with window
} // end method main
} // end class Circle