Question
in java with working code Modify the Topic 4 Reading Assignment (ReadingMaterial, Newspaper, Book) so that the constructors call setNumPages, and setNumPages throws an exception
in java with working code
Modify the Topic 4 Reading Assignment (ReadingMaterial, Newspaper, Book) so that the constructors call setNumPages, and setNumPages throws an exception that is a subclass of IllegalArgumentException called IllegalNumPagesException, with a member variable of numPages, modeled after the Exception subclass in the textbook for IllegalRadiusException.
Create a TestReadingMaterial class that demonstrates a try/catch/finally block with the use of the IllegalNumPagesException and prints the illegal number of pages. Use the toString() methods of your classes to write your instances of ReadingMaterial, Newspaper, and Book to a textfile named YourName.txt. Close the text file, then read it back to the console.
class ReadingMaterial { private String format;
public ReadingMaterial() { this.format = "print"; }
public ReadingMaterial(String format) { this.format = format; }
public String getFormat() { return format; }
public void setFormat(String format) { this.format = format; }
@Override public String toString() { return "Format: " + format; } }
class Newspaper extends ReadingMaterial { private int numPages;
public Newspaper() { super(); this.numPages = 1; }
public Newspaper(String format, int numPages) { super(format); if (numPages > 0) { this.numPages = numPages; } else { this.numPages = 1; } }
public int getNumPages() { return numPages; }
public void setNumPages(int numPages) { if (numPages > 0) { this.numPages = numPages; } else { this.numPages = 1; } }
@Override public String toString() { return super.toString() + ", Num Pages: " + numPages; } }
class Book extends ReadingMaterial { private int numPages; private String binding;
public Book() { super(); this.numPages = 1; this.binding = "hardcover"; }
public Book(String format, int numPages, String binding) { super(format); if (numPages > 0) { this.numPages = numPages; } else { this.numPages = 1; } if (binding.equals("hardcover") || binding.equals("softcover") || binding.equals("none")) { this.binding = binding; } else { this.binding = "hardcover"; } }
public int getNumPages() { return numPages; }
public void setNumPages(int numPages) { if (numPages > 0) { this.numPages = numPages; } else { this.numPages = 1; } }
public String getBinding() { return binding; }
public void setBinding(String binding) { if (binding.equals("hardcover") || binding.equals("softcover") || binding.equals("none")) { this.binding = binding; } else { this.binding = "hardcover"; } }
@Override public String toString() { return super.toString() + ", Num Pages: " + numPages + ", Binding: " + binding; } }
public class Main { public static void printReadingMaterial(ReadingMaterial rm) { System.out.println(rm.toString()); }
public static void main(String[] args) { ReadingMaterial rm = new ReadingMaterial(); Newspaper newspaper = new Newspaper("print", 20); Book book = new Book("digital", 300, "softcover");
printReadingMaterial(rm); printReadingMaterial(newspaper); printReadingMaterial(book); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started