In this weekly exercise, you will implement a hierarchy of classes as described
ID: 3818920 • Letter: I
Question
In this weekly exercise, you will implement a hierarchy of classes as described and. utilizing polymorphism, objects of the classes are exercised in a client program. Design a class hierarehy consisting of Publication, Magazine, Book, and Kids Magazine classes as described below. A Publication has a publisher, number of pages, a price, and a title. The class should implement a toString() method that displays all of this information. This class should be abstract. A Magazine is a kind of publication that has a publication unit (monthly, weekly, biweekly). Magazine should have toString() that display all the information. A Book is a kind of publication that has an author. Book should also have toString(). A KidsMagazine is a kind of magazine that has a recommended age range. Again, KidsMagazine should have toString(). Implement a client class that stores 8 different types of publications: magazine, book, or kid's magazine in an array of Publication. Exploit polymorphism and print the information about each object stored in the array.Explanation / Answer
abstract class Publication //base class
{
private String publisher;
private int pages;
private String title;
public Publication(String publisher,int pages,String title)//argument constructor
{
this.publisher = publisher;
this.pages = pages;
this.title = title;
}
public String toString()
{
return "Publisher : "+publisher + " Number of pages : "+pages + " Title : "+title;
}
}
class Magazine extends Publication //derived class
{
private String publicationUnit;
public Magazine(String publisher,int pages,String title,String publicationUnit)//argument constructor
{
super(publisher,pages,title);// call to base class constructor
this.publicationUnit = publicationUnit;
}
public String toString()
{
return super.toString() +" Publication Unit : "+publicationUnit;// call super.toString()
}
}
class Book extends Publication
{
private String author;
public Book(String publisher,int pages,String title,String author)
{
super(publisher,pages,title);
this.author = author;
}
public String toString()
{
return super.toString() +" Book Author: "+author;
}
}
class KidsMagazine extends Publication
{
private int age;
public KidsMagazine(String publisher,int pages,String title,int age)
{
super(publisher,pages,title);
this.age = age;
}
public String toString()
{
return super.toString()+" Kids Magazine Recommended Age : "+age;
}
}
class TestPublications
{
public static void main (String[] args)
{
Publication[] publications = new Publication[8];//array of publications
publications[0] = new Book("Paperback",367,"Swing","James Gosling");
publications[1] = new Magazine("BBC",78,"BBC News","daily");
publications[2] = new KidsMagazine("National Geographic",58,"Little Kids",5);
for(int i=0;i<3;i++)
{
System.out.println(" Publication "+ (i+1)+" "+ publications[i].toString());
}
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.