Introduction to Java
CS9053 Section I
October 29, 2021
Due: November 5, 2021
Part I: Working with stacks and queues
1.Write a method remAllStack(Stack
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, something which I briefly forgot during lecture.
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
Part IV: Graphics
You are going to create a “roll the dice” application. It will look like this:
The way it works is that when you click on the “Roll Dice” button, both of the dice will change value. At that point, it will show the sum of the dice. If you click on ONE die, it will “roll” that one and then update the total.
Hints: Since there’s little “skeleton” code available, modify the “ImagePanel” code. The ActionListeners/MouseListeners will update the image in the ImagePanel and repaint(). “rolling” a die means picking a random number between 0 and 5 and picking an image to display based on that.
(originally I wanted to do some kind of animation in Java, but that’s actually more difficult than you would think)