a) Create a class ReadingMaterial. b) Create a subclass of ReadingMaterial calle
ID: 3865144 • Letter: A
Question
a) Create a class ReadingMaterial.
b) Create a subclass of ReadingMaterial called Newspaper - should call superclass constructors.
c) Create a subclass of ReadingMaterial called Manga- should call superclass constructors.
d) Create a subclass of ReadingMaterial called Book- should call superclass constructors.
e) Create a subclass of Book called Textbook- should call superclass constructors.
f) Create a subclass of Book called Novel- should call superclass constructors.
g) Create a class ReadingFactory. ReadingFactory should create a newspaper, a manga, a
textbook, and a novel.
-> ReadingFactory should contain a method that demonstrates polymorphism by displaying the object sent to it.
-> ReadingFactory should have an ArrayList of all the instantiated objects - sort the collection, and then use the display method.
-> Demonstrate casting an object back to its original data type (e.g. cast a ReadingMaterial back to a Novel.)
8) Be sure all methods and JavaDocs have been tested and included.
It should have 7 differents .java class files.
ReadingMaterial language String publisher: String ReadingMaterial (publisher: String, language:String) getLanguage (void): String setLanguage (String) void getPublisher (void): String setPublisher (String): void toString() String Shouild never be instantiated Set default for language Default: EnglishExplanation / Answer
Please find the required program and output below: Please find the comments against each line for the description:
class ReadingFactory {
public static void main(String args[]) {
Newspaper newspaper = new Newspaper("English", "xyz", "Times", new Rectangle(10, 10), 12);
Manga manga = new Manga("English", "abc", "mangzz", "Farley", new Rectangle(5, 5), 24, true);
TextBook textBook = new TextBook("English", "xyz", "Times", "Tom", new Rectangle(10, 10), 123, "123442356", "Science", "Science");
Novel novel = new Novel("English", "xyz", "Times", "Bob", new Rectangle(10, 10), 65, "84736882783","none");
ArrayList<ReadingMaterial> list = new ArrayList<ReadingMaterial>();
list.add(newspaper); //add the created objects to the array list
list.add(manga);
list.add(textBook);
list.add(novel);
for(ReadingMaterial material : list){
display(material);
if(material instanceof Novel){ //checking whether ReadingMaterial is an instance of Novel class
Novel novel2 = (Novel) material; //casting ReadingMaterial to Novel
System.out.println("This is a Novel");
}
}
}
public static void display(ReadingMaterial material) {
System.out.println(material);
}
}
class ReadingMaterial {
String language = "English";
String publisher;
public ReadingMaterial(String language, String publisher) {
this.language = language;
this.publisher = publisher;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
@Override
public String toString() {
return "ReadingMaterial [language=" + language + ", publisher="
+ publisher + "]";
}
}
class Newspaper extends ReadingMaterial{
String name;
Rectangle size;
int numPages;
public Newspaper(String language, String publisher, String name, Rectangle size,
int numPages) {
super(language, publisher);
this.name = name;
this.size = size;
this.numPages = numPages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Rectangle getSize() {
return size;
}
public void setSize(Rectangle size) {
this.size = size;
}
public int getNumPages() {
return numPages;
}
public void setNumPages(int numPages) {
this.numPages = numPages;
}
@Override
public String toString() {
return "Newspaper [name=" + name + ", size=" + size + ", numPages="
+ numPages + ", language=" + language + ", publisher="
+ publisher + "]";
}
}
class Manga extends ReadingMaterial {
String name;
String author;
Rectangle size;
int numPages;
boolean typicalReadingOrder;
public Manga(String language, String publisher, String name, String author,
Rectangle size, int numPages, boolean typicalReadingOrder) {
super(language, publisher);
this.name = name;
this.author = author;
this.size = size;
this.numPages = numPages;
this.typicalReadingOrder = typicalReadingOrder;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Rectangle getSize() {
return size;
}
public void setSize(Rectangle size) {
this.size = size;
}
public int getNumPages() {
return numPages;
}
public void setNumPages(int numPages) {
this.numPages = numPages;
}
public boolean isTypicalReadingOrder() {
return typicalReadingOrder;
}
public void setTypicalReadingOrder(boolean typicalReadingOrder) {
this.typicalReadingOrder = typicalReadingOrder;
}
@Override
public String toString() {
return "Manga [name=" + name + ", author=" + author + ", size=" + size
+ ", numPages=" + numPages + ", typicalReadingOrder="
+ typicalReadingOrder + ", language=" + language
+ ", publisher=" + publisher + "]";
}
}
class Book extends ReadingMaterial {
String name;
String author;
Rectangle size;
int numPages;
String ISBN;
public Book(String language, String publisher, String name, String author,
Rectangle size, int numPages, String iSBN) {
super(language, publisher);
this.name = name;
this.author = author;
this.size = size;
this.numPages = numPages;
ISBN = iSBN;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Rectangle getSize() {
return size;
}
public void setSize(Rectangle size) {
this.size = size;
}
public int getNumPages() {
return numPages;
}
public void setNumPages(int numPages) {
this.numPages = numPages;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String iSBN) {
ISBN = iSBN;
}
@Override
public String toString() {
return "Book [name=" + name + ", author=" + author + ", size=" + size
+ ", numPages=" + numPages + ", ISBN=" + ISBN + ", language="
+ language + ", publisher=" + publisher + "]";
}
}
class TextBook extends Book{
String subject;
String course;
public TextBook(String language, String publisher, String name,
String author, Rectangle size, int numPages, String iSBN,
String subject, String course) {
super(language, publisher, name, author, size, numPages, iSBN);
this.subject = subject;
this.course = course;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
@Override
public String toString() {
return "TextBook [subject=" + subject + ", course=" + course
+ ", name=" + name + ", author=" + author + ", size=" + size
+ ", numPages=" + numPages + ", ISBN=" + ISBN + ", language="
+ language + ", publisher=" + publisher + "]";
}
}
class Novel extends Book{
String genre;
public Novel(String language, String publisher, String name, String author,
Rectangle size, int numPages, String iSBN, String genre) {
super(language, publisher, name, author, size, numPages, iSBN);
this.genre = genre;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
@Override
public String toString() {
return "Novel [genre=" + genre + ", name=" + name + ", author="
+ author + ", size=" + size + ", numPages=" + numPages
+ ", ISBN=" + ISBN + ", language=" + language + ", publisher="
+ publisher + "]";
}
}
class Rectangle {
int width;
int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", height=" + height + "]";
}
}
-------------------------------------------------------------
OUTPUT:
Newspaper [name=Times, size=Rectangle [width=10, height=10], numPages=12, language=English, publisher=xyz]
Manga [name=mangzz, author=Farley, size=Rectangle [width=5, height=5], numPages=24, typicalReadingOrder=true, language=English, publisher=abc]
TextBook [subject=Science, course=Science, name=Times, author=Tom, size=Rectangle [width=10, height=10], numPages=123, ISBN=123442356, language=English, publisher=xyz]
Novel [genre=none, name=Times, author=Bob, size=Rectangle [width=10, height=10], numPages=65, ISBN=84736882783, language=English, publisher=xyz]
This is a Novel
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.