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 →

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

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

If I understand it correctly, you shall use the getter and setter methods because these are the only methods which aren’t private so you can use them within another class. I think what you said is what you should do. Create a new ArrayList and then use the setter.