Results 1 to 3 of 3
  1. #1
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

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

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default 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

  3. #3
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Program compiles but don't think the math formulas are calculating correctly.

    Ah. I only entered 3.14 for pi when I entered it into the calculator. Didn't realize there was that much of a difference. Thanks.

Similar Threads

  1. Replies: 3
    Last Post: 12-08-2012, 03:36 AM
  2. program compiles but does not work
    By alara in forum New To Java
    Replies: 4
    Last Post: 11-27-2011, 08:47 PM
  3. Blackjack Program Not Calculating Totals Correctly
    By skaterboy987 in forum New To Java
    Replies: 1
    Last Post: 10-19-2011, 08:59 PM
  4. This program compiles but doesnt run properly!
    By ErikD99 in forum New To Java
    Replies: 5
    Last Post: 12-03-2010, 08:44 PM
  5. Program Compiles but Buttons do not display
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-11-2008, 03:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •