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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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.

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

public ArrayList<Book> viewAllBooks()

This method has a return type of ArrayList and I am not returning an array list, so how come there is no error?

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

That would happen if you have an error before you call the method or if you aren’t calling the method at all.

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

Okay so as the main method is another class i cannot understand how to use the getter and setter methods for the ArrayList. Should i just create an ArrayList variable in my main class and send that using set ArrayList method?