Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class hierarchy consisting of publication, magazine, book, and Kids Mag

ID: 3634177 • Letter: D

Question

Design a class hierarchy consisting of publication, magazine, book, and Kids Magazine Classes as follows:
. A publisher has a publisher, number of pages, a price, and a title. The class should implement a print method that displays all of the information.
. A Magazine is a kind of publication that has a publication unit (monthly, weekly, biweekly.) Magazine should override the print method of publication and display all the new information
. A book is a kind of publication that has an author. Book should also override the print method of publication
. A KidsMagazine is a kind of magazine that has a recommended age range. Again, kidsMagazine should override the print method of publication.
. Implement a test class that stores 10 different types of publication: general, magazine, book, or kid’s magazine in an array of publication. Exploit polymorphism and print the information, Sorted by title, about each object stored in the array.

Explanation / Answer

Hello, I replaced the Publisher with a String since you didn't provide a publisher class. you can just replace the String with a publisher object, and in the print method, replace publisher by it's publisher.toString() or publisher.getName() or something.

To sort the array I implemented the Comparable interface compareTo() method for publication to use it on titles.

Also, I only came up with 5 instances to test the prinitng and sorting and it works great - didn't want to rob you of the fun of coming up with your own publication titles and funny names for the authors.

I hope this helps, if something isn't clear or isn't following your requirements, PM me. Remember to rate :)

public class Publication implements Comparable {
   
    private String publisher; // replace this with publisher class
    private String title;
    private int pages;
    private double price;
   
    public Publication(String p, String t, int pg, double pr)
    {
        publisher = p;
        title = t;
        pages = pg;
        price = pr;       
    }
   
    public void print()
    {
        System.out.println("Publisher: " + publisher);
        System.out.println("Title: " + title);
        System.out.println("Number of Pages: " + pages);
        System.out.println("Price: " + price);
    }

    @Override
    public int compareTo(Object o) {
        if( !(o instanceof Publication) )
            throw new ClassCastException();
        Publication p = (Publication)o;
        return title.compareTo(p.title);
    }
   
}

public class PublishingTest {

   
    public static void main(String[] args) {
       
        Publication p1 = new Publication("Peters Publishing", "A Cup of Java",
                467, 43.00);
        Publication p2 = new Publication("IEEE", "Collection of Boring Academic Papers",
                98, 60.00);
        Book b1 = new Book("FictionGeeks", "A Hobbit at Hogwarts",
                281, 24.99, "J.K. Tolkein");
        Magazine m1 = new Magazine("NerdHouse", "Mysterious Algorithms and Whatnot",
                32, 12.00, "Monthly");
        KidsMagazine km1 = new KidsMagazine("Dora Publishing", "Explore!",
                18, 6.00, "Weekly", "5-10" );
       
       
        Publication[] list = { m1, p1, p2, b1, km1 };
       
        java.util.Arrays.sort(list);
       
        for(int i = 0; i < list.length; i++)
        {
            list[i].print();
        }  
       
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote