Results 1 to 17 of 17
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Classes Help!!! two classes with object question.

    Hi,

    I'm very confused about this one thing. Lets say for example we have to make 2 classes, one for Books and the second for Library.

    The constructor for books is: public books(String Name, String Author)
    and the methods are just getName() and getAuthor().

    Now for the Library class it has to contain a list of books with the method addBook().

    I have no idea how to do this as I'm not sure what to make the instance variable, do i make it private ArrayList <String> list; or
    private ArrayList <books> list;

    and I know in arrayists we can use list.add() to add the books but in this case I'm confused and don't understand how to get the name and author from the books class into the list in the library.

    Could anyone please clear this up, it would be very much appreciated.
    Thanks for your time.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Classes Help!!! two classes with object question.

    If you want the ArrayList in the Library class to contain Book objects, then use this one:
    Java Code:
      private ArrayList<Book> list;     // Define a list to contain Book objects
    how to get the name and author from the books class into the list in the library.
    Why do you want to copy the data from the Book class into another list?
    You should be adding the Book objects to the list.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Classes Help!!! two classes with object question.

    The class should be called "Book" -- it begins with a capital letter and is singular as it represents a "blueprint" for one single book.

    The ArrayList will hold Book objects and so should be declared ArrayList<Book> since the class name for the type held by the collection goes in the angle brackets <...>.

  4. #4
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Re: Classes Help!!! two classes with object question.

    Quote Originally Posted by Norm View Post
    If you want the ArrayList in the Library class to contain Book objects, then use this one:
    Java Code:
      private ArrayList<Book> list;     // Define a list to contain Book objects
    Why do you want to copy the data from the Book class into another list?
    You should be adding the Book objects to the list.
    Ye that's what I meant. Ok so my instance variable is private ArrayList<Book> list;

    So what would my constructor be?

  5. #5
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Re: Classes Help!!! two classes with object question.

    can anyone tell me what the constructor of the library class should be please

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Classes Help!!! two classes with object question.

    You need to define how the Library class is going to be used. What arguments need to be passed to it when it is created?
    Not all classes need arguments when try are created.
    What does the constructor need to initialize when the class is created?

  7. #7
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Re: Classes Help!!! two classes with object question.

    Quote Originally Posted by Norm View Post
    You need to define how the Library class is going to be used. What arguments need to be passed to it when it is created?
    Not all classes need arguments when try are created.
    What does the constructor need to initialize when the class is created?

    Well the library class basically just fills the list with the books which can be accessed later on (not important right now)
    So initially the library class is empty as no books have been added to it yet. So what will I have to do for my constructor?

    So far I've been working with primitive values so I could set the int's to 0 and Strings to "". But I'm not sure what to do in the constructor for this one.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Classes Help!!! two classes with object question.

    the library class is empty as no books have been added to it yet. So what will I have to do for my constructor?
    Sounds like nothing. Later as you add more code, you can come back and change it as needed.
    Don't let not knowing everything about what a class is going to do stop you from getting started writing the class.
    You need an overall design for the program, but a lot of the details you can delay coding until later.

  9. #9
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Re: Classes Help!!! two classes with object question.

    what would it be if there was already a book in the list ?

  10. #10
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Re: Classes Help!!! two classes with object question.

    public void addBook(String name, String Author)
    {
    // don't understand what to put here

    list.add(?);
    }

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Classes Help!!! two classes with object question.

    What is the addBook method supposed to do? What arguments will it get and what will it do with them?
    By the name I'd think it was supposed to take a Book and add it to a list.
    The arguments to the method is not a Book object.

  12. #12
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Re: Classes Help!!! two classes with object question.

    yes thats all its meant to do.

    Can someone help me implement the addbook method please

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Classes Help!!! two classes with object question.

    You've already almost done it.
    Your list.add() is correct if you do as Norm suggests and have addBook take a Book, and not two Strings.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Classes Help!!! two classes with object question.

    Can someone help me implement the addbook method please
    Describe what the method is supposed to do.
    What args does it take?
    What does it do with the args?
    What does it return?

  15. #15
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Re: Classes Help!!! two classes with object question.

    Quote Originally Posted by Norm View Post
    Describe what the method is supposed to do.
    What args does it take?
    What does it do with the args?
    What does it return?
    All the method does is add a book to the list. So I think the parameter is a book object and it adds it to the list. it doesn't have any return value.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Classes Help!!! two classes with object question.

    add a book to the list
    Can you explain what problem you are having doing this?
    If the list is an object of a class, you need to read the API doc for the class to see what method to use.

  17. #17
    felimblack is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default Re: Classes Help!!! two classes with object question.

    Quote Originally Posted by stuckonjava View Post
    public void addBook(String name, String Author)
    {
    // don't understand what to put here

    list.add(?);
    }
    If list was declared as an ArrayList<Books>, the right code for your application might be:
    Java Code:
    public void addBook(Books _book) {
    list.add(_book);
    }
    This simple method adds the book object you pass as argument to your library list.
    Last edited by felimblack; 02-10-2012 at 01:49 AM.

Similar Threads

  1. Question about abstract classes and methods
    By mwr1976 in forum New To Java
    Replies: 8
    Last Post: 11-21-2011, 01:42 PM
  2. Abstract Classes??? Creating a new object????
    By jonytek in forum New To Java
    Replies: 2
    Last Post: 03-07-2011, 03:34 AM
  3. question about abstract classes
    By TheFlying_Boy in forum New To Java
    Replies: 6
    Last Post: 07-08-2009, 07:19 AM
  4. Object access between classes
    By qazplm123890 in forum AWT / Swing
    Replies: 1
    Last Post: 03-05-2009, 05:00 AM
  5. Classes with object attributes
    By RRasco in forum New To Java
    Replies: 5
    Last Post: 11-25-2008, 12:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •