Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in java The class ResizableArrayBag implements the interface BagInterface where the bag is represented as an array that can expand dynamically as necessary. Make sure
in java
- The class ResizableArrayBag implements the interface BagInterface where the bag is represented as an array that can expand dynamically as necessary. Make sure that the DEFAULT_CAPACITY in ResizableArrayBag is set to 25.
- Analyze provided interface including javadoc comments that describe the purpose of each method, its parameters and return values. UML diagram is also provided for your reference.
- Analyze the implementation of the methods provided in the ResizableArrayBag class. Note that the main is included in this class as well.
- Implement the remaining methods that are stubs at this moment:
- public boolean equals(BagInterface other);
- public void removeEvery(T anEntry);
- public T replace(T replacement);
- public BagInterface union(BagInterface other);
- public BagInterface intersection(BagInterface other);
- public BagInterface difference(BagInterface other);
- public boolean isSubset(BagInterface other);
- public BagInterface getAllLessThan(T anEntry);
- public T removeMax();
The TODO sections are all that need to be done
public class ResizableArrayBag> implements BagInterface { private T[] bag; // Cannot be final due to doubling private int numberOfEntries; private boolean initialized = false; // TODO Project3 - change the DEFAULT_CAPACITY to 25 private static final int DEFAULT_CAPACITY = 3; // Initial capacity of bag private static final int MAX_CAPACITY = 10000; /** * Creates an empty bag whose initial capacity is 25. */ public ResizableArrayBag() { this(DEFAULT_CAPACITY); } // end default constructor /** * Creates an empty bag having a given initial capacity. * * @param initialCapacity The integer capacity desired. */ public ResizableArrayBag(int initialCapacity) { checkCapacity(initialCapacity); // The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] tempBag = (T[]) new Comparable[initialCapacity]; // Unchecked cast this.bag = tempBag; this.numberOfEntries = 0; this.initialized = true; } // end constructor /** * Creates a bag containing given entries. * * @param contents An array of objects. * @param numberOfElements - the number of entries we want to copy starting at index 0 */ public ResizableArrayBag(T[] contents, int numberOfElements) { this(numberOfElements); for (int i = 0; i < numberOfElements; i++) { if (contents[i] != null) { this.bag[this.numberOfEntries] = contents[i]; this.numberOfEntries++; } } } // end constructor /** * Adds a new entry to this bag. * * @param newEntry The object to be added as a new entry. * @return True. */ public boolean add(T newEntry) { checkInitialization(); if (isArrayFull()) { doubleCapacity(); } this.bag[this.numberOfEntries] = newEntry; this.numberOfEntries++; return true; } // end add /** * Retrieves all entries that are in this bag. * * @return A newly allocated array of all the entries in this bag. */ public T[] toArray() { checkInitialization(); // The cast is safe because the new array contains null entries. @SuppressWarnings("unchecked") T[] result = (T[]) new Comparable[this.numberOfEntries]; // Unchecked cast for (int index = 0; index < this.numberOfEntries; index++) { result[index] = this.bag[index]; } return result; } // end toArray /** * Sees whether this bag is empty. * * @return True if this bag is empty, or false if not. */ public boolean isEmpty() { return this.numberOfEntries == 0; } // end isEmpty /** * Gets the current number of entries in this bag. * * @return The integer number of entries currently in this bag. */ public int getCurrentSize() { return this.numberOfEntries; } // end getCurrentSize /** * Counts the number of times a given entry appears in this bag. * * @param anEntry The entry to be counted. * @return The number of times anEntry appears in this ba. */ public int getFrequencyOf(T anEntry) { checkInitialization(); int counter = 0; for (int index = 0; index < this.numberOfEntries; index++) { if (anEntry.equals(this.bag[index])) { counter++; } } return counter; } // end getFrequencyOf /** * Tests whether this bag contains a given entry. * * @param anEntry The entry to locate. * @return True if this bag contains anEntry, or false otherwise. */ public boolean contains(T anEntry) { checkInitialization(); return getIndexOf(anEntry) > -1; // or >= 0 } // end contains // Locates a given entry within the array bag. // Returns the index of the entry, if located, // or -1 otherwise. // Precondition: checkInitialization has been called. private int getIndexOf(T anEntry) { int where = -1; boolean stillLooking = true; for (int index = 0; stillLooking && (index < this.numberOfEntries); index++) { if (anEntry.equals(this.bag[index])) { stillLooking = false; where = index; } } return where; } // end getIndexOf /** * Removes all entries from this bag. */ public void clear() { checkInitialization(); while (!isEmpty()) remove(); } // end clear /** * Removes one unspecified entry from this bag, if possible. * * @return Either the removed entry, if the removal * was successful, or null. */ public T remove() { checkInitialization(); return removeEntry(this.numberOfEntries - 1); } // end remove /** * Removes one occurrence of a given entry from this bag. * * @param anElement The entry to be removed. * @return True if the removal was successful, or false if not. */ public boolean removeElement(T anElement) { checkInitialization(); int index = getIndexOf(anElement); T result = removeEntry(index); return anElement.equals(result); } // end remove // Removes and returns the entry at a given index within the array. // If no such entry exists, returns null. // Precondition: 0 <= givenIndex < numberOfEntries. // Precondition: checkInitialization has been called. private T removeEntry(int givenIndex) { T result = null; if (!isEmpty() && (givenIndex >= 0)) { result = this.bag[givenIndex]; // Entry to remove this.numberOfEntries--; this.bag[givenIndex] = this.bag[this.numberOfEntries]; // Replace entry to remove with last entry this.bag[this.numberOfEntries] = null; // Remove reference to last entry } return result; } // end removeEntry // Returns true if the array bag is full, or false if not. private boolean isArrayFull() { return this.numberOfEntries >= this.bag.length; } // end isArrayFull // Doubles the size of the array bag. // Precondition: checkInitialization has been called. private void doubleCapacity() { int newLength = 2 * this.bag.length; checkCapacity(newLength); this.bag = Arrays.copyOf(this.bag, newLength); } // end doubleCapacity // Throws an exception if the client requests a capacity that is too large. private void checkCapacity(int capacity) { if (capacity > MAX_CAPACITY) throw new IllegalStateException("Attempt to create a bag whose capacity exceeds " + "allowed maximum of " + MAX_CAPACITY); } // end checkCapacity // Throws an exception if receiving object is not initialized. private void checkInitialization() { if (!this.initialized) throw new SecurityException("Uninitialized object used " + "to call an ArrayBag method."); } // end checkInitialization /** * Displays all the elements in the bag */ public void display() { if (this.numberOfEntries > 0) { System.out.print("There are " + this.numberOfEntries + " element(s): "); for (int index = 0; index < this.numberOfEntries; index++) { System.out.print(this.bag[index] + " "); } System.out.println(); } else System.out.println("The bag is empty"); } // end display // // +++++++++++++++++++ NEW METHODS +++++++++++++++++++++++++++ // /** * Checks if the given bag called other is the same as the bag * * @param o the other bag to be compared with * @return true both bags are the same */ public boolean equals(Object o) { boolean same; if (this == o) same = true; else if (o == null || getClass() != o.getClass()) same = false; else { ResizableArrayBag other = (ResizableArrayBag) o; //TODO Project3 // one return statement per method please // first compare number of entries in both bags // only if the number of entries is the same // use a regular for loop to compare elements // stop the loop as soon as the first unequal pair is found } return false; // this is a stub, must return the computed value of same } /** * Removes the largest entry from the this.bag * * @return - null if the element was not found or the largest element. Uses removeEntry(givenIndex) method */ public T removeMax() { //TODO Project3 // one return statement per method please return null; //THIS IS A STUB } // end removeMax /** * Removes every occurrence of a given entry from this bag. Uses removeEntry(givenIndex) method * * @param anEntry the entry to be removed */ public void removeEvery(T anEntry) { //TODO Project3 // must utilize only one loop that starts with the last element } // end removeEvery /** * Replaces the last entry in this bag with a given object. * * @param replacement the given object * @return the original entry in the bag that was replaced or null if the bag was empty */ public T replace(T replacement) { //TODO Project3 // replace data at selected index // one return statement per method please return null; //THIS IS A STUB } // end replace /** * Creates a new bag that combines the contents of this bag and * the second given bag without affecting the original two bags. * * @param otherBag the given bag * @return a bag that is the union of the two bags */ public BagInterface union(BagInterface otherBag) { ResizableArrayBag other = (ResizableArrayBag) otherBag; ResizableArrayBag unionBag = new ResizableArrayBag<>(); //TODO Project3 // one return statement per method please return unionBag; } // end union /** * Creates a new bag that contains those objects that occur in both this * bag and the second given bag without affecting the original two bags. * utilizes getIndexOf(anEntry) and removeEntry(givenIndex) methods * * @param otherBag the given bag * @return a bag that is the intersection of the two bags */ public BagInterface intersection(BagInterface otherBag) { ResizableArrayBag other = (ResizableArrayBag) otherBag; ResizableArrayBag intersectionBag = new ResizableArrayBag<>(); ResizableArrayBag copyOfOtherBag = new ResizableArrayBag<>(); //TODO Project3 // do NOT call contains, utilize getIndexOf(anEntry) and removeEntry(givenIndex) methods instead // one return statement per method please return intersectionBag; } // end intersection /** * Creates a new bag of objects that would be left in this bag * after removing those that also occur in a second given bag * without affecting the original two bags. * * @param otherBag the given bag * @return a bag that is the difference of the two bags */ public BagInterface difference(BagInterface otherBag) { ResizableArrayBag other = (ResizableArrayBag) otherBag; ResizableArrayBag differenceBag = new ResizableArrayBag<>(); //TODO Project3 // do NOT call contains, utilize getIndexOf(anEntry) and removeEntry(givenIndex) methods instead // one return statement per method please return differenceBag; } // end difference /** * Creates a new bag of objects that are in this bag and are less than a given object. * * @param anEntry a given object * @return a new bag of objects that are in this bag and are less than anEntry */ public BagInterface getAllLessThan(T anEntry) { BagInterface result = new ResizableArrayBag<>(); //TODO Project3 // utilize compareTo method // one return statement per method please return result; } // end getAllLessThan /** * Checks if all the elements of the given bag are also included in the other bag * * @param other bag to check * @return returns true if all the elements of the given bag are also included in the other bag */ public boolean isSubset(BagInterface other) { //TODO Project3 // utilize difference method // one return statement per method please return false; //THIS IS A STUB }
Step 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