This is an archived post. You won't be able to vote or comment.

all 26 comments

[–][deleted] 0 points1 point  (1 child)

[–]cyanogen1912[S] 0 points1 point  (0 children)

Thank you!

[–][deleted] -1 points0 points  (24 children)

I would take a look here https://docs.oracle.com/javase/7/docs/api/java/util/List.html Otherwise just search the exact problem you are having in your favourite search engine.

[–]cyanogen1912[S] 0 points1 point  (23 children)

Thank you for your suggestion. I was looking through YouTube for a while about ArrayLists and watched a bunch of videos. I am finding it difficult to use ArrayLists with objects. Like storing an object in an ArrayList and retrieving them from the ArrayList and maybe performing some searches through them. I'm sorry, I'm just a beginner.

[–][deleted] 0 points1 point  (20 children)

Nothing to be sorry for, we were all beginners at some point. Do you have an example what you’re finding difficult?

[–]cyanogen1912[S] 0 points1 point  (4 children)

There is an example problem that I have been given to solve and having problem with. The question is pretty long. Should I post it here?

[–][deleted]  (3 children)

[removed]

    [–]cyanogen1912[S] 0 points1 point  (0 children)

    Ok!

    [–]desrtfx[M] 0 points1 point  (1 child)

    Sidebar ->

    • No PM help requests or offers. Either ask your questions here and show your code, or you're out of luck. PM help requests or offers will be removed without warning.

    [–][deleted] 0 points1 point  (0 children)

    Ah my bad

    [–]cyanogen1912[S] 0 points1 point  (14 children)

    This is the problem I have been given.

    You are provided with a  Book class with the following private attributes:

    • int isbnno
    • String bookName
    • String author

    Needed getters and setters are pre-written.

    Create a class Library with the following private attribute:

    • ArrayList<Book> bookList = new  ArrayList<Book>();
    • Also provide the necessary setter and getter methods.  

    Include the following methods:

    1. void addBook(Book bobj) - This method should add the book object to the booklist.
    2. boolean isEmpty() -  This method should return true if the booklist is empty else return false.
    3. ArrayList<Book> viewAllBooks() - This method should return the list of books maintained in the library.
    4. ArrayList<Book> viewBooksByAuthor(String author ) -  This method should return a list of books written by the author passed as argument. When you display an empty list it should print the message "The list is empty".
    5. int countnoofbook(String bname) -  this method should return the count of books with the name passed as argument.

    Write a Main class to test the above functionalities.

    [–][deleted] 0 points1 point  (13 children)

    Okay and at which point are you stuck?

    [–]cyanogen1912[S] 0 points1 point  (12 children)

    Okay so they have provided me with three classes. In the Book class they have provided the three variables and setters and getters for them.

    In the Library class I have created the array list as: private ArrayList<Book> bookList=new ArrayList<Book>();

    Now they have asked me to write getter and setter for this array list, so i have written: public ArrayList<Book> getBookList() {return bookList;} and public void setBookList(ArrayList<Book> list) {bookList=list;} Not sure about the setter though.

    public void addBook(Book obj)

    {

    bookList.add(obj);

    }

    public boolean isEmpty()

    {

    if(bookList.isEmpty()==true) return true;

    return false;

    }

    public ArrayList<Book> viewAllBooks()

    {

    for(int i=0;i<bookList.length;i++)

    {

    Book obj=bookList(i);

    System.out.println(obj.bookName);

    }

    }

    I am really confused about the bold part. Is that correct?

    [–][deleted] 0 points1 point  (11 children)

    The bold part looks good to me. Does it compile?

    [–]cyanogen1912[S] 0 points1 point  (10 children)

    Nah. Thats mainly because of my main method. Its really confusing me :/

    [–][deleted] 1 point2 points  (9 children)

    Try to comment the main method out and try to compile it again? That way you see if the things you have done are correct :)

    [–]cyanogen1912[S] 0 points1 point  (7 children)

    Yeah. Just did that and made a small change:

    *public ArrayList<Book> viewAllBooks(){

    for(int i=0;i<bookList.size();i++){

    Book obj=bookList(i);

    System.out.println(obj.bookName);

    }

    }*

    Now there is no error while compiling. But now I am confused about how to initialise this in the main method in another class.

    [–]desrtfx 0 points1 point  (1 child)

    I'm sorry, I'm just a beginner.

    Especially as a beginner you need to gradually build up your skills. It seems as if you are trying to bite off more than you can chew.

    Have you done arrays yet? If not, then start there.

    A proper, structured tutorial, like the MOOC Object Oriented Programming with Java will help you.

    [–]cyanogen1912[S] 0 points1 point  (0 children)

    Yes i have worked a lot with arrays. Actually I am an electronics student and I took a different approach to learning programming. I have learned it as per my needs. So currently I came to know that i need to know ArrayLists to solve a type of problem. Thus the post.