Skip to content

Instantly share code, notes, and snippets.

@mck368
Created December 3, 2018 00:48
Show Gist options
  • Save mck368/761c2f7491ab65d21c2f86ba38083b2e to your computer and use it in GitHub Desktop.
Save mck368/761c2f7491ab65d21c2f86ba38083b2e to your computer and use it in GitHub Desktop.
/**
* Subclass of publication - book.
*
* @author Maria Kalusz
* @version Polymorphism - part B
*
*/
public class Book extends Publication {
/**
* Field for the author of the book.
*/
private String author;
/**
* Constructor to set the number of pages, price,
* title, and author.
*
* @param pages the number of pages
* @param price the price of the book
* @param title the title of the book
* @param author the author of the book
*/
public Book(int pages, double price, String title, String author) {
super(pages, price, title);
this.author = author;
}
/**
* Give a string name to the representation
* of the class object.
*
* @return the String of specific labels
*/
public String toString() {
return super.toString() + "\nThe author is: " + author;
}
}
/**
* Subclass of publication - kids magazine.
*
* @author Maria Kalusz
* @version Polymorphism - part B
*
*/
public class KidsMagazine extends Publication {
/**
* Field for the range of appropriate ages.
*/
private String ageRange;
/**
* Constructor to set the number of pages, price,
* title, and age range.
*
* @param pages the number of pages
* @param price the price of the kids magazine
* @param title the title of the kids magazine
* @param ageRange the appropriate age range
*/
public KidsMagazine(int pages, double price, String title, String ageRange) {
super(pages, price, title);
this.ageRange = ageRange;
}
/**
* Give a string name to the representation
* of the class object.
*
* @return the String of specific labels
*/
public String toString() {
return super.toString() + "\nThe age range is: " + ageRange;
}
}
/**
* Subclass of publication - magazine.
*
* @author Maria Kalusz
* @version Polymorphism - part B
*
*/
public class Magazine extends Publication {
/**
* Field for the frequency of publication.
*/
private String publicationUnit;
/**
* Constructor to set the number of pages, price,
* title, and publication frequency.
*
* @param pages the number of pages
* @param price the prices of the magazine
* @param title the title of the magazine
* @param publicationUnit the publication frequency
*/
public Magazine(int pages, double price, String title, String publicationUnit) {
super(pages, price, title);
this.publicationUnit = publicationUnit;
}
/**
* Give a string name to the representation
* of the class object.
*
* @return the String of specific labels
*/
public String toString() {
return super.toString() + "\nPublication unit: " + publicationUnit;
}
}
/**
* The abstract class to set the
* framework for more specific
* publications.
*
* @author Maria Kalusz
* @version Polymorphism - part B
*/
public abstract class Publication {
/**
* Field for the number of pages
*/
private int pages;
/**
* Field for the price
*/
private double price;
/**
* Field for the title
*/
private String title;
/**
* Constructor to set the number of pages,
* price, and title of the specific publication.
*
* @param pages the number of pages
* @param price the price of the publication
* @param title the title of the publication
*/
public Publication(int pages, double price, String title) {
this.pages = pages;
this.price = price;
this.title = title;
}
/**
* Getter method for the number
* of pages.
*
* @return pages
*/
public int getPages() {
return pages;
}
/**
* Getter method for the price.
*
* @return price
*/
public double getPrice() {
return price;
}
/**
* Getter method for the title.
*
* @return title
*/
public String getTitle() {
return title;
}
/**
* Give a string name to the representation
* of the class object.
*
* @return the String of specific labels
*/
public String toString() {
return "Number of pages: " + pages + "\nThe cost: $" + String.format("%.2f", price) + "\nThe title: " + title;
}
}
/**
* The driver class with the main method
* to run the program.
*
* @author Maria Kalusz
* @version Polymorphism - part B
*
*/
public class PublicationMain {
/**
* Store the objects in an array and print them.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
Publication[] arr = { new Magazine(189, 11.99, "Sceince Times", "Monthly"),
new Magazine(56, 8.99, "The Daily Java", "Daily"),
new Magazine(39, 10.50, "Gossip Groupies", "Weekly"),
new Book(288, 25.50, "Ham on Rye", "Charles Bukowski"),
new Book(288, 30.00, "Thus Spoke Zarathustra", "Friedrich Nietzsche"),
new Book(192, 10.75, "Ask the Dust", "John Fante"),
new KidsMagazine(30, 6.50, "Puppy Palace", "All ages"),
new KidsMagazine(60, 10.00, "Cartoon Mania", "10 & over") };
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment