4

I am working on a CS-101 assignment and am only allowed to use a single array. I have an array that looks like the following:

[Song, Song, Album, Fiction, Movie, Nonfiction, Song]

Here is the hierarchy for background (requirements from my assignment):

"At the top level you will have a class called Library. Library will have three subclasses: Music, Book, and Movie. Music will have two subclasses: Song and Album. Book will have two subclasses: Fiction and Nonfiction. Movie, Fiction, Nonfiction, Song, and Album will not have any subclasses."

I am currently trying to write a method that will sort the Books by their ISBN number. So Fiction and Nonfiction are subclasses of my Book class, which is a subclass of Library.

I hold everything in Library myLibrary[] = new Library[100];

I'm not sure how to go about retrieving the ISBN's from the Books only and sort them since I am only allowed one array; otherwise I would love to make an array of Books, then sort those separately.

What are some hints / algorithms that I can utilize to accomplish this?

Update

I can post more code if needed. But this question is currently more focused on the approach.

user229044
  • 232,980
  • 40
  • 330
  • 338
ardavis
  • 9,842
  • 12
  • 58
  • 112

4 Answers4

3

The key here is setting up your inheritance correctly and than implementing the Comparable interface. See here for example: Java Comaprable and than calling .sort on your array of your parent type (in your case this would be myLibrary.sort();) Here is an example of how sort works on primitive types: Primitive type array sort

So

  1. Implement Comaparable on your subtypes
  2. Create your array of the parent type and populate it
  3. call sort on your array.

Good Luck!

Community
  • 1
  • 1
cobolstinks
  • 6,801
  • 16
  • 68
  • 97
1

Check if this works or not. (currently on tab so could not run code)

[I think after sorting your books will be saturated towards one side of the array. Please let me know the result]

/* book sorting is in decreasing order of ISBN, followed by non book items
The books will be at the beginning of array, other items towards the end */
Arrays.sort(myLibrary, new Comparator<Library>()
    {
        int compare(Library l1, Library l2){
            //if both are books then compare ISBN and return appropriate
            if((l1 instanceof Book) && (l2 instanceof Book)){
                Book b1=(Book)l1; Book b2=(Book)l2;
                if(b1.getISBN()<b2.getISBN) {
                    return -1;
                } else if(b1.getISBN()>b2.getISBN()) {
                    return 1;
                } else {
                    return 0;
                }
            }
            else {//if either one, or none are Book

                //if only l1 is Book, l2 is not
                if(l1 instanceof Book){
                    return 1;
                }

                //if only l2 is Book, l1 is not
                if(l2 instanceof Book){
                    return -1;
                }

                //none are Book
                return 0;
            }
        }
    }
);
Jit B
  • 1,206
  • 14
  • 26
  • So every non-book item is equal to every book? That can't be correct. (For one thing, it violates the requirement that `x.compareTo(y) == 0` implies that `sgn(x.compareTo(z)) == sgn(y.compareTo(z))` for all `z`. Let `x` and `z` be distinct books and let `y` be a non-book item.) Note that this can be fixed by always returning `1` or `-1` whenever exactly one of `l1` and `l2` is a `Book`. – Ted Hopp Sep 21 '12 at 02:00
  • But `l1,l2` are Library references and it's not necessary that `Library` has `ISBN` attribute; it's specific to `Book`. – Bharat Sinha Sep 21 '12 at 02:10
  • Sorry, the Library instances need to be cast to Book objects before comparing ISBN numbers. I edited the code to reflect the changes. It will work now imo.. – Jit B Sep 21 '12 at 04:00
1

Here you go...

As mentioned in the my previous answer Write a new Comparator and use the same for comparing Library objects.

Note: I haven't checked for null but you should do so...

class LibraryComparator implements Comparator<Library> {
    public int compare(Library l1, Library l2){
         // If Both are Book instance do the comparison
         if(l1 instanceof Book && l2 instanceof Book){
              // Assuming ISBN is a String or Long field in your class Book
              return ((Book)l1).getISBN().compareTo(((Book)l2).getISBN());
         } else {
         // Otherwise no change in ordering
              return 0;
              // You could specify sorting logic for Movie and Music here as well
         }
    }
}

And then you can sort the array like:

Arrays.sort(myLibrary, new LibraryComparator());
Community
  • 1
  • 1
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
0

Without trying to give the actual implementation of the algorithm, you should do an in-place sort where priority can be done by:

1. Books have more priority than Music and Movies

2. If two objects are Books then priority is based on ISBN

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
  • So maybe sort the entire library by first placing the Books at the front? Then only sorting the objects if they are `instanceof Book`? – ardavis Sep 21 '12 at 01:13
  • I meant while sorting you are already moving the Books to the front, take a look at in-place Quicksort – gtgaxiola Sep 21 '12 at 01:18
  • I am marking this as the answer because this is what I utilized for my homework. The other answers may also be correct. – ardavis Sep 21 '12 at 03:31