Java Reference
In-Depth Information
public class PondRadius {
public static void main(String[] args) {
// Calculate the radius of a pond
// which can hold 20 fish averaging 10 inches long
int fishCount = 20;
// Number of fish in pond
int fishLength = 10;
// Average fish length
int inchesPerFoot = 12;
// Number of inches in one foot
int lengthPerSqFt = 2;
// Fish length per square foot of
surface
double radius = 0.0;
// Pond radius in feet
int feet = 0;
// Pond radius - whole feet
int inches = 0;
//
- and whole inches
double pondArea = (double)(fishCount*fishLength)/lengthPerSqFt;
radius = Math.sqrt(pondArea/Math.PI);
// Get the whole feet and nothing but the feet
feet = (int)Math.floor(radius);
inches = (int)Math.round(inchesPerFoot*(radius - feet)); // Get
the inches
System.out.println(
"To hold " + fishCount + " fish averaging " +
fishLength +
" inches long you need a pond with an area of \n" +
pondArea + " square feet.");
System.out.println("The radius of a pond with area " + pondArea +
" square feet is " +
feet + " feet " + inches + " inches.");
}
}
Save the program source file as PondRadius.java . When you compile and run it, you should get the
following output:
To hold 20 fish averaging 10 inches long you need a pond with an area
of
100.0 square feet.
The radius of a pond with area 100.0 square feet is 5 feet 8 inches.
How It Works
You first define the variables that specify initial data followed by the variables feet and inches that you
will use to store the result. You then calculate the pond surface area in feet with this statement:
double pondArea = (double)(fishCount*fishLength)/lengthPerSqFt;
You cast the total length of fish to be in the pond, fishCount*fishLength , to type double to force the
division by the number of inches per square foot of pond surface to be done using floating-point values
rather than integers.
Search WWH ::




Custom Search