Question
Use a dynamically resizable array instead of the ResizableArrayBag as defined in the UML diagram below. Test your class with the test cases provided in
Use a dynamically resizable array instead of the ResizableArrayBag as defined in the UML diagram below. Test your class with the test cases provided in main See the UML diagram and the sample run below.
import java.util.Arrays; * A class that implements the ADT set by using a resizable array. * The array is never full. public class ArraySetWithArray
/** * Creates an empty array whose initial capacity is 3. */ public ArraySetWithArray() { //TODO Project2 } // end default constructor
/** * Creates an empty array having a given initial capacity. * * @param capacity The integer capacity desired. */ public ArraySetWithArray(int capacity) { //TODO Project2 } // end constructor
/** * Creates an array containing given entries. * * @param contents An array of objects. */ public ArraySetWithArray(T[] contents) { //TODO Project2 } // end constructor
/** * Adds a new entry to this array, avoiding duplicates. * * @param newEntry The object to be added as a new entry. * @return True if the addition is successful, or false if not. */ public boolean add(T newEntry) { //TODO Project2 return false; //THIS IS A STUB } // end add
// Doubles the size of the array if it is full. private void ensureCapacity() { //TODO Project2 } // end ensureCapacity
/** * Retrieves all entries that are in this array. * * @return A newly allocated array of all the entries. */ public T[] toArray() { //TODO Project2 return null; //THIS IS A STUB } // end toArray
/** * Sees whether this array is empty. * * @return True if this array is empty, or false if not. */ public boolean isEmpty() { //TODO Project2 return false; } // end isEmpty
/** * Gets the number of entries currently in this array. * * @return The integer number of entries currently in the array. */ public int getCurrentSize() { //TODO Project2 return 0; //THIS IS A STUB } // end getCurrentSize
/** * Tests whether this array contains a given entry. * * @param anEntry The entry to locate. * @return True if the array contains anEntry, or false if not. */ public boolean contains(T anEntry) { //TODO Project2 return false; //THIS IS A STUB } // end contains
/** * Removes all entries from this array. */ public void clear() { //TODO Project2 } // end clear
/** * Removes one unspecified entry from this bag. * * @return Either the removed entry if the removal * was successful, or null if not. */ public T remove() { //TODO Project2 return null; //THIS IS A STUB } // end remove
/** * Removes one occurrence of a given entry from this array. * * @param anEntry The entry to be removed. * @return True if the removal was successful, or null if not. */ public boolean removeElement(T anEntry) { //TODO Project2 return false; } // end removeElement
// Removes and returns the array entry at a given index. // If no such entry exists, returns null. private T removeEntry(int givenIndex) { //TODO Project2 return null; //THIS IS A STUB } // end removeEntry
// Displays a set. // If the set is empty displays a message that the set is empty and display the capacity // if the set is not empty displays the number of elements, capacity and the content of the set public void displaySet() { //TODO Project2 } // end displaySet
public static void main(String[] args) { String[] inputData = {"A", "B", "C", "D", "A", "C", "B", "B"};
System.out.println("--> Creating aSet with the secondary constructor - capacity of " + inputData.length); SetInterface
System.out.println(" --> Creating set1 with default constructor"); SetInterface
// Initial capacity is 3 System.out.println("--> set1 initially empty, capacity should be 3:"); set1.displaySet();
System.out.println(" --> Adding elements to set1"); set1.add("A"); set1.add("A"); set1.add("B"); set1.add("A"); set1.add("C"); set1.add("A");
System.out.println("--> set1 after adding elements, capacity should be 3:"); set1.displaySet();
System.out.println(" --> Adding elements to set1");
set1.add("V"); set1.add("V"); set1.add("T"); set1.add("U"); set1.add("V"); set1.add("W"); set1.add("X"); set1.add("Y"); set1.add("Z"); System.out.println(" --> set1 after adding more elements which should have triggered resizing and the capacity should be 12:"); set1.displaySet();
System.out.println(" --> Creating set2 with the secondary constructor that takes an array as input"); SetInterface
System.out.println("--> set2 after adding:"); set2.displaySet();
System.out.println(" --> set1 contains \"A\": " + set1.contains("A")); System.out.println("--> set1 contains \"E\": " + set1.contains("E"));
System.out.println(" --> Removing one element from set1"); set1.removeElement("B"); System.out.println("--> After removing \"B\" from set1:"); set1.displaySet();
System.out.println("--> Removing one element from set1"); System.out.println("--> After removing \"" + set1.remove() + "\" from set1:"); set1.displaySet();
System.out.println(" --> Adding 4 elements to set1"); set1.add("K"); set1.add("L"); set1.add("M"); set1.add("N"); System.out.println("--> After adding 4 elements to set1:"); set1.displaySet();
System.out.println("--> Adding 1 element to set1"); set1.add("O"); System.out.println("--> After adding 1 more element to set1:"); set1.displaySet(); } } // end ArraySetWithArray
Sample Run:
--> Creating aSet with the secondary constructor - capacity of 8
--> Adding elements from inputData to aSet
The set contains 4 string(s); capacity is 8
A B C D
--> Clearing aSet
The set is empty; capacity is 8
--> aSet isEmpty returns
true
--> The number of elements in aSet is
0
--> Creating set1 with default constructor
--> set1 initially empty, capacity should be 3:
The set is empty; capacity is 3
--> Adding elements to set1
--> set1 after adding elements, capacity should be 3:
The set contains 3 string(s); capacity is 3
A B C
--> Adding elements to set1
--> Resizing this.arrayOfSetEntries from 3 to 6
--> Resizing this.arrayOfSetEntries from 6 to 12
--> set1 after adding more elements which should have triggered resizing and the capacity should be 12:
The set contains 10 string(s); capacity is 12
A B C V T U W X Y Z
--> Creating set2 with the secondary constructor that takes an array as input
The set contains 4 string(s); capacity is 8
A B C D
--> Adding more elements to set2
--> set2 after adding:
The set contains 8 string(s); capacity is 8
A B C D E F G H
--> set1 contains "A":
true
--> set1 contains "E":
false
--> Removing one element from set1
--> After removing "B" from set1:
The set contains 9 string(s); capacity is 12
A Z C V T U W X Y
--> Removing one element from set1
--> After removing "Y" from set1:
The set contains 8 string(s); capacity is 12
A Z C V T U W X
--> Adding 4 elements to set1
--> After adding 4 elements to set1:
The set contains 12 string(s); capacity is 12
A Z C V T U W X K L M N
--> Adding 1 element to set1
--> Resizing this.arrayOfSetEntries from 12 to 24
--> After adding 1 more element to set1:
The set contains 13 string(s); capacity is 24
A Z C V T U W X K L M N O
Process finished with exit code 0
ISetinterface int boolean boolean getCurrentSize() m isEmpty0 madd(T) mremoveE lement(T boolean m ' remove) mclear) mcontains(T) void boolean toArray() m displaySet 0 void ArraySetW ithArray rarrayOf SetEntries T[I int int numberOfEntries DEFAULT-CAPACITY m ArraySet WithArray0 mArraySet WithArray(int) mArraySet With Array(TD) madd(T) mensureCapacity m toArray0 misEmpty0 mgetCurrentSize0 m contains(T) m clear) boolean void boolean int boolean void mremove() m removeElement(T) boolean m removeEntry(int) m displaySet 0 av.e main(String[]) void voidStep 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