Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By jim829

Thread: How to create methods?

  1. #1
    hammodi is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Angry How to create methods?

    i am pretty good at java programming. i did something lie 5 labs a day in my computer class.. but i got stuck on methods. i created the labs and they work, my teacher want them to have methods. first of all i dont understand whats the point of methods. they're useless they 're just more work for no benefit. give me a definite way to create a god damn method?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: How to create methods?

    If you don't understand methods and think they're useless then you are simply not as good at programming as you might
    think you are. Why would you make a definitive statement about the usefulness of something you don't understand?

    A method is a part of a program which uses a series of statements to perform some function and possibly return a result.
    The method may receive zero or more arguments which are used in the computation. And the method may be called from
    various locations within a program. So contrary to your uninformed statement, it reduces the amount of work and typing
    one has to do in writing an application.

    If you want to understand the syntax, check this out --> Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Regards,
    Jim
    milovan likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: How to create methods?

    Quote Originally Posted by jim829 View Post
    Why would you make a definitive statement about the usefulness of something you don't understand?
    Isn't that just a human thing? I DON'T UNDERSTAND THIS, SO I'LL KILL IT!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create methods?

    ... and especially recursive methods are utterly useless; recursion is an invention of the devil.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: How to create methods?

    For filesystem scanning purposes I kind of like recursive methods (although that might blow up with really complex and deeply nested file systems), but other than that: blegh!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create methods?

    Quote Originally Posted by gimbal2 View Post
    For filesystem scanning purposes I kind of like recursive methods (although that might blow up with really complex and deeply nested file systems), but other than that: blegh!
    I love recursion (my old mom thinks I'm posessed); there's only one drawback though: Java doesn't handle tail recursion, but if they want to go more to the functional programming way in Java 8, they really should reconsider that; stacks are still limited in size ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: How to create methods?

    Quote Originally Posted by JosAH View Post
    I love recursion (my old mom thinks I'm posessed);
    For filesystem scanning purposes I kind of like recursive methods (although that might blow up with really complex and deeply nested file systems), but other than that: blegh!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    ArnavKumar is offline Member
    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Default Re: How to create methods?

    Methods are nothing but the same as functions in C or C++.
    The purpose of their use is to follow Do not Repeat Yourself principle. If you create a good method you can use it in other class or in other package without repeating your code again. It gives you a great power. For example:

    public void MathTable(int a){
    for(int i=1;i<=10;i++)
    {
    System.out.print(a+"*"+i+"="+(a*i));
    System.out.println();
    }
    }

    you can call this method any time to print a mathematics table by passing an int from any class and from any package after importing them correctly.
    Last edited by ArnavKumar; 11-01-2013 at 08:52 AM. Reason: spelling mistake

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: How to create methods?

    These days methods are more about breaking your code up into manageable chunks. A side effect of this is that you avoid repetitive code, but that's more of a bonus than a goal.

    If you find your method is doing too much, or getting too long, then it's probably time to refactor it into smaller methods, even if those methods are only called the once.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: How to create methods?

    Quote Originally Posted by Tolls View Post
    These days methods are more about breaking your code up into manageable chunks.
    There was once a time where this was not the case? Its been like that as long as I remember, be it in assembly, Pascal, C, C++ or Java.

    In any case I don't really focus on making it small and manageable myself; I focus on the process of that naturally happening. If the code is written in a sensible way, it should be something automatic in stead of something that you have to force yourself to do. In other words: think about it and let things fall into place because they make sense.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: How to create methods?

    I seem to remember back in the 80s being told "break out repetitive code" rather than "don't do too much in a method".
    It was the late 80s that the latter became more visible.

    Of course, could just be shoddy memory.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create methods?

    Quote Originally Posted by gimbal2 View Post
    There was once a time where this was not the case? Its been like that as long as I remember, be it in assembly, Pascal, C, C++ or Java.
    Yes there was; before the processor stack was invented (a Dutch invention!) there was no unified way to call methods/functions/subbroutines from other methods; you can still see remnants of this in old Fortran compilers: they have a 'link field' stored per subroutine where the previous program counter is stored; of course old Fortran didn't allow recursive subroutines ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: How to create methods?

    He he, and people complain that C++ is difficult :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create methods?

    Quote Originally Posted by gimbal2 View Post
    He he, and people complain that C++ is difficult :)
    But C++ is difficult; the language is anyway; partly because of the value semantics, partly because of Bjarne Stroustup's allergy for new keywords; it's a behemoth of a language ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: How to create methods?

    Yeah fair enough I guess. Most of the complexity that I find is that there isn't really a clear separation between the actual language and the SDK/platform such as is the case in Java.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create methods?

    Quote Originally Posted by gimbal2 View Post
    Yeah fair enough I guess. Most of the complexity that I find is that there isn't really a clear separation between the actual language and the SDK/platform such as is the case in Java.
    Well, by definition of the Standard, only the good ol' stdlib and the STL (Standard Template Library) are parts of the language; in practice however, Booch, Microsoft et.al. added their own stuff to it as well ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Create user-defined Classes & Methods
    By lala in forum New To Java
    Replies: 6
    Last Post: 02-02-2011, 02:03 PM
  2. Create Different update() Methods for Observer
    By Jonah Bron in forum Advanced Java
    Replies: 2
    Last Post: 12-21-2010, 01:38 AM
  3. Static Builder and Create Methods for simple class
    By Pilot Ace in forum New To Java
    Replies: 3
    Last Post: 08-30-2009, 10:18 AM
  4. need to create two methods
    By alims52 in forum New To Java
    Replies: 2
    Last Post: 10-26-2008, 12:35 PM
  5. How to create object dinamically and call methods.
    By Gelembjuk in forum New To Java
    Replies: 8
    Last Post: 10-26-2008, 09:28 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
  •