1

I am trying to create an array of type Library, where I can store many objects of type Library that I'll manage later. Before I get too deep, I am trying to make a print() method on the array so that I can simply call myLibrary.print() to print the array.

public class Library {
    // Constructor

    public Library() {
    }

    public void print() {
        System.out.println("Library Sorted by Title");
    }
}

public class MediaManager {
    public static void main(String[] args) throws Exception {
        Library myLibrary[] = new Library[100];
        myLibrary.print();
    }
}

I am getting an error saying that there is no print() on Library[].

How would I go about printing this array? Would I just loop through the array in the main file and call a separate print on each object? If this is the case, where would I write custom methods to sort the array?

UPDATE

Requirements from my assignment: "Your program will use one array of type Library to store all information read from the input file."

"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."

UPDATE 2

This is for a CS-101 course. I don't feel I should need to use Comparable.

ardavis
  • 9,842
  • 12
  • 58
  • 112
  • Do you want to manage a set of Libraries, or do you want to manage Books/Titles within one Library? Means, do you have one Library with many Titles or do you have many Libraries (possibly again with many Titles)? – Andreas Fester Sep 19 '12 at 05:42
  • Here is the requirement: "Your program will use one array of type Library to store all information read from the input file." A Library is a class, Music, Book, and Movie are sublcasses. Then each of those have subclasses. – ardavis Sep 19 '12 at 05:43
  • why don't you use Collections framework ? in this case may be a List. here's a sorting method http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#sort(java.util.List). you can also use `Arrays.toString(myLibrary)` or `Arrays.deepToString(myLibrary)`, to print the array all at once. chk this: http://stackoverflow.com/questions/409784/simplest-way-to-print-an-array-in-java – Sreenath S Sep 19 '12 at 05:46
  • That does not make sense from a date model perspective. Music, Book and Movie are not Libraries. A Library is a collection of Music, Book and Movie and you can introduce a base class for Music, Book and Movie, like Title or whatsoever – Andreas Fester Sep 19 '12 at 05:47
  • I have to follow the requirements. "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." – ardavis Sep 19 '12 at 05:50
  • `This is for a CS-101 course. I don't feel I should need to use Comparable.` CS-101 course is not a criteria to use `Comparable` or not. – Bharat Sinha Sep 19 '12 at 06:06

6 Answers6

4

In addition to defining print() method for Library; You can try following:

public void printLibrary (Library [] libraryArray) {
    for(Library library :  libraryArray) 
        library.print();
}

or if you overload toString() for Library you can simply use:

public void printLibrary (Library [] libraryArray) {
    for(Library library :  libraryArray) 
        System.out.println(library);
}

And the calling will be done like:

Library myLibrary[] = new Library[100];
printLibrary(myLibrary);

EDIT:

To sort the array you should implement Comparable<Library> in Library class and simply use

Arrays.sort(myLibrary);

to sort the array.

Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • 1
    I think this might be the best answer so far, let me consider this. Thanks much! – ardavis Sep 19 '12 at 05:45
  • I guess before I go ahead and mark it. How would I go about sorting this array then? I would need to call some kind of `myLibrary.sort()` method. But where would I define that method? – ardavis Sep 19 '12 at 05:49
  • Technically that's another question but I will answer as well. – Bharat Sinha Sep 19 '12 at 05:50
  • @ardavis if you go an updating the question with new stuff; answering it makes it really difficult. Please ask separate questions. – Bharat Sinha Sep 19 '12 at 05:53
  • I was updating with background information. Many of the answers have been outside the scope of the task. So I felt more clarification was necessary. Thanks for your help – ardavis Sep 19 '12 at 06:00
  • 1
    Just added another answer to summarize. – Bharat Sinha Sep 19 '12 at 06:03
0

myLibrary is an array of type Library. It doesn't have the method print. You should try something like:

Library myLibrary[] = new Library[100];
myLibrary[0] = new Library();    
myLibrary[0].print();
Hernan Velasquez
  • 2,770
  • 14
  • 21
  • Okay, so this is what I was thinking. I was thinking each "Library" would have a print method. But is it possible to have a "print" method on the entire Library? – ardavis Sep 19 '12 at 05:42
  • but this is what @Hernan says (and what you also have implemented) - you do have the print method on the entire library. Try thinking of your data model - what do you want to model? A collection of libraries? Or Library as a collection of Titles? – Andreas Fester Sep 19 '12 at 05:44
0

you are actually calling print method from Library type Array and you should call print method from Library type object(not Library type Array).

public class Library {
    // Constructor

    public Library() {
    }

    public void print() {
        System.out.println("Library Sorted by Title");
    }
}

public class MediaManager {
    public static void main(String[] args) throws Exception {
        Library myLibrary[] = new Library[100];
        for (Library lb : myLibrary) {
            lb.print();
        }
    }
}

This will work.

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
kundan bora
  • 3,821
  • 2
  • 20
  • 29
0

You declare a print method in Library class, it means you can call that method in each Library object, but you point at an array of Library instead of each library, what you can to do to print all array's object is :

Library myLibrary[] = new Library[100];
for(Library library : myLibrary){ 
    library.print();
}
Iman
  • 381
  • 1
  • 5
0

From the discussions above, a more suited data model would look like this:

public class Library {
   private Vector<Title> allTitles;

   public void print() {
     // Print all titles from the vector
     for(Title t : allTitles) {
        t.print();
     }
   }
}

public abstract class Title {
   public abstract void print();
}

public class Movie extends Title {
   public void print() {
      ... // print the Movie
   }
}

public class Music extends Title {
   public void print() {
      ... // print the Music
   }
}

public class Book extends Title {
   public void print() {
      ... // print the Book
   }
}
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
0

I am bound to add another answer post the question is updated twice from original question:

UPDATE

Requirements from my assignment: "Your program will use one array of type Library to store all information read from the input file." "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."

UPDATE 2

This is for a CS-101 course. I don't feel I should need to use Comparable.

The best solution seems:

A. Create class structure as defined in your problem.

class Library {...}
class Music extends Library {...}
class Book extends Library {...}
class Movie extends Library {...}
class Song extends Music {...}
class Album extends Music {...}
class Fiction extends Book {...}
class NonFiction extends Book {...}

B. Override toString() method in each class.

C. Simply use a for-each loop and print the objects.

for(Library library :  libraryArray) 
    System.out.println(library);

D. You have to use Comparable or Comparator in case you want the Array to be ordered/sorted; there is no other way.

Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • In my `MediaManager` file, I will just write a method, `public void printLibrary(Library[] library)` and loop through the library calling the `print()` method then. Thus avoiding `Comparable` and `Comparator`. – ardavis Sep 19 '12 at 06:04
  • `Comparable` or `Comparator` are for Sorting and not printing. :) – Bharat Sinha Sep 19 '12 at 06:05
  • Oops, right. I meant I would write `public Library[] sortLibrary(Library[] library)` and I would then do a sorting algorithm on it, such as Bubble Sort, etc. – ardavis Sep 19 '12 at 06:06
  • If you want to re-invent the wheel go ahead... **`Comparable` and `Comparator` tells you how two objects are compared no matter what algorithm you use.** Understand them as `>` operator between `a[i]>a[j]` part of bubble sort. – Bharat Sinha Sep 19 '12 at 06:29