Results 1 to 11 of 11
Thread: methods vs. non methods
- 10-10-2010, 07:36 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
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?
- 10-10-2010, 08:14 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Perhaps you could post the two gcd examples. The term "non method" is non standard.
- 10-10-2010, 09:12 PM #3
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.
- 10-10-2010, 09:13 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 10-10-2010, 09:20 PM #5
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 10-11-2010, 12:22 PM #6
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 10-11-2010, 07:40 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
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.
- 10-11-2010, 07:55 PM #8
You'd want to do something like this:
Then use that instead of the getPentagonalNumber call.Java Code:int pentagonal = n *(3*n - 1)/2;
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.
- 10-11-2010, 08:26 PM #9
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
Thanks you guys for the examples.
- 10-11-2010, 11:36 PM #10
Member
- Join Date
- May 2010
- Posts
- 27
- Rep Power
- 0
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)
- 10-11-2010, 11:46 PM #11
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Methods Please help
By bdario1 in forum New To JavaReplies: 33Last Post: 03-25-2010, 03:26 AM -
methods
By lilac87 in forum New To JavaReplies: 7Last Post: 07-22-2009, 06:37 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM -
JSP methods example
By Java Tip in forum Java TipReplies: 0Last Post: 01-30-2008, 10:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks