8 Replies - 842 Views - Last Post: 22 November 2011 - 02:40 PM Rate Topic: -----

#1 theduck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 11

Fibonacci Program method help

Posted 22 November 2011 - 01:42 PM

I am working on an assignment where the program asks the user which Fibonacci program they want to see, and then I display it to them. I have no problems with the Fibonacci part of it. I actually had the program working perfectly, compiling, and displaying the proper info. But, then I realized the homework called for three different methods. Here's where my problem comes in. I keep getting this error when I try to compile my code.

arowlands_Fibonacci.java:29: cannot find symbol
symbol : variable fibonacci
location: class arowlands_Fibonacci
fibcalc(fibonacci);
^
arowlands_Fibonacci.java:31: cannot find symbol
symbol : variable fibonacci
location: class arowlands_Fibonacci
displayOutput(fibonacci, number3);
^
arowlands_Fibonacci.java:31: cannot find symbol
symbol : variable number3
location: class arowlands_Fibonacci
displayOutput(fibonacci, number3);

And here is my code:

import java.util.Scanner;

public class Fibonacci
{

        public static void main ( String args[])
        {
                //Declare scanner and variable

                getInput();

                fibcalc(fibonacci);

                displayOutput(fibonacci, number3);
        }

        public static int getInput()
        {

                int fibonacci;
                Scanner input = new Scanner (System.in);

                System.out.println();

                System.out.print("Which Fibonacci number would you like? ");
                fibonacci = input.nextInt();

                while (fibonacci < 0 || fibonacci > 70)
                {
                        System.out.print("Which Fibonacci number would you like? ");
                        fibonacci = input.nextInt();
                }

                return fibonacci;
        }

        public static void displayOutput(int fibonacci, double number3)
        {
                System.out.printf("Fibonacci #%d is %.0f\n\n", fibonacci, number3);
        }

        public static double fibcalc(int fibonacci)
        {
                //Declare variables

                //Initialize variables
                double number1 = 0;
                double number2 = 1;
                double number3 = 0;

                //For loop to calculate Fibonacci number
                for (int i = 2; i <= fibonacci; i++)
                {
                        number3 = number1 + number2;
                        number1 = number2;
                        number2 = number3;
                }
                //Return value
                return number3;
        }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Fibonacci Program method help

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,468
  • Joined: 27-December 08

Re: Fibonacci Program method help

Posted 22 November 2011 - 01:51 PM

The fibonacci variable is local to your getInput() method and cannot be accessed outside of it. The value that fibonacci holds though, is returned from getInput(). So you could pass that value (store it in a variable or pass the method call) to the fibCalc() method. Same thing applies for your displayOutput() method.

Really, I dislike getInput() and displayOutput() methods. It really makes your code brittle. I don't see a reason for them if you can avoid it.

Lastly, you can get an O(1) time solution for the nth Fibonacci number by Solving the Recurrence to get a formula.
Was This Post Helpful? 1
  • +
  • -

#3 theduck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 11

Re: Fibonacci Program method help

Posted 22 November 2011 - 01:54 PM

Okay, I'll try that. And yes, those methods are totally pointless. I had the program working perfectly fine with just a main method and then the fibCalc() method, but our professor is forcing us to have those other two methods.
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is online

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Fibonacci Program method help

Posted 22 November 2011 - 01:57 PM

Your error is because fibonacci and number3 are not declared in your main() method. So you cant use them in it. Your getInput() returns a number but you didnt assign it to anything. You have to take and it will be your fibonnaci
Was This Post Helpful? 1
  • +
  • -

#5 theduck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 11

Re: Fibonacci Program method help

Posted 22 November 2011 - 02:07 PM

Now I'm getting these errors.

arowlands_Fibonacci.java:23: <identifier> expected
answer = number3;
^
arowlands_Fibonacci.java:26: <identifier> expected
fibo = fibonacci;

I guess I just don't totally get what you mean by "<i>So you could pass that value (store it in a variable or pass the method call) to the fibCalc() method.</i>" Where would I even do that at or how? If you can't tell, I hate methods. I feel like they are absolutely pointless at times, especially in a program like this that isn't that complicated.


import java.util.Scanner;

public class arowlands_Fibonacci
{
        double answer;
        answer = number3;

        int fibo;
        fibo = fibonacci;
        public static void main ( String args[])
        {
                //Declare scanner and variable

                getInput();

                fibCalc(fibo);

                displayOutput(fibo, answer);
        }

