Custom methods for an array of custom objects in Java

I am trying to create an array of type Library , where I can store many objects of type Library , which I will use later. Before I get too much, I try to make the print() method in an array so that I can just 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 get an error that there is no print() on Library[] .

How can I print this array? Will I just scroll the array in the main file and cause a separate print on each object? If so, where will I write my own methods to sort the array?

UPDATE

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

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

UPDATE 2

This is for the CS-101 course. I do not feel that I need to use Comparable.

+1
source share
6 answers

In addition to the definition of the print() method for Library ; You can try the following:

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

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

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

And the call will be made as follows:

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

EDIT:

To sort the array, you have to implement the Comparable<Library> class in the Library and just use

 Arrays.sort(myLibrary); 

to sort the array.

+4
source

myLibrary is an array of type library. He has no printing method. You should try something like:

 Library myLibrary[] = new Library[100]; myLibrary[0] = new Library(); myLibrary[0].print(); 
0
source

you actually call the print method from an Array library type and you must call the print method from an object of type library (and not the type of library 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.

0
source

You declare a printing method in the Library class, this means that you can call this method in each library object, but you point to the library array instead of each library, which you can do to print the entire array object:

 Library myLibrary[] = new Library[100]; for(Library library : myLibrary){ library.print(); } 
0
source

From the discussions above, a more suitable 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 } } 
0
source

I must add another answer message, the question is updated twice from the original question:

UPDATE

Requirements of my purpose: "Your program will use one array of type library to store all the information read from the input file." โ€œAt the top level, you will have a class calledโ€œ Library. โ€The library will have three subclasses:โ€œ Music โ€,โ€œ Book โ€andโ€œ Cinema. โ€Music will have two subclasses:โ€œ Song and Album. โ€The book will have two subclasses: "Fiction and Fiction", There will be no subclasses in the works for non-fiction, song and album.

UPDATE 2

This is for the CS-101 course. I do not feel that I need to use Comparable.

The best solution seems to be:

a. Create a class structure 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 {...} 

C. Override the toString() method in each class.

C. Just use the for-each loop and print the objects.

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

E. You must use Comparable or Comparator in case you want to arrange / sort Array ; there is no other way.

0
source

Source: https://habr.com/ru/post/1435407/


All Articles