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

Write a class that represents a fixed-size set of non-negative integers (array o

ID: 3865722 • Letter: W

Question

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 NegativelntegerException. 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 clement 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. Download the driver for this lab from D2L. Try a few sequences of inputs that crash the program. 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.

Explanation / Answer

package test;

class FullSetException extends Exception {

public FullSetException(){

}

public FullSetException(String msg){

super(msg);

}

}

class NegativeIntegerException extends Exception {

public NegativeIntegerException(){

}

public NegativeIntegerException(String msg){

super(msg);

}

}

public class Set {

Integer arr[];

int maxCapacity;

int currentCapacity = 0;

public Set(int maxCapacity){

this.maxCapacity = maxCapacity;

arr = new Integer[maxCapacity];

}

// For adding the new element to the Set , Throws Exceptions if there is negative number or set is full

public void add(Integer elem) throws NegativeIntegerException, FullSetException{

if(elem < 0){

throw new NegativeIntegerException();

}

if(arr.length == maxCapacity){

throw new FullSetException();

}

arr[currentCapacity++] = elem;

}

// To check whether Element is present in the Set , Throws Exception if element is negative

public boolean contains(Integer elem) throws NegativeIntegerException{

if(elem < 0){

throw new NegativeIntegerException();

}

for(int i=0; i<currentCapacity; i++){

if(elem == arr[i]){

return true;

}

}

return false;

}

// To check whether Element is present in the Set , if it is present then remove it from the set , just by reducing the current capacity

public boolean remove(Integer elem) throws NegativeIntegerException{

if(contains(elem)){

currentCapacity--;

return true;

}

return false;

}

// You can put your test here

public static void main(String args[]){

}

}

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