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

What i need is an exception program to display the invalid page count message an

ID: 3693014 • Letter: W

Question

What 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.  

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

Hi, Added InvalidPageCountException class.

Story.java


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()!= null && !this.getMessage().isEmpty())
{
retString = retString + " Note: " + this.getMessage();
}
return retString;
}
}

Novel.java


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
{
   try{
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");
}
}
catch(InvalidPageCountException e){
   System.out.println("Error: "+e) ;
}
}
}

Novella.java


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
{
   try{
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 Novella");
}
else
{
super.setMessage("");
}
}
catch(InvalidPageCountException e){
   System.out.println("Error: "+e) ;
}
}
}

ShortStory.java


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)
{
   try{
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 ShortStory");
}
else
{
super.setMessage("");

}
}
catch(InvalidPageCountException e){
   System.out.println("Error: "+e) ;
}
}
}

StoryDemo.java


public class StoryDemo
{
public static void main(String[] args) throws InvalidPageCountException
{
     
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]+" ");
}
     
}
}

InvalidPageCountException.java


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

Output:

Error: Invalid page count message in Noval
Error: Invalid page count message in Novella
Error: Invalid page count message in ShortStory

>>>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

>>>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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote