This is a Java Programming question: Picky Publishing House publishes stories in
ID: 3691710 • Letter: T
Question
This is a Java Programming question:
Picky Publishing House publishes stories in three categories and has strict requirements 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.
-This is the part I need help with-
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
ANS;
Story.java ->
public abstract class Story
{
private String storyTitle;
private String authorName;
protected int numberOfPages;
private String message;
protected int minimumNumberOfPages;
protected int maximumNumberOfPages;
public String getStoryTitle()
{
return storyTitle;
}
public String getAuthorName()
{
return authorName;
}
public int getPages()
{
return numberOfPages;
}
public String getMessage()
{
return message;
}
public void setStoryTitle(String storyTitle)
{
this.storyTitle = storyTitle;
}
public void setAuthorName(String authorName)
{
this.authorName = authorName;
}
public abstract void setPages(int numberOfPages);
protected void setMessage(String message)
{
this.message = message;
}
}
Novel.java ->
public class Novel extends Story
{
Novel()
{
minimumNumberOfPages = 101;
setMessage("");
}
public void setPages(int numberOfPages)
{
this.numberOfPages = numberOfPages;
if(numberOfPages<minimumNumberOfPages)
{
setMessage((minimumNumberOfPages-numberOfPages) + " pages must be added to satisfy the rules for the story type");
}
}
}
Novella.java ->
public class Novella extends Story
{
Novella()
{
minimumNumberOfPages = 50;
maximumNumberOfPages = 100;
setMessage("");
}
public void setPages(int numberOfPages)
{
this.numberOfPages = numberOfPages;
if(numberOfPages<50)
{
setMessage((50-numberOfPages) + " pages must be added to satisfy the rules for the story type");
}
else if(numberOfPages>100)
{
setMessage((numberOfPages-100) + " pages must be cut to satisfy the rules for the story type");
}
}
}
ShortStory.java ->
public class ShortStory extends Story
{
ShortStory()
{
maximumNumberOfPages = 49;
setMessage("");
}
public void setPages(int numberOfPages)
{
this.numberOfPages = numberOfPages;
if(numberOfPages>maximumNumberOfPages)
{
setMessage((numberOfPages-maximumNumberOfPages) + " pages must be cut to satisfy the rules for the story type");
}
}
}
StoryDemo.java ->
public class StoryDemo
{
public static void main(String srgs[])
{
Story story[] = new Story[6];
story[0] = new Novel();
story[0].setStoryTitle("Story1");
story[0].setAuthorName("Author1");
story[0].setPages(500);
story[1] = new Novel();
story[1].setStoryTitle("Story2");
story[1].setAuthorName("Author2");
story[1].setPages(60);
story[2] = new Novella();
story[2].setStoryTitle("Story3");
story[2].setAuthorName("Author3");
story[2].setPages(70);
story[3] = new Novella();
story[3].setStoryTitle("Story4");
story[3].setAuthorName("Athor4");
story[3].setPages(20);
story[4] = new ShortStory();
story[4].setStoryTitle("Story5");
story[4].setAuthorName("Athor5");
story[4].setPages(10);
story[5] = new ShortStory();
story[5].setStoryTitle("Story6");
story[5].setAuthorName("Athor6");
story[5].setPages(55);
System.out.println("Story Title : " + story[0].getStoryTitle() + ", Author Name : " + story[0].getAuthorName() + ", Number Of Pages : " + story[0].getPages() + ", Message : " + story[0].getMessage());
System.out.println("Story Title : " + story[1].getStoryTitle() + ", Author Name : " + story[1].getAuthorName() + ", Number Of Pages : " + story[1].getPages() + ", Message : " + story[1].getMessage());
System.out.println("Story Title : " + story[2].getStoryTitle() + ", Author Name : " + story[2].getAuthorName() + ", Number Of Pages : " + story[2].getPages() + ", Message : " + story[2].getMessage());
System.out.println("Story Title : " + story[3].getStoryTitle() + ", Author Name : " + story[3].getAuthorName() + ", Number Of Pages : " + story[3].getPages() + ", Message : " + story[3].getMessage());
System.out.println("Story Title : " + story[4].getStoryTitle() + ", Author Name : " + story[4].getAuthorName() + ", Number Of Pages : " + story[4].getPages() + ", Message : " + story[4].getMessage());
System.out.println("Story Title : " + story[5].getStoryTitle() + ", Author Name : " + story[5].getAuthorName() + ", Number Of Pages : " + story[5].getPages() + ", Message : " + story[5].getMessage());
}
}
Output ->
Story Title : Story1, Author Name : Author1, Number Of Pages : 500, Message :
Story Title : Story2, Author Name : Author2, Number Of Pages : 60, Message : 41 pages must be added to satisfy the rules for the story type
Story Title : Story3, Author Name : Author3, Number Of Pages : 70, Message :
Story Title : Story4, Author Name : Athor4, Number Of Pages : 20, Message : 30 pages must be added to satisfy the rules for the story type
Story Title : Story5, Author Name : Athor5, Number Of Pages : 10, Message :
Story Title : Story6, Author Name : Athor6, Number Of Pages : 55, Message : 6 pages must be cut to satisfy the rules for the story type
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.