Results 1 to 6 of 6
  1. #1
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Question Recusion problem

    Hi Everyone..
    I' am a new member of this site. I have a problem with my program. We use recursion in creating a program called fibonacci. Our instructor give us this program last December 12, 2008. He wanted us to solve the lowest number and the higher number and display the answer and it will add the previous number. We use java programming language. The output will be like this:
    11235813....
    We use recursion in creating this program and I dont know how to start it.
    Your help is well appreciated. Thank You.

  2. #2
    ptsofathrty is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default try this

    public class Fibonacci {
    private long number;
    private int index;

    public Fibonacci ( ) {
    number = 0;
    index = 0;
    }

    public static void main ( String [] args ) {
    Fibonacci program = new Fibonacci ( );
    program.GetIndex ( );
    program.FindAndPrint ( );
    }

    public void GetIndex ( ) {
    TextReader keyboard = new TextReader ( );
    do {
    System.out.print ( "\n\n\nEnter a Fibonacci index (1 to 25) -> " );
    index = keyboard.readlnInt ( );
    } while ( index < 1 || index > 25 );
    }

    public void FindAndPrint ( ) {
    number = FindFib ( index );
    System.out.println ( "The Fibonacci number at index " + index + " is: " + number );
    }

    public int FindFib ( int i ) {
    // System.out.println ( i );
    if ( i == 1 || i == 2 )
    return 1;
    else
    return ( FindFib (i - 2) + FindFib(i - 1) );


    }
    }

  3. #3
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Smile hi

    hi,
    Thank you for the program but it has 2 errors. line 17. It is said that cannot find symbol
    symbol : class TextReader
    location: class Fibonacci
    TextReader keyboard = new TextReader ( );

  4. #4
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Default

    hi,
    Thank you for the program but it has 2 errors. line 17. It is said that cannot find symbol
    symbol : class TextReader
    location: class Fibonacci
    TextReader keyboard = new TextReader ( );

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    @ptsofathrty: It's probably not a good idea to post complete code for the OPs. Too much temptation to cut & paste & forget (no learning involved). The purpose of this forum is for people to learn, not get free code. I'm pretty sure that Mika doesn't know what the code does because he/she hasn't figured out what the TextReader method is supposed to do or why it's failing.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    exornextman is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    hello...... i have another problem, how could i display messages or statement in a JOptionPane which this JOptionPane was inside of a LOOP.... someone suggested to use ConfirmationDialog isntead of MessageDialog.... but my INSTRUCTOR said that I must use Message dialog because I`m only displaying the result.
    ex. in BufferedReader the output is like this: 1 2 3 4 5, how could I display this in JoptionPane in the same way?
    code:
    "only the loop part"
    [1] for(int countr=0; countr<=20; countr++){
    System.out.print(" "+countr);
    }
    in this way there is no problem it will print 0 1 2 to 20;

    [2]for(int countr=0; countr<=20; countr++){
    JOptionPane.showMessageDialog(" "+countr);
    }
    in this way u need to click the OK before you can view the other numbers so you need to click OK 20 times;
    HOW COULD I DISPLAY IT IN ONE JOPTIONPANE DIALOG THE 21 NUMBERS?

Similar Threads

  1. Recusion Help
    By calicocal in forum New To Java
    Replies: 3
    Last Post: 12-02-2008, 09:18 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
  •