Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

MIT Library assignment

 
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
 
Marshal
Posts: 70384
283
  • 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??
 
Bartender
Posts: 4069
156
  • 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
Bartender
Posts: 4069
156
  • 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: 70384
283
  • 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 difficult to free fools from the chains they revere - Voltaire. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic