Reputation: 47
I want to use the method isLong() from Book class and use it in displayLongBooks() which is in the BookCollection method. Class BookCollection has bookList variable. Variable bookList is of ArrayList type and contains the collection of all books. All objects in the ArrayList are of Book type.
import java.util.ArrayList;
public class BookCollection
{
ArrayList<Book> bookList = new ArrayList<Book>();
public BookCollection(){
bookList.add(new Book(1001-01-141514, "Letones", "CS", 611));
bookList.add(new Book(1002-01-141424, "Lewis", "CS", 477));
bookList.add(new Book(1003-01-141434, "Smith", "MATH", 698));
bookList.add(new Book(1004-01-141444, "Smith", "CS", 617));
bookList.add(new Book(1005-01-141454, "Brown", "CHEM", 326));
bookList.add(new Book(1006-01-141464, "Smith", "BIO", 127));
bookList.add(new Book(1007-01-141474, "Sanket", "CS", 998));
}
public String toString()
{
}
public void displayLongBooks()
{
System.out.println();
System.out.println("LONG BOOKS");
if (isLong() == true)
System.out.println(bookList);
}
public void displayBooksFromAuthor(String author)
{
}
public void displayBooksFromArea(String area)
{
}
public void displayAverageLength()
{
}
}
import java.util.*;
import java.io.*;
public class Book
{
String author;
String area;
int isbn;
int pages;
public Book (int isbn, String author, String area, int pages)
{
this.isbn = isbn;
this.author = author;
this.area = area;
this.pages = pages;
}
public boolean isLong()
{
if(pages>600)
return true;
else
return false;
}
public String toString()
{
return "ISBN: " + this.isbn + "Author: " + this.author
+ "Subject: " + this.area + "Pages: " + this.pages;
}
/**PrintWriter outfile = new PrintWriter (new FileWriter("books.txt"));
outfile.write("100101141514, Letones, CS, 611");
outfile.write(100201141424, Lewis, CS, 477);
outfile.write(100301141434, Smith, MATH, 698);
outfile.write(100401141444, Smith, CS, 617);
outfile.write(100501141454, Brown, CHEM, 326);
outfile.write(100601141464, Smith, BIO, 127);
outfile.write(100701141474, Sanket, CS, 998);
outfile.close();**/
}
Upvotes: 1
Views: 129
Reputation: 201409
Sounds like you need to iterate the bookList
for Book
(s) and call Book.isLong()
for each one. Using a for-each
loop that might look something like
public void displayLongBooks()
{
System.out.println();
System.out.println("LONG BOOKS");
for (Book b : bookList) { // <-- for each Book b in bookList
if (b.isLong()) { // no need for == true
System.out.println(b); // <-- b, not bookList
}
}
}
Upvotes: 2