Question: Part 1 - Throwing Exceptions Write a class that represents a fixed-size set of non-negative integers (array of integers) with the following methods. You should
Part 1 - Throwing Exceptions Write a class that represents a fixed-size set of non-negative integers (array of integers) with the following methods. You should choose instance variables that will make the implementation easy. The constructor of the set should take the maximum capacity of the set. You should also define two custom exception types subclassing RuntimeException called FullSetException and NegativeIntegerException.
The add(Integer elem) method should add the element to the set. If the input is a negative number it should throw an instance of NegativeIntegerException. If the set is already at-capacity, it should throw an instance of FullSetException.
The contains(Integer elem) method returns true if the element is in the set. The remove(Integer elem) method should remove the element from the set and return true if the set was modified and false otherwise. Both of these methods should throw NegativeIntegerException if the argument is negative.
Part 2 - Handling Exceptions Download the driver for this lab from D2L. Insert try-catch blocks into the program that will inform the user about the specific problem encountered and, if it makes sense, re-prompt for valid input.
THIS IS THE CODE FOR THE DRIVER from D2L.
import java.util.*
public class Driver { public static void main(String[] args) {
Scanner src = new Scanner(System.in); int maxc = 0;
System.out.println("Enter the Maximum capacity of your set: "); maxc = src.nextInt();
Set set = new Set(maxc);
do {
System.out.println("Enter a positive integer to add to your set: "); Integer elem = src.nextInt();
// TODO You need to add one more catch block to the following try // block, // to handle the exception when the user enters negative number to // add to the set // Use e.getMessage() to provide an appropriate message when // exception happens.
try { set.add(elem); } catch (FullSetException e) { System.out.println(e.getMessage()); break;// at this point the array is full and we should break to // stop asking the user to enter new elements }
} while (true);
do {
System.out.print("Enter a positive integer to search in your set: "); Integer elem = src.nextInt();
// TODO You need to put the following statements in a try and catch // block, to handle if user enters a negative number to search // Use e.getMessage() to provide an appropriate message when // exception happens. boolean result = false;
result = set.contains(elem); if (result) System.out.println(elem + " found in the set."); else System.out.println(elem + " not found in the set."); break;
} while (true);
do {
System.out.print("Enter a positive integer to remove from your set: "); Integer elem = src.nextInt();
// TODO You need to put the following statements in a try and catch // block, to handle if user enters a negative number to remove // Use e.getMessage() to provide an appropriate message when // exception happens.
boolean result = false;
result = set.remove(elem); if (result) System.out.println(elem + " removed from the set."); else System.out.println(elem + " removed from the set."); break;
} while (true);
src.close();
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
