Java Program Question(Simple)

Discussion in 'Software' started by Newskin01, Sep 13, 2010.

  1. Newskin01

    Newskin01 Private E-2

    Hello,

    I cannot get it to compute using my formula is there something wrong here?


    package csc212lab02a;

    import java.text.DecimalFormat;
    /**
    *
    * @author sudddsadfdsfddsfns
    * course CSC 212 Section fsdfds
    * date September 13, 2010
    * description: Program to calculate the volume of
    * of a cone with height and radius hard-coded
    * into the application
    * Note: volume = 1/3 x PI x R^2 x height
    */
    public class Main
    {

    /**
    * The radius of the cone.
    */
    static double radius;

    /**
    * The height of the cone.
    */
    static double height;

    /**
    * The volume of the cone.
    */
    static double volume;

    public static void main(String[] args)
    {

    // Display a "startup banner" for the application
    System.out.println("Welcome to Conan");
    System.out.println("\t... a program which calculates the area of a cone.");

    // Create a DecimalFormat object that displays
    // numbers to two decimal places
    DecimalFormat df = new DecimalFormat("0.00");

    // Set values for radius and height
    radius = 8;
    height = 18;

    // Volume of cone = (1/3) * pi * radius2 * height
    volume = 1/3 * Math.PI * Math.pow(radius, 2) * height;

    System.out.println("The volume of a cone with " +
    "radius " + df.format(radius) +
    " and height " + df.format(height) +
    " is " + df.format(volume));


    }

    }
     
  2. Newskin01

    Newskin01 Private E-2

    Always the culprit, Integer division.

    Remember that when you give the division operator (/) two integers, it returns another integer.

    1/3 = 0
    1.0/3.0 = .3333333333333 (ish)
    1f/3f = .33333333333333 (ish)
    1/3.0 = .33333333333 (ish)

    Please, note the "ish" above, by that I mean there is no binary representation for 1/3 so the value will be the floating point whose value is as close to it as possible, this may be different, so you should never compare floating point numbers to each other, or doubles. Instead subtract one from the other and make sure the difference is less than some margin of error.


    source : http://answers.yahoo.com/question/index?qid=20100913135239AAw5B6B

    You can delete topic as I answered my own question.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds