PROBEMS:

The Library class would include data members such as ID, Date of acquisition, description, number of copies, title and instance methods such addItem() and retrieveItem() also accessor and mutator methods are needed to accecss data members.

User arrays to store library holdings.

So here's my questions:
What do the instance methods in the problem means and how can make such method?
How can i store library holdings into an array? Can anybody give me an easiest example?
So far here's my code:

public class Library 
{
    private int id;
    private String date_acquisition;
    private String description;
    private int num_copies;
    private String title;

    public void addItem()
    {

    }

    public void retrieveItem()
    {

    }

    public void setId(int temp_id)
    {
        id = temp_id;
    }
    public int getId()
    {
        return id;
    }

    public void setDateAcquisition(String temp_date_acquisition)
    {
        date_acquisition = temp_date_acquisition;
    }
    public String getDateAcquisition()
    {
        return date_acquisition;
    }

    public void setDescription(String temp_description)
    {
        description = temp_description;
    }
    public String getDescription()
    {
        return description;
    }

    public void setNumberOfCopies(int temp_num_copies)
    {
        num_copies = temp_num_copies;
    }
    public int getNumberOfCopies()
    {
        return num_copies;
    }

    public void setTitle(String temp_title)
    {
        title = temp_title;
    }
    public String getTtle()
    {
        return title;
    }

}

Thanks in advance ;)

Recommended Answers

All 36 Replies

All of the methods in the code you posted are instance methods. The only kind of method that isn't an instance method is a static method and you can recognize that by the static keyword. In other words "instance method" just means ordinary method, so you don't need to worry about it.

I'm not sure exactly what you are supposed to do, but it doesn't make much sense to have setTitle as an instance method of a Library, since books have titles, not Libraries. It would make more sense to have something like this:

class Book {
    private int id;
    private String date_acquisition;
    private String description;
    private int num_copies;
    private String title;
    public Book(int id, String date_acquisition, String description, int num_copies, String title) {
        this.id = id;
        this.date_acquisition = date_acquisition;
        this.description = description;
        this.num_copies = num_copies;
        this.title = title;
    }
    // Also, put some methods to access and modify the data of the book here
}

Then your Library could have a Book[] array where books are stored. At least that is how libraries tend to work in real life, but from the instructions you quote I guess that's not what they intend. They do say title and number of copies and all of them are to be data members of the library, so I suppose there really are supposed to be just one of each of those values per library, but then I can't imagine what they mean for you to store in the array as the library holdings.

Perhaps they are just speaking loosely and you should use your own best judgement about how libraries are structured.

commented: Nice +0

Thanks for that info bguild.
Im confused too about the problem given.
BTW, here's the complete problem:

Develop an application that has to deal with Library Holdings, including Books, Magazines, and Multimedia Resources (This might be a program used by the Librarian to add newly acquired Library Holdings). The program could use a class name Library to represent all types of library resources.

The Library Class would include data members such as ID, Date of Acquisition, Description, number of copies, title and instance methods such addItem() and retrieveItem() also accessor and mutator methods are needed to accecss data members. These are variables and method common to all Library Holdings. The three subclasses of Library - Books, Magazines, and Multimedia - could then use to hold data members and methods specific to particular Library Holdings. The Books class member contains author, publisher and ISBN as its data members and accessor and mutator methods. The Magazines class member includes magazine edition and date issued as its data members, accessor and mutator for its methods. Lastly the class Multimedia may contain the subject and type for its data members together with a accessor and mutator. Use arrays to store Library Holdings. Provide an interface using swing components for your application.

Im confused on the Bold and Italicized portion of the problem.
Can somebody explain what will i do on that part?
Or give an easiest example maybe?

thanks in advance

BUMP BUMP BUMP

