• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

book and library class

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created a book class which is to be implemented in a library class. My current problem is getting the method getTitle to print the title of the book.
When testing this method I am getting null instead of the name of the book. here is code.
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that a constructor at the top of the class? I didn't notice. What I did notice however is that you have got the assignment the wrong way round. Remember that value goes from right to left.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also make all your fields private. Work out what it means if you make a field static. Hint: I think static is a mistake unless yo uhave a good explanation for it.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jordan King wrote:My current problem is getting the method getTitle to print the title of the book.
When testing this method I am getting null instead of the name of the book. here is code.



Well, your getTitle() method really shouldn't be printing the title of the book. It should be returning the title of the book. Other code may wish to print the book's title, but it isn't the responsibility of a Book object to print anything.

However you do have a problem and it's in the setTitle() method. Your assignment in that method is backwards, look at which value you are assigning to which variable.
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 3 & 4 are fields, 8 & 9 is constructor
the problem was my assignment thanks
and I changed getTitle to just return the title.
Thanks
now I am trying to create my addBook method in my Library class.
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am wondering if I need to create a stack or if there is an easier method to adding a book.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you choose to have your Library class store its Books in an array, then you should at least start out by creating an array for it to use. As it is you have only a Book[] variable which is null because it doesn't yet refer to an array object.

But naturally you're asking "How big should the array be, then?" And after a while you'll be asking "How do I know how much of the array is full?" So your question about whether you should use something else is a good question.

First of all if your assignment says you should use an array, then you should use an array. The question of "what else" doesn't arise. Otherwise you're better off using some kind of List (an ArrayList would be reasonable) and then you don't have to worry about how big it is or how full it is. So: Are you required to use an array? Or if you aren't, have you come to List in your studies yet?
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I have for my addBook method


this is the error code I am getting
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at libraryandbook.Library.addBook(Library.java:19)
at libraryandbook.Library.main(Library.java:27)
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by the way Paul I was lead to use an array by my assignment notes but it is for ocw and I am interested in other ways as well.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you've got an empty array and therefore when you try to put something into it, there's no place for it. I believe I already discussed questions you might be asking when you use an array...
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I set the size of the array now. I have read up on literature on arrays and I am curious if having a method to add books into my array would work and if so how because I haven't seen any way to continually add new elements into an array. I suppose arrays are defined at creation but does that necessarily mean that its contents have to be defined at that point. if not I am looking for pointers on how to create a method that places each "book" into a new slot in my array each time the method is called. I am also curious if the elements rotate within an array upon placement of new elements(my intuition leads me to believe that this is not the case) but I am just sort of trying to figure this out. If anyone else out there would like to shine some insight on this it would be much appreciated.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, arrays aren't nearly that smart. When you create an array with (say) 8 entries, all you get is a container which has 8 places to put things. That's all. If you want to keep track of which of those places you've actually used, that's up to you. If you want to do something special when you've used all of those places, it's up to you to write the code to do that. If you want an exception to be thrown when you're asked to put thing number 9 into the array, that's up to you too.
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
right Paul I get that. I figured it out. I defined a class level variable that I used as an index into the array. It's value was zero and it increments by one every time a value is added.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect!

(Actually, not quite perfect because you still have to deal with what happens when the array fills up. But forgive me for griping.)
 
reply
    Bookmark Topic Watch Topic
  • New Topic