In this programming project, you will implement a hierarchy of classes as descri
ID: 3857616 • Letter: I
Question
In this programming project, 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, bi- weekly). 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() middotA 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.Explanation / Answer
Publication.java
public abstract class Publication {
private String publisher;
private int noOfPages;
private double price;
private String title;
public Publication(String publisher, int noOfPages, double price,
String title) {
super();
this.publisher = publisher;
this.noOfPages = noOfPages;
this.price = price;
this.title = title;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getNoOfPages() {
return noOfPages;
}
public void setNoOfPages(int noOfPages) {
this.noOfPages = noOfPages;
}
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() {
return "Publication [publisher=" + publisher + ", noOfPages=" + noOfPages + ", price=" + price + ", title=" + title + "]";
}
}
_____________________
Magazine.java
public class Magazine extends Publication {
private String pubUnit;
public Magazine(String publisher, int noOfPages, double price, String title,String pubUnit) {
super(publisher, noOfPages, price, title);
this.pubUnit=pubUnit;
}
public String getPubUnit() {
return pubUnit;
}
public void setPubUnit(String pubUnit) {
this.pubUnit = pubUnit;
}
@Override
public String toString() {
return super.toString()+" Magazine [pubUnit=" + pubUnit + "]";
}
}
___________________
Book.java
public class Book extends Publication {
private String author;
public Book(String publisher, int noOfPages, double price, String title,
String author) {
super(publisher, noOfPages, 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() + " Book [author=" + author + "]";
}
}
______________________
KidsMagazine.java
public class KidsMagazine extends Publication {
private int recAge;
public KidsMagazine(String publisher, int noOfPages, double price,
String title, int recAge) {
super(publisher, noOfPages, price, title);
this.recAge = recAge;
}
public int getRecAge() {
return recAge;
}
public void setRecAge(int recAge) {
this.recAge = recAge;
}
@Override
public String toString() {
return super.toString() + " KidsMagazine [Required Age=" + recAge + "]";
}
}
______________________
Client.java
public class Client {
public static void main(String[] args) {
Publication pub[] = {
new Magazine("ThomsonReuters", 500, 200, "Business India ", "Monthly"),
new Magazine("Wolters Kluwer", 250, 150, "The Caravan", "Weekly"),
new Magazine("Random House", 300, 75, "Dataquest", "Bi-weekly"),
new Book("Cengage", 100, 20, "PCQuest", "J.K Rowling"),
new Book("Wiley", 150, 30, "Tehelka", "John"),
new KidsMagazine(" Shueisha", 50, 10, "Reader's Digest", 12),
new KidsMagazine("Kodansha", 70, 15, "Sportstar", 8),
new KidsMagazine("Shogakukan", 30, 8, "Cartoons", 4),
};
for (int i = 0; i < pub.length; i++) {
System.out.println(pub[i].toString());
}
}
}
____________________
Output:
Publication [publisher=ThomsonReuters, noOfPages=500, price=200.0, title=Business India ] Magazine [pubUnit=Monthly]
Publication [publisher=Wolters Kluwer, noOfPages=250, price=150.0, title=The Caravan] Magazine [pubUnit=Weekly]
Publication [publisher=Random House, noOfPages=300, price=75.0, title=Dataquest] Magazine [pubUnit=Bi-weekly]
Publication [publisher=Cengage, noOfPages=100, price=20.0, title=PCQuest] Book [author=J.K Rowling]
Publication [publisher=Wiley, noOfPages=150, price=30.0, title=Tehelka] Book [author=John]
Publication [publisher= Shueisha, noOfPages=50, price=10.0, title=Reader's Digest] KidsMagazine [Required Age=12]
Publication [publisher=Kodansha, noOfPages=70, price=15.0, title=Sportstar] KidsMagazine [Required Age=8]
Publication [publisher=Shogakukan, noOfPages=30, price=8.0, title=Cartoons] KidsMagazine [Required Age=4]
_________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.