Picky Publishing House publishes stories in three categories and has strict requ
ID: 3692841 • Letter: P
Question
Picky Publishing House publishes stories in three categories and has strict require ments for page counts in each category. Create an abstract class named Story that includes a story title, an author name, a number of pages, and a String message. Include get and set methods for each field. The method that sets the number of pages is abstract. Also include constants for the page limits in each category. Create three Story subclasses named Novel, Novella, and ShortStory, each with a unique setPages() method. A Novel must have more than 100 pages, a Novella must have between 50 and 100 pages inclusive, and a ShortStory must have fewer than 50 pages. If the parameter passed to any of the set methods in the child class is out of range, set the page value but also create and store a message that indicates how many pages must be added or cut to satisfy the rules for the story type. Write an application named StoryDemo that creates an array of at least six objects to demonstrate how the methods work for objects created both with valid and invalid page counts for each story type. For each story, display the title, author, page count, and message if any was generated. Save the files as Story.java, Novel.java, Novella.java, ShortStory.java, and StoryDemo.java
Write an exception program to display the invalid page count message and save it as InvalidPageCountException. Java. Adapt the previous programs to use the Exception program.
Explanation / Answer
public abstract class Story {
protected int pages;
private String title;
private String author;
private String message;
public int getPages() {
return pages;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getMessage() {
return message;
}
public abstract void setPages(int pages);
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
String temp = "Authoe: "+author+
" Title: "+title+
" Pages: "+pages;
if(message !=null)
temp = temp + " Message: "+message;
return temp;
}
}
public class Novel extends Story {
public final int MIN_LIMIT = 101;
public void setPages(int pages){
if(pages <101){
setMessage((101-pages)+" must be included to satisfy story rule");
}
this.pages = pages;
}
}
public class Novella extends Story {
public final int MIN_LIMIT = 50;
public final int MAX_LIMIT = 100;
@Override
public void setPages(int pages) {
if(pages <50){
setMessage((50-pages)+" must be included to satisfy story rule");
}else if(pages > 100){
setMessage((100-pages)+" must be cut to satisfy story rule");
}
this.pages = pages;
}
}
public class ShortStory extends Story {
public final int MAX_LIMIT = 49;
@Override
public void setPages(int pages) {
if(pages >=50){
setMessage((49-pages)+" must be cut to satisfy story rule");
}
this.pages = pages;
}
}
public class StoryDemo {
// creating a Story Object
public static void main(String[] args) {
Story[] stories = {
new Novel(),
new Novella(),
new ShortStory(),
new Novel(),
new Novella(),
new ShortStory()
};
// setting members value of each story object
stories[0].setAuthor("Alex");
stories[0].setTitle("Algorithm");
stories[0].setPages(150);
stories[1].setAuthor("Bob");
stories[1].setTitle("Electronics");
stories[1].setPages(100);
stories[2].setAuthor("Coreman");
stories[2].setTitle("Network");
stories[2].setPages(40);
stories[3].setAuthor("Manno");
stories[3].setTitle("Digital");
stories[3].setPages(130);
stories[4].setAuthor("Chetan");
stories[4].setTitle("Three Mistake");
stories[4].setPages(90);
stories[5].setAuthor("Kalam");
stories[5].setTitle("Gladitor");
stories[5].setPages(150);
// printing
for(Story s : stories){
System.out.println(s.toString());
System.out.println();
}
}
}
/*
Output:
Authoe: Alex
Title: AlgorithmPages: 150
Authoe: Bob
Title: ElectronicsPages: 100
Authoe: Coreman
Title: NetworkPages: 40
Authoe: Manno
Title: DigitalPages: 130
Authoe: Chetan
Title: Three MistakePages: 90
Authoe: Kalam
Title: GladitorPages: 150
Message: -101 must be cut to satisfy story rule
*/