Omar93
Omar93

Reputation: 45

Java specific exercise with Collections.sort

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.

Upvotes: 3

Views: 148

Answers (1)

Mureinik
Mureinik

Reputation: 311338

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;
    }
}

Upvotes: 2

Related Questions