• Post Reply Bookmark Topic Watch Topic
  • New Topic

An array of book objects  RSS feed

 
Blake Unnerstall
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am a college student that is completely new to programming and I'm having some trouble with an assignment. These are the instructions:


Create a Book object. This object should contain the following variables bookName, bookAuthorName, bookGenre.

Now create 3 different book objects and store them in an array.

Finally iterate through this array and print out the book names.


I've been playing around with some code but I'm not sure if I'm on the right path. I'm not sure if I'm populating the array correctly and I have no clue how i'd go about iterating through it. I've been trying to teach myself for the past couple hours but I'm having trouble understanding. If anyone could give me some help I'd greatly appreciate it.

 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

After defining the size of the array that gives it a number of empty(null) slots, you need to create a Book object for each slot and use an assignment statement to assign the reference to the new Book to a slot in the array.  Or it can be done in one statement:
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Blake, welcome to the Ranch!

Short answer: No.

Have a look at this code for example:



Looks to me like bookName, bookAuthorName, and bookGenre should be attributes of a Book object. They shouldn't be Book objects themselves.

Imagine having "Moby Dick" being one book, "Herman Melville" as another book, and "Weird 19th Century Stuff" as a third book. Not on the right track, is it?

So let's go back to the instructions:

Create a Book object. This object should contain the following variables bookName, bookAuthorName, bookGenre.


You haven't done that yet. Unfortunately the instructions are a bit dodgy -- you should start by creating a Book class. Have you seen examples of creating a class yet? You should have, or else your lessons are being given in the wrong order. A class is a text file which starts like



Does that look familiar? After that, you need to declare some variables and put in a constructor which initializes those variables, etc, etc, etc. If that doesn't ring any bells then I think you need to go back to your instructor and ask about what you've missed.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error messages do you get when you try to compile the code you posted?
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a Book object class. This object should contain the following variables bookName, bookAuthorName, bookGenre.

I would suggest starting over. Implement just this much and post it back here.

Note that you are currently declaring, for example, bookName to be static. "static" means there will only be one regardless of how many instances of Book you have. Remove the word "static" and then you'll have a bookName for each new Book you create.

(edit) "Create a Book class." is how it should have been specified.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Carey, throw away that code you posted and start over. I don't know what you've gone over in class but the lessons you need to complete this assignment can be found in the official Java Tutorial on Classes: https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

The three "variables" you were instructed to create are also called "fields" so keep that in mind when you go through the tutorial.

There's also a tutorial on Arrays: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

and a tutorial on the for-loop, which you can use to go through an array: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Unfortunately the instructions are a bit dodgy

"Dodgy" is a bit generous, I think.

The instructions should have been more like this:

Define a Book class that has three String fields: title, author, and genre.

Define a constructor for Book that takes the new Book's title, author, and genre as its arguments. The constructor should save the values passed to it in the appropriate fields.

Define appropriate accessor methods (getters) for each of the fields.

Create an array and make it store three of your favorite books, each one represented as a new Book object.

Finally, iterate through the array and print out the genre, title, and author of each book, in that order, one book per line.
 
Blake Unnerstall
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instructions definitely confused me and I kinda regret not asking my professor for more details sooner, but anyway I restarted and listened to all of your advice and have now come up with this:



So I made the Book class with the variables and the book constructor. Would my next step be to create the array? If so, is that first line in the populateArray method the correct starting point?
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's good. It's not great but it's a small step forward.

Your instructions were specific about how you should name those fields. However, you should be aware that those specifications make your names redundant. The fields are already defined in a Book, so what's the point of having that constant, annoying reminder that the name is referred to as bookName and the author is referred to as the bookAuthor and the genre is referred to as the bookGenre.  Pull out your driver's license, if you have one. Does it say Driver's Name, Driver's Address, Driver's Height, Driver's Weight, Driver's Date of Birth, Driver's Picture, Driver's whatever else on there? It doesn't, right? It just says: Name, Address, Height, Weight, Date of Birth. Since all that information is on your Driver's license it's understood that they all pertain to you, the driver whose license that is, right? Why should your class definition be any different?

If you have a class with a bunch of fields named with that unnecessary redundant label, fix them.  If you find that your fields have more than one "owner of the field" type label, then each set of fields with their "owner" label should probably be in a separate class by themselves with the class name being that "owner" label.

So, instead of repeating yourself and naming those fields bookWhatever, you should just use title, author, and genre for the field names as I suggested in my version of the instructions. However, since you might get docked points for not following those, IMO, flawed instructions, just go ahead with the names you were instructed to use anyway for your submission.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now let's look at this other part you wrote:
Blake Unnerstall wrote:

The instructions say: "create an array".  Fine, on line 16, you created a three-element array of Book objects.  However, the method that this line of code is in is called populateArray(). Is that what the code inside this method is doing?  If someone instructs you to start your car, would you be following those instructions if you got into your car and just sit there? No, right? So what do you need to do to actually populate the array?

Also, did you learn about what the static main() method does? Will anything actually happen if you try to run a Java program that doesn't have a main() method?
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the constructor parameter names:

Try to use more expressive names. Those parameter names are cryptic. The idiomatic way to write that in Java would be (using the fields names I suggested):

The things on left side of the assignment statements refer to the current object's (this) fields. The right side refers to the constructor parameter.

Also, you would declare the fields as private so that only code in the Book class have direct access to them. Anything outside the book class would have to go through accessor and mutator methods (getters/setters).
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Now let's look at this other part you wrote:
Blake Unnerstall wrote:

The instructions say: "create an array".  Fine, on line 16, you created a three-element array of Book objects.  However, the method that this line of code is in is called populateArray(). Is that what the code inside this method is doing?  If someone instructs you to start your car, would you be following those instructions if you got into your car and just sit there? No, right? So what do you need to do to actually populate the array?

Also, did you learn about what the static main() method does? Will anything actually happen if you try to run a Java program that doesn't have a main() method?

Another issue I have with this code is that it is declaring bookArray local to the method. That means that unless you return bookArray from the method or assign it to a class member variable, it will disappear when the method returns.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!