Please help me :(

commented: Please wait patiently? -3

The person who wrote this problem description obviously doesn't understand object oriented design, so that makes it harder for you. Judging from the variables, the class they call Library clearly does not represent a library, it represents one LibraryHolding - which is an abstract class (instance variables ID, Date of Acquisition, Description, number of copies, title) with non-abstract sub-classes Book, Magazine, and Multimedia ( singular nouns, not plural - each instance represents one book etc) that have extra instance variables. There is no class corresponding to a Library - that's just an array of LibraryHoldings.
The addItem and retriveItem methods would sensibly be instance methods of a class that represents a Library, but your teacher has confused that with LibraryItem.
In short - you need a Library class whose instances represents a whole library, and which has instance methods for addItem etc, and an array of LibraryHoldings. And a LibraryHolding class that represents a single item in the library, with variables like ID, title etc.

You really need to make a start on this now. If/when you get stuck post what you have done so far for further help.

Dear 'techxaidz', the problem is not very clear, what I understood is:
you have to make a class Library
then create classes: Book, Magazin etc ... that will inherite attributes and methods from the Library class. But the problem is that the relationship of the inheritence is "is"
For example: we have the following classes:
Circle, Rectangle, Square and Shape
Here we can use the inheritence:
Circle, Rectangle and Square will be subclass (will inherit) SHAPE
Because Circle is a Shape, Rectangle is a Shape etc ...
But Book is not a Library etc ...

Here's what I think you have to do:

Library.java

private Libarry[] items = new item[100]; //Array of size 100 that will contain books magazins etc ...
private int id;
private String title;
private int NumOfCopies;

private int counter = 0;

OTHER ATTRIBUTES
...
...

public void addItem(Library object){
    items[counter] = object;
    counter++;
}

....
....

LibraryHolding.java

main()
Library lib = new Library();
lib.add(new Book("CMPS",12 etc ....));

Book.java extends Library

public Book(String title,NumOfCopies etc...){
    super.setTitle(title);
    super.setNumOfCopies(NumOfCopies);
    etc...
}

Very Long Exercise but hope you got the concept, and hope this is what the prob wants.
I repeat, the problem is not professional and the relationship is not correct.

Using this code of CMPS:

public void addItem(Library object){
    items[counter] = object;
    counter++;
}

how can i retrieve the information in that object so that i can display it on a message dialog box per item?

it would be best that you either pick up a good textbook on java and go over the material, or look up online what you need to know to work with arrays:
This link right here 'll provide you with all you need.
but, just to give you a quick answer:

items[counter] = object;

with this line of code, you store your object into the array items, at the index counter.
for instance:

items[0] = object;

would store your object as the first element of the array, at index 0 (arrays are 0 based). if you want to get that object out of your array, just 'reverse' the code.

Library newObject = items[0];

this line declares a new Library object, and you assign it with the value stored as the first element of your array.

Thanks for the reply stultuske.

But i am having trouble in the retrieveItem() method.

public void addItem(Library object){
    items[counter] = object;
    counter++;
}




public void retrieveItem()
{
    //What will i put here to retrieve the informations (i.e Authors, title, bla bla bla)

}

you'll need to know the index of the element you want to get. when you know that, look at my previous post on how to get the object from the array.

The problem with your problem is that it is highly unintuitive. It's difficult to guess what things are supposed to do and I'm sure that makes it difficult for you as well as difficult for anyone trying to help. There is a serious question of what retrieveItem is supposed to do before you can even begin worrying about how to do it.

You have stored some number of libraries. It seems clear that retrieveItem should pick one out and present it somehow. Perhaps you should say public Library retrieveItem() so that retrieveItem() can return the Library, but earlier you said something about a dialog box, so that's another option. Once you've decided what "retrieve" means, you still need to decide how you pick the item. You could pick the first item, the last item, or any other item. Considering how unintuitive this problem is, it wouldn't surprise me if you decide to pick a random item using java.util.Random.

Yeah, actually that's my serious question. What retrieveItem() method supposed to do in the program. Since i can already call the getId(), getTitle(). . . etc, to put it on a Joption.message dialog and display them.

So i decided that retrieveItem() method maybe is used to get and display the information on a certain item added by the addItem() method. So any idea how can i retrieve the Information (i.e title, id, etc.) from the items[counter]?

Here's one way to interpret what that method should do...
1. prompt the user for some identifying info (eg item ID)
2. write a loop that accesses each item in the items array in turn, and tests each one to see if they have the ID you are looking for.
3. Once you have found the item in the array you can retrive all its other info and display it.

Here's one way to interpret what that method should do...
1. prompt the user for some identifying info (eg item ID)
2. write a loop that accesses each item in the items array in turn, and tests each one to see if they have the ID you are looking for.
3. Once you have found the item in the array you can retrive all its other info and display it.

Is it possible to do this without having a database? (newbie)

Is it possible to do this without having a database? (newbie)

Yes, just follow the 3 steps I described. That's all just ordinary Java.

Right now, your "database" is just the array of items. Maybe later you will be asked to create a "real" database to store all that info, but for now it sounds like you are just being asked to hold it in memeory in an array.

Ok, i think i'll start working on this. instead of having another -1 post

Dear techxaidz, retrieveItem() method:

public Library retrieveItem(int index){
    return items[index];
}

The retrieveItem(int index) method is used is such example:

Library lib = new Library();
lib.addItem(new Book(....));
lib.addItem(new Magazin(....));
lib.addItem(new Book(....));

System.out.print(lib.retrieveItem(0).getTitle());

Good Luck, and yes I think you have to try to start writing some code. Here we can give you Algorithm, Idea, Some Pseudocode and correct your code, but we cannot solve the Execise ;)

