View Full Version : Is there an efficient way of shortening this to get the same result?

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?



* @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

4th February 2007, 08:18 PM
One thing you can do, although not for efficiency...

Instead of these two lines

diameter = radius * 2;
JOptionPane.showMessageDialog( null, "The Diameter of your circle is: " + diameter );

Use like

JOptionPane.showMessageDialog( null, "The Diameter of your circle is: " + radius*2 );

That eliminates some variable declarations and calculations.

Also, just use console output instead of GUI?

4th February 2007, 08:38 PM
you could save a LITTLE bit of ram by doing
static const double pi = 3.14... outside of the public static void main; and make circumference = pi * diameter; and when doing math you shouldn't mix types, make radius a double, there is a potential loss of percision of the calculation because it will truncate at the lowest number of decimals.

5th February 2007, 08:01 AM
firstNumber = JOptionPane.showInputDialog( "Enter radius of a circle" );
radius = Integer.parseInt( firstNumber );

mate, the first thing you should learn is Input validation!!
tell me what happens to your program when the user decides to just press "cancel" at the input dialog? or he migh think he discovered something new and types "vegna is a gay!!" ??
I will give you a hint..
public static int parseInt(String s) throws NumberFormatException
it throws and exception that must be catched

believe me.. if you start thinking bout Input validation now... you will have less problems with bugs and hijackers exploiting vulneravilities in your code later.

radius = Integer.parseInt( firstNumber );
you are asigning an int value to a float its ok cos an int is automatically casted to a float when needed but what happens if you want to calc the diameter, circunference .. of a circle of radius 2.5? wouldnt it make more sence to use a floating point number in the first place?

radius = Float.parseFloat( firstNumber );

and when doing math you shouldn't mix types, make radius a double, there is a potential loss of percision of the calculation because it will truncate at the lowest number of decimals.
thats not true, in an aritmetic operation a float will be automatically converted to a double when needed (no explicit cast required) with no precision loss and the result will be a double.

5th February 2007, 08:23 AM
In a lot of legacy compilers Float loses precision when used with a double; that's the reason for the warning on compile time on most legacy C compilers. The reason I think it should be corrected NOW is that he is in an intro level class, it's best that he NOT do it in java, so he doesn't do it in C; it results in practices.

In terms of handling exceptions... for this it isn't such a big deal, but later it will be.

Vegna, read up about try/catch clauses.

5th February 2007, 08:38 AM
In java you're right, but for legacy compilers you're wrong... which is why it's stylistically retarded to NOT use the same datatype. People move on from java to C, write for an old compiler, and have code that doesn't work worth a shit, this is why; correct it early.

umm yeah you are right with the datatype thingie, in the same sense that i sugest him about the imput validation thingie, if you get used to proper coding stardars early in your learning prosses it will make your life easier. I'm sure you already noticed that once you learn to program it will take you 1-2 hours to learn the sintax and datatypes of a new hight level language and be able to write usefull code in it, but are particular features like "automatic cast" and "api structure" that will take you some time to discover on any language.

edited:removed some part of no relevance after chiserv edits :P