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

home / study / engineering / computer science / questions and answers / picky pu

ID: 3693125 • Letter: H

Question

home / study / engineering / computer science / questions and answers / picky publishing house publishes stories in three ...

Your question has been answered! Rate it below.

Let us know if you got a helpful answer.

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

BELOW IS THE CODE I ALREADY HAVE.

public abstract class Story
{
    private String title;
    private String authorName;
    protected int numberOfPages;
    private String message;
   
    protected final int MIN_PAGES_IN_NOVEL = 101;
    protected final int MIN_PAGES_IN_NOVELLA = 50;
    protected final int MAX_PAGES_IN_NOVELLA = 100;
    protected final int MAX_PAGES_IN_SHORT_STORY = 49;
   
    public Story(String title, String authorName)
    {
        this.title = title;
        this.authorName = authorName;
    }
   
    public String getTitle()
    {
        return title;
    }
   
    public String getAuthorName()
    {
        return authorName;
    }
   
    public int getNumberOfPages()
    {
        return numberOfPages;
    }
   
    public String getMessage()
    {
        return message;
    }
   
    public void setTitle(String title)
    {
        this.title = title;
    }
   
    public void setAuthorName(String authorName)
    {
        this.authorName = authorName;
    }
   
    public abstract void setPages(int numberOfPages);
   
    public void setMessage(String message)
    {
        this.message = message;
    }
   
    public String toString()
    {
        String retString = ">>>Title: " + this.getTitle() + ", Author: " +
        this.getAuthorName() + ", Number of pages: " + this.getNumberOfPages();
       
        if (!this.getMessage().isEmpty())
        {
            retString = retString + "     Note: " + this.getMessage();
        }
        return retString;
    }
}

################################################################

public class Novel extends Story
{
    public Novel(String title,String authorName,int pages)
    {
        super(title,authorName);
        setPages(pages);
    }
    public void setPages(int pages)
    {
        super.numberOfPages = pages;
        if(pages < super.MIN_PAGES_IN_NOVEL)
        {
            super.setMessage("You still need "+(super.MIN_PAGES_IN_NOVEL - pages)+" pages.");
        }
        else
        {
            super.setMessage("");
        }
    }
}

#########################################

public class Novella extends Story
{
    public Novella(String title,String authorName,int pages)
    {
        super(title,authorName);
        setPages(pages);
    }
  
    public void setPages(int pages)
    {
        super.numberOfPages = pages;
        if(pages < super.MIN_PAGES_IN_NOVELLA)
        {
            super.setMessage("You still need "+(super.MIN_PAGES_IN_NOVEL - pages)+" pages.");
        }
        else if (pages > super.MAX_PAGES_IN_NOVELLA)
        {
            super.setMessage("You have "+(pages - super.MAX_PAGES_IN_NOVELLA)+" extra pages.");
        }
        else
        {
            super.setMessage("");
        }
    }
}

#################################

public class ShortStory extends Story
{
    public ShortStory(String title,String authorName,int pages)
    {
        super(title,authorName);
        setPages(pages);
    }
  
    public void setPages(int pages)
    {
        super.numberOfPages = pages;
        if(pages > super.MAX_PAGES_IN_SHORT_STORY)
        {
            super.setMessage("You have "+(pages - super.MAX_PAGES_IN_SHORT_STORY)+" extra pages.");
        }
        else
        {
            super.setMessage("");
        }
    }
}

###############################

public class StoryDemo
{
    public static void main(String[] args)
    {
        Story books[] = new Story[6];
      
        books[0] = new Novel("Moby-Dick","Herman Melville",350);
        books[1] = new Novel("The Great Gatsby","F. Scott Fitzgerald",98);

        books[2] = new Novella("Breakfast at Tiffany's","Truman Capote",83);
        books[3] = new Novella("The Time Machine","H.G. Wells",300);
      
        books[5] = new ShortStory("The Legend of Sleepy Hollow","Washington Irving",40);
        books[4] = new ShortStory("The Lottery","Shirley Jackson",70);
      
      
        for(int i=0;i<6;i++)
        {
            System.out.println(books[i]+" ");
        }
    }
}

OUTPUT:

>>>Title: Moby-Dick, Author: Herman Melville, Number of pages: 350

>>>Title: The Great Gatsby, Author: F. Scott Fitzgerald, Number of pages: 98
    Note: You still need 3 pages.

>>>Title: Breakfast at Tiffany's, Author: Truman Capote, Number of pages: 83

>>>Title: The Time Machine, Author: H.G. Wells, Number of pages: 300
    Note: You have 200 extra pages.

>>>Title: The Lottery, Author: Shirley Jackson, Number of pages: 70
    Note: You have 21 extra pages.

>>>Title: The Legend of Sleepy Hollow, Author: Washington Irving, Number of pages: 40

Explanation / Answer

class InvalidPageCountException extends RuntimeException{ public InvalidPageCountException(String message) { super(message); } } abstract class Story { private String title; private String authorName; protected int numberOfPages; protected final int MIN_PAGES_IN_NOVEL = 101; protected final int MIN_PAGES_IN_NOVELLA = 50; protected final int MAX_PAGES_IN_NOVELLA = 100; protected final int MAX_PAGES_IN_SHORT_STORY = 49; public Story(String title, String authorName) { this.title = title; this.authorName = authorName; } public String getTitle() { return title; } public String getAuthorName() { return authorName; } public int getNumberOfPages() { return numberOfPages; } public void setTitle(String title) { this.title = title; } public void setAuthorName(String authorName) { this.authorName = authorName; } public abstract void setPages(int numberOfPages); public String toString() { String retString = ">>>Title: " + this.getTitle() + ", Author: " + this.getAuthorName() + ", Number of pages: " + this.getNumberOfPages(); return retString; } } class Novel extends Story { public Novel(String title,String authorName,int pages) { super(title,authorName); setPages(pages); } public void setPages(int pages) { super.numberOfPages = pages; if(pages super.MAX_PAGES_IN_SHORT_STORY) { new InvalidPageCountException("You have "+(pages - super.MAX_PAGES_IN_SHORT_STORY)+" extra pages."); } } } class StoryDemo { public static void main(String[] args) { Story books[] = new Story[6]; books[0] = new Novel("Moby-Dick","Herman Melville",350); books[1] = new Novel("The Great Gatsby","F. Scott Fitzgerald",98); books[2] = new Novella("Breakfast at Tiffany's","Truman Capote",83); books[3] = new Novella("The Time Machine","H.G. Wells",300); books[5] = new ShortStory("The Legend of Sleepy Hollow","Washington Irving",40); books[4] = new ShortStory("The Lottery","Shirley Jackson",70); for(int i=0;i