CS9053, Assignment7
Due April 21, 2020, 11:55 PM
Prof. Dean Christakos
Part I: Working with stacks and queues
1. Write a method remAllStack(Stack
Important guidelines:
§ Your method may use either another stack or a queue to assist it. It may not use an array, linked list, or other data structure. When choosing between a stack or a queue, choose the one that leads to the more efficient implementation.
§ You should assume that the method does not have access to the internals of the collection objects, and thus you can only interact with them using the methods in the interfaces that we discussed in lecture.
2. Write a method remAllQueue(Queue
Part II: Sets
The reason we like Sets in Java is because they help us think about Sets in a mathematical sense and we can easily implement the functions of Sets that exist in Math—eg, Set intersections and unions. In Python, these set functions are explicit. In Java, they are not.
Create a class MathSet which extends HashSet. It should have three methods:
public Set intersection(Set s2): Takes a Set, s2, and returns the intersection of the Set and s2—the elements that are in both sets.
public Set union(Set s2): Takes a Set, s2, and returns the union of the Set and s2—the combination of all elements.
public Set
I have provided a Pair class for this. Return the Cartesian Product of the base set, s and s2: s × s2:
A Cartesian product of two sets A and B, written as A×B, is the set containing ordered pairs from A and B. That is, if C=A×B, then each element of C is of the form (x,y) where x∈A and y∈B:
A×B={(x,y)|x∈A and y∈B}.
For example, if A={1,2,3} and B={H,T}, then
A×B={(1,H),(1,T),(2,H),(2,T),(3,H),(3,T)}
Note that here the pairs are ordered, so for example, (1,H)≠(H,1). Thus A×B is not the same as B×A.
Part III: Maps
A Map creates Key -> Value relationships. Each Key is unique, but different Keys may map to the same Values.
Start with a Map
2020-04-14