Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Rewrite the codes I provided in order to make the following methods must throw the following Exceptions given the following conditions: Make sure your methods

Rewrite the codes I provided in order to make the following methods must throw the following Exceptions given the following conditions:

Make sure your methods declare the Exceptions they throw (and Javadoc @throws too). For example:

/**

* @param parameter - some parameter

@throws NullPointerException if parameter is null

@throws IllegalArgumentException if parameter is bigger than 100

*/

SAMPLE:

public void pretendMethod(String parameter) throws IllegalArgumentException, NullPointerException

{

// code goes here

}

printTitlesOfLength(int length)

Must throw an IllegalArgumentException if length is 0 or negative with the message bad length

printNameStartsEndsWith(String substring)

Must throw a checked Exception (YOU create the class) named IllegalNameException with the message "bad name" if the substring parameter is null or blank

printTitlesContaining(String substring, boolean caseSensitive)

Must throw a NullPointerException (with message null not allowed) if the substring parameter is null

Must throw an IllegalArgumentException (with message bad string) if the substring parameter is blank

NOTE: when a method or constructor throws one of several different methods, the try/catch block is extended as follows:

try

{

// code to try

}

catch(ExceptionTypeOne e)

{

// handle exception type one here

}

catch(ExceptionTypeTwo e)

{

// handle exception type two here

}

Etc

getLongest(String property)

Must throw an unchecked Exception (YOU create the class) named IllegalNovelPropertyException with the message "bad property" if its value is not "author" nor "title" (in any letter casing)

main(String[] args)

Must try/catch all of its methods now; in the event of an Exception being thrown, simply print the Exception objects getMessage() method output.

---------------------------------Codes------------------------------------ 

// Novel.java

public class Novel { private String title; private String authorName; private int yearPublished; // constructor public Novel(String title, String authorName, int yearPublished) { this.title = title; this.authorName = authorName; this.yearPublished = yearPublished; } // return title public String getTitle() { return title; } // return author name public String getAuthorName() { return authorName; } // return year published public int getYearPublished() { return yearPublished; } }

// end of Novel.java

// BookStore.java import java.util.ArrayList;

public class BookStore {

private String name; private ArrayList novels; // constructor public BookStore(String name) { if(name.equalsIgnoreCase("Amazon")) // check if name is Amazon in any case, then set it to Chapters this.name = "Chapters"; else this.name = name; // create a new ArrayList to store Novel objects novels = new ArrayList<>(); // add Novel objects novels.add(null); novels.add(new Novel("The Adventures of Augie March", "Saul Bellow", 1953)); } // method to display all titles in uppercase public void printAllTitles() { // loop over the Novel objects for(Novel novel : novels) { // novel is not null and title is not null and title is not empty, display the title in uppercase if(novel != null && novel.getTitle() != null && novel.getTitle().trim().length() > 0) System.out.println(novel.getTitle().toUpperCase()); } } // method to display all titles containing substring. Search is case sensitive or not depending upon the parameter caseSensitive public void printTitlesContaining(String substring, boolean caseSensitive) { // loop over the Novel objects for(Novel novel : novels) { // novel is not null and title is not null and title is not empty if(novel != null && novel.getTitle() != null && novel.getTitle().trim().length() > 0) { if(caseSensitive) // case sensitive search { if(novel.getTitle().contains(substring)) System.out.println(novel.getTitle()); } else // case insensitive search { if(novel.getTitle().toLowerCase().contains(substring.toLowerCase())) System.out.println(novel.getTitle()); } } } } // method to display all titles of given length public void printTitlesOfLength(int length) { // loop over the Novel objects for(Novel novel : novels) { // novel is not null and title is not null and title is not empty if(novel != null && novel.getTitle() != null && novel.getTitle().trim().length() > 0) { if(novel.getTitle().length() == length) // novel title's length = input length System.out.println(novel.getTitle()); } } } // method to display the author's name of all Novels which starts or ends with the given substring irrespective of case in lower case public void printNameStartsEndsWith(String substring) { // loop over the Novel objects for(Novel novel : novels) { // novel is not null and authorName is not null and authorName is not empty if(novel != null && novel.getAuthorName() != null && novel.getAuthorName().trim().length() > 0) { // check if author name's starts or ends with the given substring, then display the author's name in lowercase if(novel.getAuthorName().toLowerCase().startsWith(substring.toLowerCase()) || novel.getAuthorName().toLowerCase().endsWith(substring.toLowerCase())) System.out.println(novel.getAuthorName().toLowerCase()); } } } // method to determine and return the longest author name or title based on the given property else return null public String getLongest(String property) { String longest = null; // initialize longest to null if(property.equalsIgnoreCase("author")) // return longest author name { // loop over the Novel objects for(Novel novel : novels) { // novel is not null and authorName is not null and authorName is not empty if(novel != null && novel.getAuthorName() != null && novel.getAuthorName().trim().length() > 0) { // longest is null or current authorName length > longest length, update longest if(longest == null || longest.length() < novel.getAuthorName().length()) longest = novel.getAuthorName(); } } } else if(property.equalsIgnoreCase("title")) // return longest title { // loop over the Novel objects for(Novel novel : novels) { // novel is not null and title is not null and title is not empty if(novel != null && novel.getTitle() != null && novel.getTitle().trim().length() > 0) { // longest is null or current title length > longest length, update longest if(longest == null || longest.length() < novel.getTitle().length()) longest = novel.getTitle(); } } } return longest; } public static void main(String[] args) { // create the BookStore object with the name passed from command line argument BookStore b = new BookStore(args[0]); // call the methods System.out.println("b.printAllTitles();"); b.printAllTitles(); System.out.println(" b.printTitlesContaining(\"the\", false);"); b.printTitlesContaining("the", false); System.out.println(" b.printTitlesContaining(\"the\", true);"); b.printTitlesContaining("the", true); System.out.println(" b.printTitlesOfLength(13);"); b.printTitlesOfLength(13); System.out.println(" b.printNameStartsEndsWith(\"aN\");"); b.printNameStartsEndsWith("aN"); System.out.println(" System.out.println(b.getLongest(\"xyz\"));"); System.out.println(b.getLongest("xyz")); System.out.println(" System.out.println(b.getLongest(\"AutHor\"));"); System.out.println(b.getLongest("AutHor")); System.out.println(" System.out.println(b.getLongest(\"titlE\"));"); System.out.println(b.getLongest("titlE")); }

}

// end of BookStore.java

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions