Results 1 to 3 of 3
 10072013, 10:26 PM #1Member
 Join Date
 Sep 2013
 Posts
 19
 Rep Power
 0
Program compiles but don't think the math formulas are calculating correctly.
Trying to get familiar with using math functions in Java. Trying to run a program that calculates the volume of a cone. The program compiles but I get a different value when I manually enter the numbers into a calculator compared to the value that displays when the program runs in Java.
The formula for the volume of a cone is: pi * height * radius * radius /3
When entering the values into a calculator I get 261666.66
When compiling and running the program in java, it returns the value of 261799.38
Here's the code:
Java Code://Purpose of this program is to calculate the volume of a cone. import java.lang.Math; import java.util.*; class Cone { double r; //declare radius variable double h; //declare height variable public void setR(double radius) { r = radius; } public void setH(double height) { h = height; } public double getR() { return r; } public double getH() { return h; } public double VolumeFormula() { double volume = Math.PI * h * Math.pow(r,2)/3; return volume; } } class ConeTestDrive { public static void main (String[] args) { Cone c = new Cone(); c.setR(50); c.setH(100); System.out.println("Radius:" + c.getR()); System.out.println("Height:" + c.getH()); System.out.println("The volume of the cone is:" + c.VolumeFormula()); } }
 10072013, 11:56 PM #2
Re: Program compiles but don't think the math formulas are calculating correctly.
You must have entered the formula wrong in the calculator. I get these results:
Calculator: 3.141592653589793 * 100 * 50 * 50 / 3 = 261799.38779914941666666666666667
Your program: The volume of the cone is: 261799.38779914947
 10082013, 12:06 AM #3Member
 Join Date
 Sep 2013
 Posts
 19
 Rep Power
 0
Similar Threads

this program compiles with no suntax errors but does not run,help !
By Udaycampus in forum New To JavaReplies: 3Last Post: 12082012, 03:36 AM 
program compiles but does not work
By alara in forum New To JavaReplies: 4Last Post: 11272011, 08:47 PM 
Blackjack Program Not Calculating Totals Correctly
By skaterboy987 in forum New To JavaReplies: 1Last Post: 10192011, 08:59 PM 
This program compiles but doesnt run properly!
By ErikD99 in forum New To JavaReplies: 5Last Post: 12032010, 08:44 PM 
Program Compiles but Buttons do not display
By ljk8950 in forum AWT / SwingReplies: 8Last Post: 08112008, 03:41 AM
Bookmarks