Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Fibonacci generator and gives the number back as output

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Fibonacci generator and gives the number back as output

    I have a program that i need to complete. I wrote the first one (FibonacciGenerator) with stated variables - everything worked fine
    I now want to split it up so that the user can give a number and the program gives the correct number back. (ie, fib(5)=5 AND NOT fib(5)=2)
    Please help as I know what i need to do but the how escapes me.

    constructor portion
      /*
       Class used to generate a Fibonacci number with a given input
    */
        public class FibonacciGenerator
       {
       /**
          Construct a FibonacciGenerator object to
             generate a Fibonacci number
       */
           public FibonacciGenerator()
          {
             initialize();
          }
     
       	/**
       	Initializes the FibonacciGenerator object. Allows
       	the object to be reset and reused.
       	*/   	
     
           public void initialize()
          {
             fold1 = 1;
             fold2 = 1;
             fnew = 1;
             count = 0;
          }   
       /**
          Method used to calculate a fibonacci number
          @return fnew the fibonacci number
       */
           public int nextNumber()
          {
             if(count < 2)
             {
                count++;
                return 1;
             }
             else
             {
               	fnew = fold1 + fold2;
     
                fold2 = fold1;
                fold1 = fnew;
     
                return fnew;
             }
          }
     
          private int fold1;
          private int fold2;
          private int count;
          private int fnew;
       }

    Test Program (I know i need to return the input to the constructor file but how??)
      import javax.swing.JOptionPane;
     
    /**
       Test driver class for FibonacciGenerator class
    */
    public class FibonacciGeneratorTester
    {
       public static void main(String[] args)
       {
          String input = JOptionPane.showInputDialog(
             "Enter a Number or hit ''Cancel'' to exit:");
          int n = Integer.parseInt(input);
     
          FibonacciGenerator fg = new FibonacciGenerator();
     
          int next = n;
     
          if (n < 2)
             next = 1;
          else
          {
             for (int i = 3; i <= n; i++)
             {
                next = fg.nextNumber();
             }
          }
     
          System.out.println("fib(" + n + ") = " + next);
     
          System.exit(0);
       }
    }

    Any help is appreciated


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: help with FibonaccciGenerator

    Welcome to Java Programming Forums, Aurora.

    Have a read of this, a previous topic about the fibonacci sequence see if it helps you out. If it doesn't then get back to us and I'll have a proper look over your code.

    http://www.javaprogrammingforums.com...-sequance.html

    Regards,
    Chris