Results 1 to 14 of 14
  1. #1
    codybear11 is offline Member
    Join Date
    Feb 2015
    Posts
    7
    Rep Power
    0

    Default calling a method in another method.

    hey all. So i am trying to write a code that creates a robot's head using turtles. This is an assignment for an intro to programming course, and I must make various methods using loops that draw different shapes using the turtles. One of the specifications is that for the eyes of the robot, I must make a method called drawCamera (which has some variable that dictates how the shape will look, separate of the other method) and call upon another method in that method, . I am trying to call upon my drawRegularPolygon method. Here is my drawRegularPolygon method:

    //drawRegularPolygon method//
    public void drawRegularPolygon (int width, int numSides){
    int angle = (int)(360/numSides);
    for(int i = 0; i<numSides; i++){
    this.turn(angle);
    this.forward(width);
    }
    }

    this method works, as I drew the ears of the robot using this method. Here is what I have for my drawCamera method:


    //draw Camera method (for the eye of the robot)//

    public void drawCamera (int size){
    this.setPenWidth(size);
    this.drawRegularPolygon();
    }
    }


    How should I change the code so that I call on the drawRegularPolygon method in this new method? Thanks :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: calling a method in another method.

    When posting code, please use the code tags to preserve formatting.

    But take a look at your drawRegularPolygon() function. Notice that it takes parameters. That means when you call that function, you have to pass in arguments.

    However, when you call the drawRegularPolygon() from your drawCamera() method, you aren't providing any arguments. I would expect this to result in a compilation error.

    Recommended reading: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    codybear11 is offline Member
    Join Date
    Feb 2015
    Posts
    7
    Rep Power
    0

    Default Re: calling a method in another method.

    thanks for the response!
    Im not sure what you mean by not providing any arguments. Do you mean that I must refer to the variables that I used in the drawRegularPolygon method again in my drawCamera method? if so, how would I do this?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: calling a method in another method.

    Look at how when you call the setPenWidth() function, you pass in an argument.

    You need to do the same thing with your drawRegularPolygon() method.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    codybear11 is offline Member
    Join Date
    Feb 2015
    Posts
    7
    Rep Power
    0

    Default Re: calling a method in another method.

    I'm sorry, I don't think I understand what is meant by "argument". I am new to any sort of computer programming :p

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: calling a method in another method.

    Did you read the link I gave you? It explains what an argument is.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    codybear11 is offline Member
    Join Date
    Feb 2015
    Posts
    7
    Rep Power
    0

    Default Re: calling a method in another method.

    yeah, and I don't understand what the difference between an argument and parameter is.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: calling a method in another method.

    And what did google tell you?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  9. #9
    codybear11 is offline Member
    Join Date
    Feb 2015
    Posts
    7
    Rep Power
    0

    Default Re: calling a method in another method.

    nothing that I could understand. I've had a total of 100 minutes of comp sci lectures so far and am in a class intended for people with prior programming experience so the pace is a bit fast so far. I'm having a bit of trouble with the terminology, and the sources I've read online all use terminology I can't understand too well. Could you explain what the difference between argument and parameter, in terms that a little kid would understand? :p

  10. #10
    codybear11 is offline Member
    Join Date
    Feb 2015
    Posts
    7
    Rep Power
    0

    Default Re: calling a method in another method.

    something else I'm confused with and something i asked my prof this morning but she didn't do too well of a job explaining is if I can make the drawCamera method such that I can declare all 3 "parameters" (not sure if it would be argument of parameter :P) while calling the drawCamera method, or if in the drawCamera method I would have to declare the width and numsides of the regular polygon and then dictate the pen width when calling the method. Thanks again!

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: calling a method in another method.

    You really might want to consider getting into a more basic class. You can't just start learning about calculus without knowing arithmetic yet.

    The link I provided is a full tutorial that contains explanations of all of the terms you need to know.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  12. #12
    codybear11 is offline Member
    Join Date
    Feb 2015
    Posts
    7
    Rep Power
    0

    Default Re: calling a method in another method.

    I can't. My major (physics) requires me to take a computational physics class first semester third year, and a pre req for that class is the one I'm in now, which is only offered during second semester. I intend on catching up with my coding skills during my break in 2 weeks, but until then I need to complete this assignment and hand it in (due at 11:59 tonight). Could you offer any help into getting my method to work? Thanks

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: calling a method in another method.

    Go back and read the basics. The tutorial I posted is a good start. If that specific tutorial is too difficult to follow, then start from the beginning of the tutorials.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  14. #14
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    12

    Default Re: calling a method in another method.

    Check out this question on Stack Overflow: c# - What&#39;s the difference between an argument and a parameter? - Stack Overflow

    Basically, an argument is what gets passed into a method, and a method has defined parameters that it can use. I usually just use the term parameter. Looking at your code, in the "drawCamera(int size)" method, the "size" variable is a parameter coming into the method, and it gets passed as an argument to the "setPenWidth" method. If you then look inside the "setPenWidth" method, you would refer to the size variable as a parameter for the method.

    So if your teacher is saying to add parameters to your "drawCamera" method, try defining more variables that can be passed in that you would then use when performing that method (actually drawing the camera).

Similar Threads

  1. Replies: 17
    Last Post: 10-23-2014, 08:17 PM
  2. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  3. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  4. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Posting Permissions

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