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

In this programming project, you will implement a hierarchy of classes as descri

ID: 3853487 • 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().

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.

Explanation / Answer

The answer is as follows:

The code is as follows:

import java.io.*;
import java.util.*

public abstract class Publication {
   private int num_pages;
   private double price;
   private String title;

   public Publication(int num_pages, double price, String title) {
      this.num_pages = num_pages;
      this.price = price;
      this.title = title;
   }
  
   public String toString() {
      return Integer.toString(num_pages) + " " + Double.toString(price) + " " + title;
   }
}

public class Book extends Publication {
   private author;

   public Book(int num_pages, double price, String title, String author) {
      super(num_pages, price, title);
      author = author;
   }
  
   public String toString() {
      return super.toString() + " " + author;
   }
}

public class Magazine extends Publication {
   String unit;

   public Magazine(int num_pages, double price, String title, String unit) {
      super(num_pages, price, title);
      unit = unit;
   }
  
   public String toString() {
      return super.toString() + " " + unit;
   }
}

public class KidsMagazine extends Magazine {
   String age_range; // assuming age range can be given as "2-9" etc

   public Magazine(int num_pages, double price, String title, String unit, String age_range) {
      super(num_pages, price, title, unit);
      age_range = age_range;
   }
  
   public String toString() {
      return super.toString() + " " + age_range;
   }
}

public class Demo {
     public static void main(String[] args){
      
          Publication P[] = new Publication[8];
          int i;
         
          P[0] = new Book(23, 14.25, "Book1", "author1");
          P[1] = new Book(26, 15.25, "Book2", "author2");
          P[2] = new Book(27, 16.25, "Book3", "author3");
          P[3] = new Magazine(26, 15.25, "Magazine1", "weekly");
          P[4] = new Magazine(27, 16.25, "Magazine2", "monthly");
          P[5] = new Magazine(28, 17.25, "Magazine3", "bi-weekly");
          P[6] = new KidsMagazine(27, 16.25, "KidsMagazine1", "monthly", "3-6");
          P[7] = new KidsMagazine(28, 17.25, "KidsMagazine2", "weekly", "7-9");

          for (i = 0; i<8; i++){
              P[i].toString();
          }

     }
}

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