Results 1 to 7 of 7
  1. #1
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Main method separate // Fibonacci numbers

    Here's a class called Fibonacci

    Java Code:
    public class Fibonacci {
    	
    	public static int fib(int n) {
                    if (n < 2) {return n;}
                    else {return fib(n-1)+fib(n-2);}
    	}
    	
    	public static void main(String[] args) {
    		System.out.println("Fibonacci");
    	    for (int i=1; i<=10; i++) {
    	    	System.out.println("Number " + i + ": " + fib(i)+"");
    	    }
    	}
    	
    }
    As you can see, it contains one method called fib() and one main method.

    If I would want to have the main method in another class than fib(), how would I write the two classes? Only cutting the main method from this class to another one doesn't work.

    My question is, is the reason it doesn't work because I then would have to have a constructor in the Fibonacci class, and create a Fibonacci object first which I then use the method on?
    Last edited by Stud1; 01-13-2014 at 04:25 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Main method separated from other method

    Quote Originally Posted by Stud1 View Post
    My question is, is the reason it doesn't work because I then would have to have a constructor in the Fibonacci class, and create a Fibonacci object first which I then use the method on?
    What happened when you tried that?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Main method separated from other method

    Quote Originally Posted by KevinWorkman View Post
    What happened when you tried that?
    It works. But I'm wondering if it's possible to use the method in another class without having a constructor?

    I mean, which object is the method performed on when the method and the main method are in the same class. Since I didn't create any object at all then?

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

    Default Re: Main method separated from other method

    Quote Originally Posted by Stud1 View Post
    It works. But I'm wondering if it's possible to use the method in another class without having a constructor?

    I mean, which object is the method performed on when the method and the main method are in the same class. Since I didn't create any object at all then?
    Since fib() is a static function, you don't need to create an instance to call it. Similarly, out is a static variable in the System class.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Main method separated from other method

    Quote Originally Posted by KevinWorkman View Post
    Since fib() is a static function, you don't need to create an instance to call it. Similarly, out is a static variable in the System class.
    Ah, I see how it works now. Thanks for the explanation.

  6. #6
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Main method separated from other method

    Another thing about this class, which is not related to the topic but to the class so I guess it's ok to ask it here. The class I wrote above works well for calculating Fibonacci numbers up to the 40th number or so. But after that, the calculations start to take very long (please try it yourself and you see what I mean).

    How could I make so that it calculates also higher numbers faster? For example if I want to calculate the 100th Fibonacci number?

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Main method separated from other method

    Quote Originally Posted by Stud1 View Post
    Another thing about this class, which is not related to the topic but to the class so I guess it's ok to ask it here. The class I wrote above works well for calculating Fibonacci numbers up to the 40th number or so. But after that, the calculations start to take very long (please try it yourself and you see what I mean).

    How could I make so that it calculates also higher numbers faster? For example if I want to calculate the 100th Fibonacci number?
    You're using recursion here, which is going to be more expensive than, say, a loop. You also might want to google something like "Fibonacci algorithm" for alternative approaches.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 7
    Last Post: 04-11-2013, 05:31 AM
  2. Replies: 4
    Last Post: 03-28-2013, 08:40 PM
  3. Main method
    By suresh.sa in forum New To Java
    Replies: 2
    Last Post: 10-22-2010, 09:16 AM
  4. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  5. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 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
  •