aspose file tools*
The moose likes Beginning Java and the fly likes please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please help" Watch "please help" New topic
Author

please help

Jake Spivey
Greenhorn

Joined: Mar 14, 2012
Posts: 4
I have to create a FibonacciGenerator test program that calls the nextNumber method using a loop until the correct fibonacci number is returned. Output will be the fibonacci number for n, so if the user inputs 4, then the output will be fib(4)=3.

Here is the FibonacciGenerator code that was provided:



Here is the test program that I created:



Here are the errors I'm getting:

FibonacciGeneratorTest.java:22: <identifier> expected
System.out.print("fib(" + fib + ")=" + n);
^
FibonacciGeneratorTest.java:22: illegal start of type
System.out.print("fib(" + fib + ")=" + n);
^
FibonacciGeneratorTest.java:22: ')' expected
System.out.print("fib(" + fib + ")=" + n);
^
FibonacciGeneratorTest.java:22: ';' expected
System.out.print("fib(" + fib + ")=" + n);
^
FibonacciGeneratorTest.java:22: illegal start of type
System.out.print("fib(" + fib + ")=" + n);
^
FibonacciGeneratorTest.java:22: <identifier> expected
System.out.print("fib(" + fib + ")=" + n);
^
FibonacciGeneratorTest.java:22: ';' expected
System.out.print("fib(" + fib + ")=" + n);
^
FibonacciGeneratorTest.java:22: <identifier> expected
System.out.print("fib(" + fib + ")=" + n);
^
8 errors

When I try to resolve these errors, new ones are created. Can someone please explain or show what I'm doing wrong? Thanks.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi stressed student,

Welcome to CodeRanch!

Please use real name.

Coming to your FibonacciGeneratorTest class, I can see there is a major syntax error at line 21 (hint : in which method this print statement should be? and where exactly it is?)

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
You have a serious design fault that you are mixing static and instance members in that class. I think you are making things static because they seem to work, rather than because you really want them static.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1038
    
  13

This is a good example of why you should always use braces around the code in if/else statements, even if there is only one line to be executed. If you go back and do that, I think you'll see immediately where the problem is.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Jake Spivey
Greenhorn

Joined: Mar 14, 2012
Posts: 4
Thanks for all of the help! I made a couple of adjustments and now when I run the program with user input of 4, the program returns fib(4)=FibonacciGenerator@3e86d0 instead of fib(4)=3. Here's my new code:



Any other suggestions?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

Jake Spivey wrote:... when I run the program with user input of 4, the program returns fib(4)=FibonacciGenerator@3e86d0 instead of fib(4)=3.

What you're doing in line 12 is printing the FibonacciGenerator object that you created in line 11. This will call the toString() method on class FibonacciGenerator. You haven't defined a toString() method in that class, so it falls back to the toString() method of class Object, which returns "FibonacciGenerator@3e86d0".

Note that you are not at all calling the method fib defined in lines 15 - 26.

You probably meant this:

But then you're still not using the FibonacciGenerator object you're creating in line 11. You could remove line 11.

Your code doesn't test class FibonacciGenerator at all.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Give your fields private access. All of them, except constants.
Get rid of those dreadful combined return and assignment statements. You can reduce your fib method to one line with the ?: operator. Then you will only use return once. You do not need the field fib, so you could get rid of it. Also you are causing no end of confusion to yourself by having three things all called fib between lines 5 and 15.
If you go through this post about static methods, and the older posts referred to there, particularly this one, you find the fib() method probably ought to be static.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22


Looking at your code again, I see that you have three different things that are called "fib", which makes the code very confusing.

  • In line 5 you have a member variable called "fib" which is an int.
  • In line 11 you have a local variable called "fib" which is a FibonacciGenerator.
  • In line 15 you have a method called "fib".

  • In lines 19 and 23 you're doing something very strange. In one line, you assign a value to the member variable "fib" (which was declared in line 5) and you're returning its value from the method. Don't do that, as Campbell already said.

    The local variable "fib" in line 11 hides the member variable. When you print "fib" in line 12, you're printing the value of the local variable, not the value of the member variable.

    Some tips to fix this:

  • You don't need the member variable. Remove line 5.
  • Also remove the assignments in lines 19 and 23.
  • Don't give different things the same name, because it is confusing.
  • If the idea of this class is to test the code in class FibonacciGenerator, then you'll need to do some more rewriting. Right now it's not testing anything in class FibonacciGenerator at all.

  • Jake Spivey
    Greenhorn

    Joined: Mar 14, 2012
    Posts: 4
    Again, thanks for all of the advice. I have went in and made some drastic changes, but still cannot get my test program to run. Here is the new code:



    Any other ideas? And thanks again. This is the best help forum ever!
    Anayonkar Shivalkar
    Bartender

    Joined: Dec 08, 2010
    Posts: 1509
        
        5

    Jake Spivey wrote:but still cannot get my test program to run

    1) Please TellTheDetails. Is it compilation error? Runtime exception? A bug?
    2) Where is your main method?
    3) In class FibonacciGeneratorTest, I can see the code which does not belong to any method. Why is that?
    4) What is fib.nextNumber? nextNumber is method of FibonacciGenerator class. Now I guess I can be pretty sure that it is compile time error.

    I would suggest that before thinking the logic of Fibonacci series, please pick a descent Java book and try to do some hands on coding. Also, first, try to get the code working with simple console, after which you can go with Swing.

    I hope this helps.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39784
        
      28
    You are making life difficult for yourself by trying too much at once. Let’s reduce your classes a bit.That makes it nice and simple. I have taken your code, put in a trap to prevent 0 or negative arguments, and left you to fill in the rest. There is all sorts of code in your latest posting which will simply not work, so you might as well delete the lot and start again.
    Note there are more efficient ways to calculate a Fibonacci number than the one-line technique. But get this version working first.
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14338
        
      22

    There are still a number of things wrong with this.

    Your program still has two things named "fib":
  • A member variable in line 5 which is an int
  • A local variable in line 13 which is a FibonacciGenerator

  • You don't need the member variable. Get rid of line 5.

    Line 9 has an error. That's not a valid method declaration line. It shouldn't end with a ; and there should be () after the method name. Why does the method return an int?

    Line 17 has an error. You're trying to call the method nextNumber() on n, but that doesn't work, because n is an int. You cannot call a method on an int.

    Line 32 has an error. What is fib.nextNumber?

    Why do you have return statements in line 22 and 27? If you return from the method, line 32 will never be reached.
    Jake Spivey
    Greenhorn

    Joined: Mar 14, 2012
    Posts: 4
    I am extremely new to this, so I understand that helping me has not been easy. I really do appreciate all of the feedback. Based off of instructor feedback and feedback from this forum, here is what I have come up with:



    My instructor states that he wants a while loop that calls the nextNumber method from the original class the appropriate number of times to print the correct fibonnaci number that correlates with the number input by the user. For instance, if the user inputs 4, the program should print "fib(4)=3. Mine prints "fib(4)=1." I really don't know where to go from here...
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14338
        
      22

    In line 8 you're declaring a variable n that gets the number that the user entered. But you are not using that number anywhere. To get your FibonacciGenerator class to do something with the number, you have to pass it to it somehow.

    Also, the while (n > 0) { ...; break; } is a bit strange. Just use an if-statement:

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: please help