Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Design a class that acts as a library for the following kinds of media: book, video, and newspaper

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Design a class that acts as a library for the following kinds of media: book, video, and newspaper

    import java.util.ArrayList;

    /*
    * example of using Generics
    */

    public class Library<T> {

    private ArrayList<T> mediaCollection = new ArrayList<T>();

    public Library() {
    Book b = new Book();
    storeMedia(b);
    Video v = new Video();
    storeMedia(v);
    Newspaper n = new Newspaper();
    storeMedia(n);
    showMedia();
    }

    public void showMedia() {
    System.out.println("These items are in the media collection:");
    for (Object resource : mediaCollection.toArray()) {
    System.out.print("\t" + resource.toString() + "\n");
    }
    }

    public void storeMedia(T item) {
    mediaCollection.add(item);
    }

    public class Book implements Media {

    String type = "book";
    String name = "Robinson Curuso";

    public Book(){
    System.out.println(this.getClass());
    }

    public String toString() {
    return "type is " + type + ", name is: " + name;
    }
    }

    class Video implements Media {

    String type = "video";
    String name = "Elvis Pressley in the Army";

    public String toString() {
    return "type is " + type + ", name is: " + name;
    }
    }

    class Newspaper {

    String type = "newspaper";
    String name = "News & Disturber";

    public String toString() {
    return "type is " + type + ", name is: " + name;
    }
    }

    interface Media {
    // String type;
    }

    public static void main(String[] args) {
    Library<Media> lib = new Library<Media>();
    }

    }

    --- Update ---

    Current error is on line 13 the "storeMedia(b);" and other two similar lines. Error message, "method storeMedia in class Library<T> cannot be applied to given types, required T, found Library<T>.Book.

    I have made many attempts trying to get clean compile. I think I am missing a basic concept so an explanation along with the code remedy would be appreciated. Thanks in advance


    Quote Originally Posted by vbala2k5 View Post
    import java.util.ArrayList;

    /*
    * example of using Generics
    */

    public class Library<T> {

    private ArrayList<T> mediaCollection = new ArrayList<T>();

    public Library() {
    Book b = new Book();
    storeMedia(b);
    Video v = new Video();
    storeMedia(v);
    Newspaper n = new Newspaper();
    storeMedia(n);
    showMedia();
    }

    public void showMedia() {
    System.out.println("These items are in the media collection:");
    for (Object resource : mediaCollection.toArray()) {
    System.out.print("\t" + resource.toString() + "\n");
    }
    }

    public void storeMedia(T item) {
    mediaCollection.add(item);
    }

    public class Book implements Media {

    String type = "book";
    String name = "Robinson Curuso";

    public Book(){
    System.out.println(this.getClass());
    }

    public String toString() {
    return "type is " + type + ", name is: " + name;
    }
    }

    class Video implements Media {

    String type = "video";
    String name = "Elvis Pressley in the Army";

    public String toString() {
    return "type is " + type + ", name is: " + name;
    }
    }

    class Newspaper {

    String type = "newspaper";
    String name = "News & Disturber";

    public String toString() {
    return "type is " + type + ", name is: " + name;
    }
    }

    interface Media {
    // String type;
    }

    public static void main(String[] args) {
    Library<Media> lib = new Library<Media>();
    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Design a class that acts as a library for the following kinds of media: book, video, and newspaper

    Current error is on line 13
    Which line is line 13? The posted code does not have line numbers. Add a comment on line 13 to make it easy to find.

    Please copy the full text of the error messages and paste it here. You have only posted a few lines.

    The storeMedia method takes a type T. The code is not passing a correct type to the method.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to approach the book Head First Design Patterns?
    By AvivC in forum Java Theory & Questions
    Replies: 0
    Last Post: March 13th, 2014, 03:19 PM
  2. How do you install a class library?
    By ConMan in forum Java Theory & Questions
    Replies: 1
    Last Post: June 7th, 2011, 12:57 AM
  3. Class design
    By arithmetics in forum Object Oriented Programming
    Replies: 4
    Last Post: November 4th, 2010, 08:44 AM
  4. Video Class
    By Swankee in forum Java SE APIs
    Replies: 2
    Last Post: October 8th, 2009, 11:19 AM