CSC 143 excersise In this weekly exercise, you will implement a hierarchy of cla
ID: 3823258 • Letter: C
Question
CSC 143 excersise
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 hierarchy consisting of Publication, Magazine, Book, and KidsMagazine 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.
To submit this weekly exercise, gather all *.java source-code files that have been created for the above classes (in Eclipse, you can find these files in src folder under the folder that has been created for this Java project), place them in a folder, zip/compress and attach the folder.
i Adopted from: https://www.engage-csedu.org/find-resources/assignment-4 and https://www.engagecsedu.org/
Explanation / Answer
PROGRAM CODE:
Publication.java
package book;
public class Publication {
private String publisher;
private int numPages;
private double price;
private String title;
public Publication(String publisher, int numPages, double price, String title) {
this.publisher = publisher;
this.numPages = numPages;
this.price = price;
this.title = title;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getNumPages() {
return numPages;
}
public void setNumPages(int numPages) {
this.numPages = numPages;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Title: " + title + " Publisher: " + publisher + " Pages: " + numPages + " Price: $" + price;
}
}
Magazine.java
package book;
public class Magazine extends Publication{
private String publicationUnit;
public Magazine(String publisher, int numPages, double price, String title, String unit) {
super(publisher, numPages, price, title);
publicationUnit = unit;
}
public String getPublicationUnit() {
return publicationUnit;
}
public void setPublicationUnit(String publicationUnit) {
this.publicationUnit = publicationUnit;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString() + " Publication Unit: " + publicationUnit;
}
}
Book.java
package book;
public class Book extends Publication{
private String author;
public Book(String publisher, int numPages, double price, String title, String author) {
super(publisher, numPages, price, title);
this.author = author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return super.toString() + " Author: " + author;
}
}
KidsMagazine.java
package book;
public class KidsMagazine extends Publication{
private int minAge;
private int maxAge;
public KidsMagazine(String publisher, int numPages, double price, String title, int minage, int maxage) {
super(publisher, numPages, price, title);
this.minAge = minage;
this.maxAge = maxage;
}
public int getMinAge() {
return minAge;
}
public void setMinAge(int minAge) {
this.minAge = minAge;
}
public int getMaxAge() {
return maxAge;
}
public void setMaxAge(int maxAge) {
this.maxAge = maxAge;
}
@Override
public String toString() {
return super.toString() + " Min Age: " + minAge + " Max Age: " + maxAge;
}
}
PublisherClient.java
package book;
public class PublisherClient {
public static void main(String[] args) {
Publication publications[] = new Publication[10];
publications[0] = new Magazine("Pearson & Sons", 50, 10, "Bride & Groom", "Monthly");
publications[1] = new Magazine("Techno Publications", 20, 5, "Digit", "Biweekly");
publications[2] = new Book("Harry Publications", 500, 100, "A delight to eye", "Richard Spree");
publications[3] = new KidsMagazine("Perry's Books", 20, 10, "Baby's Day Out", 1, 4);
publications[4] = new Magazine("Harry and Sons", 30, 35, "Vogue", "Weekly");
publications[5] = new KidsMagazine("Newman's Publications", 20, 15, "Young World", 4, 8);
publications[6] = new Book("Grey's Publications", 250, 125, "Green Light", "David Sketcher");
publications[7] = new Book("Harry Publications", 700, 165, "A sky full of stars", "John Wright");
for(int i=0; i<8; i++)
{
System.out.println(publications[i]);
System.out.println("------------------------------------------");
}
}
}
OUTPUT:
Title: Bride & Groom
Publisher: Pearson & Sons
Pages: 50
Price: $10.0
Publication Unit: Monthly
------------------------------------------
Title: Digit
Publisher: Techno Publications
Pages: 20
Price: $5.0
Publication Unit: Biweekly
------------------------------------------
Title: A delight to eye
Publisher: Harry Publications
Pages: 500
Price: $100.0
Author: Richard Spree
------------------------------------------
Title: Baby's Day Out
Publisher: Perry's Books
Pages: 20
Price: $10.0
Min Age: 1
Max Age: 4
------------------------------------------
Title: Vogue
Publisher: Harry and Sons
Pages: 30
Price: $35.0
Publication Unit: Weekly
------------------------------------------
Title: Young World
Publisher: Newman's Publications
Pages: 20
Price: $15.0
Min Age: 4
Max Age: 8
------------------------------------------
Title: Green Light
Publisher: Grey's Publications
Pages: 250
Price: $125.0
Author: David Sketcher
------------------------------------------
Title: A sky full of stars
Publisher: Harry Publications
Pages: 700
Price: $165.0
Author: John Wright
------------------------------------------