Results 1 to 11 of 11
  1. #1
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default methods vs. non methods

    I know there are many advantages on using methods. I am just trying to understand the meaning on why would we use method vs. a non method. I am new at java and it's is definitely like learning a second language. Ex. In my book it shows an example of a greatest common divisor by a non method way and a method way. Why wouldn't I be able to reuse the GCD by other programs by using the the non method way? I am confused by that concept. Could someone provide me with some other examples as well as why methods are beneficial? Should I always use methods vs non methods? In my book it says using a method isolates the problem for computing the GCD from the rest of the code in the main method. Thus, the logic becomes clear and the program easier to read. It also says it narrows the scope of debugging. Could someone explain that to me in a little more detail and with examples?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Perhaps you could post the two gcd examples. The term "non method" is non standard.

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Agreed with pbrockway. I've never heard of any way of doing Java without methods... unless you just stuff EVERYTHING into one method (the main method), but that's... silly, at best.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Arrow

    The whole purpose of methods is to recycle code, providing flexability to your application and minimizes redundancy. It also splits the code into more managable chunks where each one can be tested for functionality and make debugging simpler.

    For example:
    Java Code:
    public static void main(String[] args)
    {[INDENT]// Do stuff...
    // Do same stuff...
    // Do same stuff again...[/INDENT]
    }

    This is ok if the stuff done was only one or two lines of code but if you are dealing with twenty, thirty or even more lines of code, then you can see it getting very repeatitive. Whilst cut and paste will work, it is not good coding.

    Instead:
    Java Code:
    public static void main(String[] args)
    {[INDENT]doStuff(); // A loop may be more appropriate here.
    doStuff();
    doStuff();[/INDENT]
    }
    
    public void doStuff()
    {[INDENT]// Do lots of stuff[/INDENT]
    }

    Regards.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    Going back to what I said about reusablilty, you could create classes which provided functionality you want to use in multiple projects. From here all you would have to do is create new instances of the class and call the required method.

    For example:
    Java Code:
    Class class = new Class();
    class.methodName(); // Performs the required functionality

    Note that the names used above are for example purposes only and should not be copied directly. You will have to modify the code to suit your particular application.

    Regards.

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    A more suitable alternative to the above may be to declare the reused methods as static. This allows the method to be used without creating an instance of the class.

    Regards.

  7. #7
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Perhaps you could post the two gcd examples. The term "non method" is non standard.

    Here is an example:


    Java Code:
    public class getPentagonalNumber
    {
        public static void main(String[] args)
        {
            //Assigning a variable
           int n;
           
           //for loop to call getPentagonalNumber method and print out table
           for(n=1; n <=100; n++)
           {
              System.out.print(getPentagonalNumber(n));
              if (n % 10 == 0) 
              {
                    System.out.println();
              } 
              else 
              {
                    System.out.print("\t");
              }
           }
        }
    
           
        //method for getPentagonalNumber
        public static int getPentagonalNumber(int n)
        {
            
            return n *(3*n - 1)/2;
            
        }
    }

    Is there a way I could write this program without adding this method?

    Java Code:
    //method for getPentagonalNumber
        public static int getPentagonalNumber(int n)
        {
            
            return n *(3*n - 1)/2;
            
        }

    Are should I use this format at all times?

    I am sorry I'm such a newbie. I am still learning the java language.

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You'd want to do something like this:
    Java Code:
    int pentagonal = n *(3*n - 1)/2;
    Then use that instead of the getPentagonalNumber call.

    Usage is really up to the designer of the software. If you think you'll be getting pentagonal numbers more than once, it might as well be a method. There's little to no overhead or downfall to using methods so you might as well recycle the code as Ronin's first post suggests.

  9. #9
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Thanks you guys for the examples.

  10. #10
    somanayr is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    It is considered bad form and practice to not use methods, just FYI. It is also considered bad form to start classes with lowercase, fields with UPPERCASE (except constants), and methods with UPPERCASE. It's also considered bad form to not annotate, as other may or may not read your code in the future, especially if it's open sources. (I can tell you, I've read some pretty YUCKY code before :P)

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Some more bad form:
    -- a class name that's a verb. Class names should be nouns.
    -- a class with a method of the same name. Highly misleading.

    db

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Methods Please help
    By bdario1 in forum New To Java
    Replies: 33
    Last Post: 03-25-2010, 03:26 AM
  3. methods
    By lilac87 in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 06:37 PM
  4. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  5. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 10:00 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
  •