Dear JamesCherrill, I can't see why is retrieveItem is pointless! The items array is private and you cannot access it from other classes (Tester/Driver) so you have to create a retrieveItem() for the items array so you can get the elements.
Is there a way to get a certain element from the private items array ?
I though about it like the arrayList in which there is a function called get(), that will get a specific index from the private Data array.
Logically it makes sense when you use lib.retrieveItem(#); It will return an Item of type Library that will be treated as Book or Magazin depending on what the real type is.

Hi cmps
I haven't seen any relevant code from techxaidz, so I have no idea whether his array is private or not. I know you suggested that, but...
More seriously, he's struggling with which item to get. Showing him how to get an item from its index number really doesn't help. For this to make any sense he needs to retrieve items by ID or title or somesuch, which is the direction I was trying to point him in. Although you can think of this like an ArrayList, I believe it would be more relevant to think of it like a Map<String><Library> where the key is ID or Title.
Having said all that, I'm sure we both agree that this is one of the worst designed and specified exercises ever, and OP deserves all the help he can get.

Yes 100%

Having said all that, I'm sure we both agree that this is one of the worst designed and specified exercises ever, and OP deserves all the help he can get.

It could be an idea to retrieve an Item using ID but I don't know why my idea deserved a -1 :o
Let me explain more why I suggested the int index as parameter:
Let's say he added 100 item (Books and Magazins), he can now do a for loop that will iterat on all the items and do whatever task he wants to do.
Example:
for(int i=0; i < 100; i++)
System.out.println(lib.retrieveItem(i).getTitle());

Just by having the index as parameter, he can now do lot of things like for example count how many Books or Magazins are available in the lib, Display only items with ID > 2000 etc ...

Hope you understood my point. Regards

We shouldn't hijack OP's post any more now we have both made our points. I've withdrawn the downvote to help put this to bed.
regards
J

Any comment on this code:

public void addItem(int ids,String date,String desc,int num,String titles)
            {
                for(int h =0;h<=4;h++)
                {
                        items[h] = new Library();
                }   


                if(xtr<=5)
                {   
                    items[xtr].setId(ids);
                    items[xtr].setDateAcquisition(date);
                    items[xtr].setDescription(desc);
                    items[xtr].setNumberOfCopies(num);
                    items[xtr].setTitle(titles);    
                    xtr++;

                }


            }

        public void retrieveItem(int id)
                {
                    int z;
                    for(z=0;z<=4;z++)
                    {
                    if(id==items[z].getId())
                        {
                        JOptionPane.showMessageDialog(null, "ID number: " + items[z].getId() +
                                            "\nDate of Acquisition: " + items[z].getDateAcquisition() +
                                            "\nDescription: "+ items[z].getDescription() +
                                            "\nNumber of Copies: " + items[z].getNumberOfCopies() +
                                            "\nTitle: " + items[z].getTitle()); 


                        }

                    }

retrieveItem looks OK, except you have hard-coded 4 as the number of items. You really need a variable that tells you how many items there currently are in the array.
You may want to add some code to check if you went all through the array without finding the id you are looking for - in which ase you could issue an "id not found" error message to the user.

addItem is a bit more of a question... you are adding 1 item, so why add 4 entries to the array? And what is xtr?

It looks like you are wiping out your entire items array every time you add an item, so you will only have one item at a time. It's hard to tell if that is intentional.

You are using the numbers 4 and 5 as constants, but there is no comment to indicate where those numbers come from. Why is 5 being used instead of 6 and why not 3 where you have 4? It is good practice to declare constant fields so you can give these numbers names and make it clear what each one means. If one of them is the length of items then you should use items.length instead.

Comments noted!

@James - there's a code on top of it and i already declared the array. but i didn't put it there because it's to long and it will occupy a lot of space. i "hard-coded 4 as the number of items because i am only testing an array of 5, so from 0-4 is my range. Thanks for the idea you given about the checking, ill add that. on the otherhand, what 4 entries are you talking about? xtr is this -> private int xtr = 0; used as a counter.

@bguild - What do you mean by wiping out entire items? I forgot to include that my array is 5, so the range of the ints are ranging from 0-4. Sorry for that. Thanks for the comments by the way, ill note every mistakes i committed. you're right, maybe i can change it to items.length.

bguild and I are both worried about lines 3-6 in that code. Every time you call addItem you overwrite all five items in the array, thus deleting whatever you added before.

items.length is always better than hard-coding the array size, but in this case you probably should not be doing either. Your array shoud be "big enough" - maybe 100 elements for now, and initially all those elements will be null. As you add new items you will replace those nulls with real items, starting at items[0], then items[1] etc, and keep a counter of how many you have added. Your counter tells you which is the next available element for adding a new item.
Then when you look through the array you use the counter to tell you how many elements to loop through. You don't want to use the complete array size, because then you will be accessing all those null entries that you haven't used yet.

@james - ops! i almost forgot, yea you're right! but how can i instantiate the array items?

" instantiate the array items" doesn't really make sense in Java-speak.
All you need to do is to create/instatiate a new item then put it in the array at the next avaiable slot. It's what you already do on line 5 above, except you need to use your counter to identify the appropriate array index.

So what is my mistake on the line 3-6?

personally, I would have trouble reading the class's descriptions.

your first method 's name is 'addItem', and you pass the information for (I guess) one item, yet you add about 4 items.

your second method is called 'retrieveItem', but you don't retrieve anything....

what I would do:

public class MyClass{
  private Library[] libraries;

  public MyClass(){
    libraries = new Library[5]; // default size
  }

  public MyClass(int newSize){
    libraries = new Library[newSize];
  }

  public void addItem(Library libToAdd){
    int loc = -1;
    for ( int i = 0; i < libraries.length; i++){
      // assuming we don't remove any items, or that we want the item to be entered
      // in the first empty spot (null)
      if ( libraries[i] == null ){
        loc = i;
        break;
      }
    }
    if ( loc == - 1){
       // array is full, won't be added
    }
    else{
      libraries[loc] = libToAdd;
    }
  }

  public Library retrieveItem(int index){
    if ( index < 0 || index >= libraries.length )
      return null;
    // simple solution to prevent arrayIndexOutOfBoundsException
    return libraries[index];
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.