Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Program not compiling + calling methods help

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program not compiling + calling methods help

    Sup guys, newcomer to the forums with a relatively easy question to ask. Being a relatively newcommer to Java, my first assignment was to create a simple program which asked the user to input a desired radius, and then I was to create several helper methods which used the radius as a passing argument.

    I'm using Netbeans 7.2
    Code goes as follows:

    package circleapp;
     
    /**
     *
     * @author Alex
     */
     
    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class CircleApp 
        {
     
     
     
     
     
        public static void main(String[] args) 
            {
                double radius;
                double circ;
                double area;
     
                radius = getRadius();
     
                circ = calcCirc();
                area = calcArea();
     
                DecimalFormat fmt = new DecimalFormat ("0.##");
     
               System.out.println("The circumference of the circle is" + circ + "\n"
                       + "The area of the circle is" + area + "\n" + "Thank you"
                       + "for using my program");
     
            }
     
         public static double getRadius()
         {
           double radius; 
           Scanner scan = new Scanner(System.in);
           System.out.println("Enter the radius of the circle");
           radius = scan.nextDouble();
           return radius;
         }
     
         public static double calcCirc(double rad)
         {
     
             radius = rad;
             double circ;
             double pi;
             pi = 3.14159;
     
             circ = 2.0 * rad * pi;
             return circ;
         }
         public static double calcArea(double rad)
         {
             radius = rad;
             double area;
             double pi;
             pi = 3.14159;
     
             area = pi * rad * rad;
             return area;
         }
     
     
     
    }

    When I debugg it, the program compiles up to the beginning step of asking me for the radius, then stops due to the error found when calling the circumference method.

    Can't seem to find what is wrong with my helper methods, any ideas?
    Last edited by alex067; September 9th, 2012 at 05:45 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program not compiling + calling methods help

    stops due to the error
    Please post the full text of the error message.

    Also please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Program not compiling + calling methods help

    1) please put [code=java] before your code and [/code] after your code, it makes the code pretty for my old eyes.
    2) when you have a question associated with an error message, always copy paste the full text of the error message, and all relevant code in your post. This makes it easier for people to see what is necessary to help you all in the first go.

    You have defined two methods as follows:
    public static double calcCirc(double rad)
    public static double calcArea(double rad)

    ...and subsequently called those methods as follows
    circ = calcCirc();
    area = calcArea();

    Notice anything different in the footprint? The methods each have a parameter of type double, and the calls to the methods do not have this necessary data. Modify the calls to supply the double maybe?

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not compiling + calling methods help

    Sorry about not putting it around the code format!

    So in order to fix it I would have to call the methods as, circ = calcCirc(double); ?
    and area = calcArea(double);

    Or instead of (double) I would put (radius)? is that how I would succesfully pass an arguement into a method?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program not compiling + calling methods help

    Put the name of the variable with the value you want to pass to the method within the ()s
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Program not compiling + calling methods help

    when ur calling calcCirc & calcArea u aren't passing any parameters.... check the methods u defined outside of the main and see if you're following the rules u specified for the two

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not compiling + calling methods help

    Quote Originally Posted by C++kingKnowledge View Post
    when ur calling calcCirc & calcArea u aren't passing any parameters.... check the methods u defined outside of the main and see if you're following the rules u specified for the two
    so when I identify calcCirc, instead of calcCirc(); I would have to identify it as calcCirc(double radius); in order to pass the radius value onto the calcCirc method?

  8. #8
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Program not compiling + calling methods help

    ur on the right track... since u defined radius as a double already u dont have to write (double radius) in the parameter just (radius)... o yeah and u didnt assign any type to radius in ur calcCirc and calcArea methods so u should do something about that so ur program will compile correctly

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program not compiling + calling methods help

    Quote Originally Posted by C++kingKnowledge View Post
    ur on the right track... since u defined radius as a double already u dont have to write (double radius) in the parameter just (radius)... o yeah and u didnt assign any type to radius in ur calcCirc and calcArea methods so u should do something about that so ur program will compile correctly
    What do you mean I didn't assign any type to radius in my calcCirc and calcArea? In the methods I put radius = rad; is that wrong? am I supposed to put double rad = radius???

  10. #10
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Program not compiling + calling methods help

    Quote Originally Posted by alex067 View Post
    What do you mean I didn't assign any type to radius in my calcCirc and calcArea? In the methods I put radius = rad; is that wrong? am I supposed to put double rad = radius???
    The number type for the radius is already declared in the calcCirc and calcArea methods, so you don't need to do it again. They expect doubles.

    As for rad/radius: The methods already have a double rad variable declared in the parameters, so you can happily just use rad. (Remember that the radius variable declared in main won't be seen by the other methods.)

Similar Threads

  1. Calling upon methods
    By jonathanfox in forum Java Theory & Questions
    Replies: 4
    Last Post: August 3rd, 2012, 11:58 AM
  2. Calling methods in other files
    By zdavos in forum Object Oriented Programming
    Replies: 4
    Last Post: March 2nd, 2012, 07:57 AM
  3. [SOLVED] Calling methods from other classes
    By knightknight in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 7th, 2011, 06:16 PM
  4. Calling worker methods in JSF.
    By newbie in forum Web Frameworks
    Replies: 0
    Last Post: March 18th, 2011, 02:21 PM
  5. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM