Write a class that represents a set. Recall that a set has the following propert
ID: 3885048 • Letter: W
Question
Write a class that represents a set. Recall that a set has the following properties it does not contain duplicates and order is not important. If you are unable to make a class that can use any type (generics), make your set handle whole numbers. You must implement your set using an array. The amount of storage used should grow and shrink as needed: it should be halved if the array is 25% full and doubled when full. Your set should support the following operations: a. Add a value b. Does the set contain a value c. Delete a value d. Union: return a new set containing all your values and another set e. toString Here is a UML diagram for a MagicSet that only handles whole numbers. Write a driver program to test your class.Explanation / Answer
import java.util.List; import java.util.ArrayList; import java.util.Iterator; public class ArrayListPreJDK15Test { public static void main(String[] args) { List lst = new ArrayList(); // A List contains instances of Object. Upcast ArrayList to List lst.add("alpha"); // add() takes Object. String upcast to Object implicitly lst.add("beta"); lst.add("charlie"); System.out.println(lst); // [alpha, beta, charlie] // Get a "iterator" instance from List to iterate thru all the elements of the List Iterator iter = lst.iterator(); while (iter.hasNext()) { // any more element // Retrieve the next element, explicitly downcast from Object back to String String str = (String)iter.next(); System.out.println(str); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.