This will require that you use the ‘SetInterface’ of Chapter 1 instead of the ‘B
ID: 3885308 • Letter: T
Question
This will require that you use the ‘SetInterface’ of Chapter 1 instead of the ‘BagInterface’ of Chapter 2. You will add the methods of the interface to the existing class. Then, develop a test application that will verify the functionality of the solution. You may wish to look at the example test applications. As always, be sure to test ALL methods of the class and interface. Requirements: You will submit the lab following the normal lab procedures.
To Be Done: -
Open Resizable ArrayBag and rename as ArraySet.java
- Global replace of ‘Bag’ with ‘Set’
- Re-write ‘add’ methods to prevent the addition of duplicate entries, using either the ‘contains’ method or ‘getIndexOf’
- We can remove ‘getFrequencyOf’ method.
- Update the test class
– replace the ‘testGetFrequencyOf’ method and add a method to verify that duplicates can’t be added *PDF of the texbook "Data Structures and Abstractions with Java 3rd edition" available upon request
public interface SetInterface
{
public int getCurrentSize();
public boolean isEmpty();
/** Adds a new entry to this set, avoiding duplicates.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful, or
false if the item is already in the set. */
public boolean add(T newEntry);
/** Removes a specific entry from this set, if possible.
@param anEntry The entry to be removed.
@return True if the removal was successful, or false if not. */
public boolean remove(T anEntry);
public T remove();
public void clear();
public boolean contains(T anEntry);
public T[] toArray();
} // end SetInterface
This is the SetInterface.java
I need to modify, add method and i need test files.
Explanation / Answer
ArraySet.java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class ArraySet {
ArrayList set ;
public ArraySet(){
this.set = new ArrayList();
}
public void add(Integer elem){
if(!this.set.contains(elem)){
set.add(elem);
}
}
public void remove(Integer elem){
set.remove(elem);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.