The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
1. For the first question, only need to finish toArray() and fromArray(E[] arr) these two methods.
1) toArray()-- this method returns a newly allocated array containing the elements in the multiset. The array this method returns must only contain the elements in the multiset and not any nulls or other values that are stored in the backing store, but are not in the multiset.
2) fromArray(E[] arr) this method updates the multiset so that it contains the data in arr. Check if arr is larger than the backing store and, if it is, allocate a new array to be used by the backing store. You should then set the size of the multiset equal to the length ofarrand assign those entries in the backing store to the entries in arr.
2. Implement the add() method and also complete the removeAtIndex(). removeAtIndex(int i) should remove the element current at index i, decrease the multi-set's size, and make certain that the class invariant holds.
For this question I have finished the add() method, but I did not figure out the removeAtIndex() method...
Â
package edu.buffalo.cse116;import java.util.Arrays;import java.util.Collection;import java.util.Iterator;import java.util.List;import java.util.Set;/*** Class which implements the concept of a multiset -- an unorganized collection* which does not limited the number of times an instance can be added.** @author Carl Alphonse* @author Matthew Hertz* @param <E> Type of data contained with the instance.*/public class InitialArrayMultiSet<E> implements Collection<E> {/** Array in which the elements in this multiset are stored. */protected E[] _store;/*** Array indices below this amount contain the active elements in this* collection.*/protected int _size;/*** Modification counter used to preserve the fail-fast nature of its* iterators.*/protected long _modCount;/*** Create a new empty multiset.*/public InitialArrayMultiSet() {_modCount = 0;clear();}/*** Remove all of the elements within the instance and invalidate any current* iterators.*/@SuppressWarnings("unchecked")public void clear() {_store = (E[]) (new Object[16]);_size = 0;// maintains the class invariant_modCount += 1;}/*** Update the multiset so that it includes all of the elements from before the* call AND the given element.** @param e Item to be added to this collection.*/public boolean add(E e) {// To be discussed on Wednesday!throw new UnsupportedOperationException();}/*** Return true if at least one element in the multiset is equal to the given* object. When {@code obj} is null, it must use the {@code ==} operator to
package edu.buffalo.cse116;import java.util.Collection;import java.util.Iterator;/*** Class which implements the concept of a multiset -- an unorganized collection* which does not limited the number of times an instance can be added.** @author Carl Alphonse* @author Matthew Hertz* @param <E> Type of data contained with the instance.*/public class PartialArrayMultiSet<E> implements Collection<E> {/** Array in which the elements in this multiset are stored. */protected E[] _store;/*** Array indices below this amount contain the active elements in this* collection.*/protected int _size;/*** Modification counter used to preserve the fail-fast nature of its* iterators.*/protected long _modCount;/** Class invariants:* _store has all of the elements in the multi-set.* All elements in _store are in indices < _size*//*** Create a new empty multiset.*/public PartialArrayMultiSet() {_modCount = 0;clear();}/*** Remove all of the elements within the instance and invalidate any current* iterators.*/@SuppressWarnings("unchecked")public void clear() {_store = (E[]) (new Object[16]);_size = 0;// maintains the class invariant_modCount += 1;}/*** Update the multiset so that it includes all of the elements from before the* call AND the given element.** @param e Item to be added to this collection.*/@SuppressWarnings("unchecked")public boolean add(E e) {if(_size == _store.length){
-----------