• Post Reply Bookmark Topic Watch Topic
  • New Topic

MIT Library assignment  RSS feed

 
Sebastien Manic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone! I'm learning Java right now and i'm trying to do the MIT assignment about books and libraries. So i read the previous post about it and apply it. I can't seem to make it work.


(i cleaned out for presentation)
and i have a result for printAvailableBooks():
Books available in the first library:
The Lord of the Rings
The Lord of the Rings
The Lord of the Rings
The Lord of the Rings

What i understand is : The last book created is put in all the 'boxes' of the array "Collection", as far as index. In the previous post, this response was given and seems to work :

Am I not doing the same thing? What is wrong?
Thanks for help
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I can't see what is going wrong. You will have problems if you try a null book or try to add a 5th book, but I can't see that you are doing that. Please post the whole code so we can try it out.
 
Sebastien Manic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. So these are the two classes i use :

Book.java


and Library.java



Narrowing the problem i found that using a different type (i.e. String instead of Book) works fine. But i don't know why??
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sebastien,

do you know what it means when a member variable is declared static?

Have a look at how you declare the members 'title' and 'borrowed'.

Greetz,
Piet
 
Sebastien Manic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ho thanks Piet !
So i changed to non-static and voilà ! So in fact, the addBook method worked fine but when creating a Book object, the getTitle method would always return the String corresponding to the title of the last Book created, right ? (because the variable title is independent of the instance).
Well i feel a bit ashamed by my problem-solving skills :/
Thanks a lot !
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!

And kudos to Campbell for asking for the full code. The problem was
indeed not in the code in the opening post.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you said static, I started wondering whether the title was shown as static, and yes, that was the problem. Well done
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!