Results 1 to 6 of 6

Thread: Writing a JAVA Program for Calculating the Circumference of a Circle from a radius

  1. #1

    Thread Starter
    New Member SheFishy's Avatar
    Join Date
    Sep 2006
    Posts
    2

    Question Writing a JAVA Program for Calculating the Circumference of a Circle from a radius

    i found a similar query under the "C / C++" forum that was answered apparently using the "C" language. im in dire need of it in Java. i was wondering if someone could help me with it as i am having a hard time figuring out where to begin.

    can anyone please show me how to write the java program taking into consideration the following conditions? :

    1) must be a stand-alone application
    2) calculation of circle's circumference must be from a radius which will be an integer value input from the keyboard.
    3) create a method that will accept the integer and perform the calculation using the formula: "2*pi*r".
    4) may use either the value "3.14" or the Java variable, "Math.PI" for the calculation.


    i would really appreciate the help! thanks a bunch in advance!


    PS - by the way, i use the Eclipse SDK compiler.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Writing a JAVA Program for Calculating the Circumference of a Circle from a radiu

    to the Forums
    Try this:
    Code:
    public class MyClass
    {
      public static void main ( String args[] )
      {
        Scanner s = new Scanner( System.in ) ;
        System.out.println( "Please enter the radius of the circle:" ) ;
        int r = s.nextInt() ;
        System.out.printf( "The circumference of the circle of radius %d is %f", r,
    		       Circumference( r ) ) ;
      }
    
      private static double Circumference ( int radius )
      {
        return Math.PI * 2 * radius ;
      }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    New Member SheFishy's Avatar
    Join Date
    Sep 2006
    Posts
    2

    Smile Re: Writing a JAVA Program for Calculating the Circumference of a Circle from a radius

    yep, it did work. just changed the variables and added a few more. thanks a bunch!

    thanks for the welcome greeting, too. cheers!

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Writing a JAVA Program for Calculating the Circumference of a Circle from a radius

    You are welcome, don't forget to mark the Thread resolved from the "Thread Tools" menu
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    New Member
    Join Date
    Nov 2006
    Posts
    1

    Re: Writing a JAVA Program for Calculating the Circumference of a Circle from a radius

    Wow...that program worked great. I was wondering if there was a way to do the same thing except also:

    Write your own constructors, your own copy constructor, your own equals method. Include a method to prompt the user for the radius of a circle. And then Define your own toString() method. In your driver use the toString() method to test your constructor and to display the current radius after the user has entered a radius.

    Any ideas??

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Writing a JAVA Program for Calculating the Circumference of a Circle from a radius

    ???

    Constructors? Copy constructors? (Never heard that term in relation to Java.) toString()?

    What nonsensical assignment is that?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width