        public static int getInput()
        {

                int fibonacci;
                Scanner input = new Scanner (System.in);

                System.out.println();

                System.out.print("Which Fibonacci number would you like? ");
                fibonacci = input.nextInt();

                while (fibonacci < 0 || fibonacci > 70)
                {
                        System.out.print("Which Fibonacci number would you like? ");
                        fibonacci = input.nextInt();
                }

                return fibonacci;
        }

        public void displayOutput(int fibo, double answer)
        {
                System.out.printf("Fibonacci #%d is %.0f\n\n", fibo, answer);
        }

        public static double fibCalc(int fibo)
        {
                //Declare variables

                //Initialize variables
                double number1 = 0;
                double number2 = 1;
                double number3 = 0;

                //For loop to calculate Fibonacci number
                for (int i = 2; i <= fibo; i++)
                {
                        number3 = number1 + number2;
                        number1 = number2;
                        number2 = number3;
                }
                //Return value
                return number3;
        }
}
                                          



Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,468
  • Joined: 27-December 08

Re: Fibonacci Program method help

Posted 22 November 2011 - 02:14 PM

Quote

The fibonacci variable is local to your getInput() method and cannot be accessed outside of it.

Remember about local variables. The number3 variable is local to fibCalc(). Your fibonacci variable is local to your getInput() method. You cannot access them outside their respective methods.

However, getInput() returns a value. So you can say in your main() method:
int input = getInput();
double fib = fibCalc(input);



Notice how I store the returned value from both of these methods. Also notice how I pass input as a parameter to the fibCalc() method.

Regarding methods, they are very useful and helpful in organizing your code. Your instructor wants you to misuse them with getInput() and displayOutput() methods in my opinion. However, a fibCalc() method is a good example of why you should use a method. It performs a modular and reusable task based on a piece of input.

Some supplemental reading:
Dogstopper's Variable scope tutorial
smohd's Passing objects to a method tutorial
xor-logic's Passing information between methods tutorial
NeoTifa's Basic Java for Beginners 3 Tutorial
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is online

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Fibonacci Program method help

Posted 22 November 2011 - 02:22 PM

You cant do that in a class scope. You have to do that in main(), getInput() returns fibonacci so assign the value to it, and fibCalc() return number3 so assign it to where you want. Something like:
 fibo = getInput(); //instead of just getInput() and so
This will make to do not need to make your variable in class scope, just get the returned values. because you only need them in main() those vaariables
Was This Post Helpful? 0
  • +
  • -

#8 theduck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 11

Re: Fibonacci Program method help

Posted 22 November 2011 - 02:35 PM

Okay, thank you. That helped a lot!

Now, I only have one problem. Everything compiled and it runs and does the proper math.

However, when it runs, it prompts the user twice. You have to enter the number you'd like twice. Like so:

Which Fibonacci number would you like? 5

Which Fibonacci number would you like? 5
Fibonacci #5 is 5

Any idea why?

 
import java.util.Scanner;

public class arowlands_Fibonacci
{
        public static void main ( String args[])
        {

                //Declare scanner and variable
                int userinput;
                double answer;

                getInput();

                userinput = getInput();
                fibCalc(userinput);

                answer = fibCalc(userinput);
                displayOutput(userinput, answer);
        }

        public static int getInput()
        {

                int fibonacci;
                Scanner input = new Scanner (System.in);

                System.out.println();

                System.out.print("Which Fibonacci number would you like? ");
                fibonacci = input.nextInt();

                while (fibonacci < 0 || fibonacci > 70)
                {
                        System.out.print("Which Fibonacci number would you like? ");
                        fibonacci = input.nextInt();
                }

                return fibonacci;
        }

        public static void displayOutput(int userinput, double answer)
        {
                System.out.printf("Fibonacci #%d is %.0f\n\n", userinput, answer);
        }

        

        public static double fibCalc(int userinput)
        {
                //Declare variables

                //Initialize variables
                double number1 = 0;
                double number2 = 1;
                double number3 = 0;

                //For loop to calculate Fibonacci number
                for (int i = 2; i <= userinput; i++)
                {
                        number3 = number1 + number2;
                        number1 = number2;
                        number2 = number3;
                }
                //Return value
                return number3;
        }
}


Was This Post Helpful? 0
  • +
  • -

#9 musiliu  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 110
  • Joined: 04-December 09

Re: Fibonacci Program method help

Posted 22 November 2011 - 02:40 PM

you don't need the extra getInput() call before the userinput = getInput() in main()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1