Question: package exceptioncatch; import java.util.List; public class CatchExceptions { static List strings; /** * Validates the given string and adds it to the end of list

 package exceptioncatch; import java.util.List; public class CatchExceptions { static List strings;

package exceptioncatch;

import java.util.List;

public class CatchExceptions { static List strings;

/** * Validates the given string and adds it to the end of list of strings * timesToAdd times. * * For example, calling validateAndAdd("abc", 3) should add the string "abc" * to the list of strings three times. * * @param newString string to be added * @param timesToAdd number of times to add * @throws IllegalArgumentException if timesToAdd is less than zero; or if * newString is the empty string * @throws NullPointerException if newString is null * @throws IllegalStateException if the list of strings has not yet been * initialised (i.e. it is null) */ public static void validateAndAdd(String newString, int timesToAdd) { // write your code here }

/** * Calls validateAndAdd with the given parameters. * * If the call to validateAndAdd throws an IllegalStateException, it should * be caught and the message "IllegalStateException" should be printed to * System.out. * * @param newString string to be added * @param timesToAdd number of times to add */ public static void add1(String newString, int timesToAdd) { // write your code here }

/** * Calls validateAndAdd with the given parameters. * * If the call to validateAndAdd throws an IllegalStateException or a * NullPointerException, it should be caught and an IllegalArgumentException * should be thrown instead. * * @param newString string to be added * @param timesToAdd number of times to add */ public static void add2(String newString, int timesToAdd) { // write your code here }

/** * Calls validateAndAdd with the given parameters. * * If the call to validateAndAdd throws an IllegalArgumentException, it * should be "squashed" (ignored). * If the call to validateAndAdd throws an IllegalStateException or a * NullPointerException, it should be caught and the message "Whoops" should * be printed to System.out. * * @param newString string to be added * @param timesToAdd number of times to add */ public static void add3(String newString, int timesToAdd) { // write your code here } }

Generating and catching exceptions Implement the methods validateAndAdd, add, add2 and adds in the manner described by the comments. The validateAndAdd method should validate the given string according to the @throws clauses in the Javadoc, and throw the appropriate exceptions as necessary. The methods add through adds should serve as "wrappers" for the validateAndAdd method, and should use try-catch blocks to detect exceptions thrown by validateAndAdd according to each method's respective Javadoc

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!