In my computer science class, we use an online program called CodeHS that gives us assignments that cover certain topics. Usually, I understand ArrayLists relatively well, but this assignment uses Lists instead and it is kind of tripping me up. I have been struggling a little bit and my code (I will link it at the bottom) is returning the exact list that they insert in the method parameters. (in other words, my code is not doing what I want it to. It's not doing anything at all.) I am very new to this and would appreciate some help, with helpful criticism rather than scolding, haha. Thank you.
Here is the Assignment:
You’ve been given a list of books to read over the summer, but you need to trim down the list of books so you can finish all of them.
Write a method
public List<Book> filterBooks(List<Book> readingList, int maxPages)
That takes a List of Books as a parameter, removes all Books from the readingList that have more than maxPages pages, then returns the resulting list.
You can access the number of pages of a Book by calling book.getNumPages(). The Book class is provided for reference.
public class Book
{
private String title;
private String author;
private int numPages;
public Book(String theTitle, String theAuthor, int numberOfPages)
{
title = theTitle;
author = theAuthor;
numPages = numberOfPages;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public int getNumPages()
{
return numPages;
}
public String toString()
{
return title + " by " + author;
}
public boolean equals(Book other)
{
return title.equals(other.title) && author.equals(other.author);
}
}
This is what I have tried:
public List<Book> filterBooks(List<Book> readingList, int maxPages)
{
Book currentBook;
ArrayList<String> readingList2 = new ArrayList<String>();
for(int i= 0; i < readingList.size(); i++)
{
currentBook = readingList.get(i);
if(currentBook.getNumPages() >= maxPages)
{
readingList.remove(currentBook);
}
}
return readingList;
}