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

I need is an exception program to display the invalid page count message and sav

ID: 3693122 • Letter: I

Question

I need is 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. The output for the exception program should show all exceptions for all programs Novel, Novella, and Short Story. As well as the rest of the output. Right now it is only showing the exception for Novel and no other output.  

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)throws InvalidPageCountException;
   
    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)throws InvalidPageCountException
    {
        super(title,authorName);
        setPages(pages);
    }
    public void setPages(int pages)throws InvalidPageCountException
    {
        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("");
            throw new InvalidPageCountException("Invalid page count message in Noval");
        }
    }
}

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

public class Novella extends Story
{
    public Novella(String title,String authorName,int pages)throws InvalidPageCountException
    {
        super(title,authorName);
        setPages(pages);
    }
  
    public void setPages(int pages)throws InvalidPageCountException
    {
        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.");
            throw new InvalidPageCountException("Invalid page count message in Noval");

        }
        else
        {
            super.setMessage("");
        }
    }
}

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

public class ShortStory extends Story
{
    public ShortStory(String title,String authorName,int pages)throws InvalidPageCountException
    {
        super(title,authorName);
        setPages(pages);
    }
  
    public void setPages(int pages)throws InvalidPageCountException
    {
        super.numberOfPages = pages;
        if(pages > super.MAX_PAGES_IN_SHORT_STORY)
        {
            super.setMessage("You have "+(pages - super.MAX_PAGES_IN_SHORT_STORY)+" extra pages.");
            throw new InvalidPageCountException("Invalid page count message in Noval");

        }
        else
        {
            super.setMessage("");
        }
    }
}

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

public class StoryDemo
{
    public static void main(String[] args)
    {
      try{
        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]+" ");
        }
         }
         catch(InvalidPageCountException e)
         {
             System.out.println(e);
         }  
    }
}

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

public class InvalidPageCountException extends Exception{
    String str1;
    public InvalidPageCountException(String s){
        this.str1 = s;
    }
public String toString(){
return (str1) ;
}
}

Output:

Invalid page count message in Noval

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