• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fibonacci number  RSS feed

 
Kacper Szmigiel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi nerd, geek and outsiders I'm on about 330 page of Head First Java. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. So it goes like this: 0, 1, 1, 2, 3, 5, 8, 13, 21... And I've got a problem. Big problem. Everything is compiling, but when I'm tring to run it, I get a long, long error. Like this: http://www.bankfotek.pl/image/1328623.jpeg

What's wrong? Here is the code http://pastebin.com/XNmRqVBV

Have fun looking for defect

PS. Sorry for polish names of class and methods.
 
Paul Clapham
Sheriff
Posts: 22836
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We would appreciate seeing the errors, so we can help you. Please read our FAQ article PostTextNotScreenshots (<== click on that link).
 
Kacper Szmigiel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go.

C:\Documents and Settings\Kacper>java glowna
Exception in thread "main" java.lang.NoClassDefFoundError: petla
at przygotowanie.przygotuj(przygotowanie.java:14)
at glowna.main(glowna.java:4)
Caused by: java.lang.ClassNotFoundException: petla
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)






 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is a messy code...

Are all the classes in the same package?
Note that I'm not paying attention to the logic of this code, or what it's supposed to do... Just to your exception message.
 
Kacper Szmigiel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 class = 1 *.java file
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what's that name of yours, couldn't you read this?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And again - are they all in the same package?
If they are, are you sure you have .class (compiled) files of other classes you use (in this case, you're missing petla)?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uggh...it doesn't do much good to use code tags if your code isn't properly formatted. what's with the no indentation?

is your CLASSPATH set to something, and if so, what?

Did all these files compile cleanly? Where are they all located (same folder, different folders, etc)?
 
Kacper Szmigiel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh... right. I was missing petla.class I've compilled petla.java and this is what I've got by running glowna:

C:\Documents and Settings\Kacper>java glowna
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size:
3
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at petla.petla1(petla.java:9)
at przygotowanie.przygotuj(przygotowanie.java:15)
at glowna.main(glowna.java:4)
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lista1 contains 3 values (indexed 0, 1, 2), but you're trying to access value at index 3.
 
Kacper Szmigiel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I've make it like this and error is the same ;_;
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it really a goal to calculate a Fibonacci number here? Because you can do it in couple of lines of code that are pretty straightforward.
 
Kacper Szmigiel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to do it with my own knowledge, I know that is pointless. Just want to do it. Steve Wozniak was starting in this way
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kacper Szmigiel wrote:I want to do it with my own knowledge, I know that is pointless.

No it isn't. It's precisely how most of us did it.

Just want to do it. Steve Wozniak was starting in this way

Don't worry about who did it; just DO it. And what I'd suggest you do first is get out a pencil and paper, and read through your program.
  • What does it do?
  • What do you want it to do?
  • What's the difference?

  • Programming is thinking, NOT coding.

    Winston
     
    Paul Clapham
    Sheriff
    Posts: 22836
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay, then carry on. Kemal already told you what the error message means. If it continues to occur then you're going to have to change your code somehow.
     
    Kacper Szmigiel
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yeah! I got it! It was all about that calling of method petla(.....) was giving wrong arguments (2 and 3), and the cells collected form the table were wrong. The correct answer was giving arguments like 1 and 2, whitch were writed in int f and int r. Thank you all for time
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    > PS. Sorry for polish names of class and methods.

    The Polish Notation is always welcome ;-)
     
    Paul Clapham
    Sheriff
    Posts: 22836
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kacper Szmigiel wrote:PS. Sorry for polish names of class and methods.


    At least those were meaningful names. What you should really be sorry for is the names like "a" and "v" which mean nothing and make it harder to understand the code. So for example you could assign them the wrong values and not notice you had done that.
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    > names like "a" and "v"

    So short names for variable of small scope can be OK for me like "k" for key and "v" for value.
    Who knows what "a" and "v" stand for in Polish?
     
    Kacper Szmigiel
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ivan Jozsef Balazs wrote:
    The Polish Notation is always welcome ;-)


    Not in every corner of the internet, trust me

    Here is final, compiled version of my program. Unzip it and type: java directory/glowna

    http://speedy.sh/MxXxK/Fibonacci-numbers.rar

    And here you have code:

    http://speedy.sh/QEWM5/Fiboncci-numbers-code.rar

    EDIT.

    >Who knows what "a" and "v" stand for in Polish?

    Nothing
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kacper Szmigiel wrote:

    >Who knows what "a" and "v" stand for in Polish?

    Nothing


    I am sure one could find a plethora of Polish words beginning with "a" or "v" ...

    a = and ?
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't know about the others, but I always name classes, methods, variables, ..., in English, even though it's not my native language. I think it's a good practice, since you can't know in advance who might need to take a look at your code, are you going to publish something online, etc. If necessary, I put documentation in Serbian (my native) but the naming is always in English.
     
    Paul Clapham
    Sheriff
    Posts: 22836
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Would you say that the fact that you write Serbian in the Cyrillic character set makes it more important for you to do that? Seems to me that calling a method "getИме" would be much more inconvenient than calling it "getName", for example, just because it's harder to type. Whereas Polish doesn't have that problem.
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kemal Sokolovic wrote:I don't know about the others, but I always name classes, methods, variables, ..., in English, even though it's not my native language.


    Actually it is the same for me.
    A sidenote: AFAIK Serbian is written either with Cyrillic or with Latin letters. My mother tongue, Hungarian, is written with Latin letters. But there are many accented letters beyond the English alphabet: áéíóúöőüű
    For example: árvíztűrő üvegfúró ("floodresistant glassdrill").

    Anyway, if someone started programming earlier, in good old Cobol, Fortran or C days, one had no choice to use but ascii characters in the names. This was a kind of invitation to use English variable and function names. I still practice this even though in Java accented letters are allowed in the names.
     
    Kacper Szmigiel
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ivan Jozsef Balazs wrote:Anyway, if someone started programming earlier, in good old Cobol, Fortran or C days


    What the hell? Are you 60 years old?
     
    Paul Clapham
    Sheriff
    Posts: 22836
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kacper Szmigiel wrote:
    Ivan Jozsef Balazs wrote:Anyway, if someone started programming earlier, in good old Cobol, Fortran or C days


    What the hell? Are you 60 years old?


    Some of us are, you know. Don't be so surprised.
     
    Kacper Szmigiel
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow, I'm impressed. It's not often to found programmers that old, even in internet
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:
    Kacper Szmigiel wrote:
    Ivan Jozsef Balazs wrote:Anyway, if someone started programming earlier, in good old Cobol, Fortran or C days


    What the hell? Are you 60 years old?


    Some of us are, you know. Don't be so surprised.



    Why do you have to be old to code in those languages? For example, I coded in C, well, last week. And I have a friend who coded in Cobol only 5 years ago -- okay, to be more exact, it was CICS code with portions in Cobol.

    Henry
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:Would you say that the fact that you write Serbian in the Cyrillic character set makes it more important for you to do that? Seems to me that calling a method "getИме" would be much more inconvenient than calling it "getName", for example, just because it's harder to type. Whereas Polish doesn't have that problem.


    As Ivan Jozsef Balazs said, Serbian can be written in both Cyrillic and Latin (yeah, we are the only one in Europe that can do that :)), so you can write getIme instead of getName, or getIznos instead of getAmount. But I already stated why I practice to use English for naming in my code.

    Henry Wong wrote:Why do you have to be old to code in those languages? For example, I coded in C, well, last week. And I have a friend who coded in Cobol only 5 years ago -- okay, to be more exact, it was CICS code with portions in Cobol.


    I agree with that, I've done C/C++ for 2 years at University, even though one might think these are outdated.

    Paul Clapham wrote:Some of us are, you know. Don't be so surprised.


    So, it is possible to survive that long all that fast food, Coke and all night debugging? :)
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If anybody tells you C and C++ are outdated, send them to this link.
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I avoid participating in such discussions anymore, because it is usually started by those without arguments to support their claim. They've just heard it somewhere else, found out that C appeared in the 70's and that's it - they claim it's outdated. Those claims remind me of ones I hear in discussions C# vs. Java, or Microsoft vs. Apple, again with not many arguments to support one or the other side. It's just funny to watch/listen it and not get involved.
     
    Paul Clapham
    Sheriff
    Posts: 22836
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kemal Sokolovic wrote:
    Paul Clapham wrote:Some of us are, you know. Don't be so surprised.


    So, it is possible to survive that long all that fast food, Coke and all night debugging? :)


    We don't do any of that where I work. We have a well-run department and 8 hours per day gets the job done.
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:
    Kemal Sokolovic wrote:
    Paul Clapham wrote:Some of us are, you know. Don't be so surprised.

    So, it is possible to survive that long all that fast food, Coke and all night debugging? :)

    We don't do any of that where I work. We have a well-run department and 8 hours per day gets the job done.


    Good to see that the sarcasm is still well received... :-)
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kacper Szmigiel wrote:
    What the hell? Are you 60 years old?


    No, "only" between 50 and 55. There *is* live (to some extent) beyond 50.
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kemal Sokolovic wrote:They've just heard it somewhere else, found out that C appeared in the 70's and that's it - they claim it's outdated.


    My writing "good old Cobol, Fortran or C days" was not intended to suggest these languages' being outdated. But their relative importance diminished and these days now are rather Java than, say, Cobol ones.

    I have an ex-class-mate who still does Cobol and PL-I. When I started, it was *the* programming language (and environment if we consider things like index-sequential files) supported on many systems. And when there was a porting project to Unix with C (and Cobol too), it was a revelation for me to meet C (and Unix by the way) which I got to like very much and also C's descendants (C++, Java). But all this does not imply these languages are obsolete or gone.


     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!