3

I have this Test class and I have to write the others classes code in order to pass the asserts. That's the Test class:

import java.util.*;

public class Test
{
    public static void main(String[] args)
    {
        Book javabook = new Book("Learn Java",150);
        Book cbook = new Book("C",120); 
        Book[] books = new Book[] {javabook, cbook};

        Library lib = new Library(books);
        assert(lib.pages() == 270);

        List l = lib;
        assert(l.size() == 2);

        Collections.sort(l); 

        assert(l.get(0) == cbook);
        assert(l.get(1) == javabook);
    }
}

I started making the Book class, here is my implementation:

public class Book implements Comparable
{
    private String name;
    private int pages;

    public Book(String name, int pages)
    {
        this.name = name;
        this.pages = pages;
    }

    public int getPages()
    {
        return this.pages;
    }

    public int compareTo(Object obj)
    {
        if(this == obj) return 0;
        else
        {
            Book x = (Book) obj;
            return this.pages - x.pages;
        }
    }
}

Then I wrote the Library class:

import java.util.*;

public class Library extends ArrayList
{
    ArrayList a;

    public Library(Book[] b)
    {
        a = new ArrayList(b.length);
        for(int i=0; i<b.length; i++)
        {
            a.add(b[i]);
        }
    }

    public int pages()
    {
        int p = 0;
        for(int i=0; i<a.size(); i++)
        {
            p += ( (Book) a.get(i) ).getPages();
        }   
        return p;
    }

    public int size()
    {
        return a.size();
    }

    public Object get(int i)
    {
        return a.get(i);
    }
}

I think the problem is on the Library class, seems like the Collections.sort doesn't work and I can't pass the asserts after the call of the method sort in the Test class! I can't figure out what's the problem on my code, can someone help me please?

Remember: I have to make my code in order to pass the asserts. I'm not sure about my Library implementation in order to make this line on the Test work:

List l = lib;

Not sure about the ArrayList extension.
P.S. I know it's better to use generic types but I mustn't use them for this exercise. Without using them I get some warnings, just ignore them.

2
  • Collections.sort uses some methods like iterators, that your Library class does not delegate to a. What might confuse you is that there are two arraylist states involved for each Library--the one from extending ArrayList, and the one from a. Commented Apr 26, 2015 at 17:14
  • So the problem is that my class doesn't implements iterators methods? Commented Apr 26, 2015 at 17:18

1 Answer 1

2

Your implementation of the Library class is indeed wrong - it shouldn't have a list of Books, it should be a list of Books - that's why it extends an ArrayList:

public class Library extends ArrayList<Book> {
    // Note: no additional data members.
    // The Library is already a List<Book>:

    public Library(Book[] b) {
        super(Arrays.asList(b));
    }

    public int pages() {
        int p = 0;
        for(int i = 0; i < size(); i++) {
            p += get(i).getPages();
        }
        return p;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This solution is helpful for me, never thought it was so simple, but I didn't know the Arrays.asList method so I think I couldn't get this kind of solution by myself. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.