COMPX201 WEEK 1 TUTORIAL SLIDES Notes

Tutorial 1: Sets

Course Information
  • Course Codes: COMPX201-26A & COMPX241-26A

Tutorial Marking Scheme

  • Tutorials contribute 10% to the overall mark.
  • There are ten weekly tutorials to attend.
  • Procedure for Signing Off:
      - Write your name, student ID, and a question on an index card.
      - Submit this index card to the tutor at the start of the tutorial.
      - Questions can pertain to:
        - Lecture topics
        - Assignments
        - Topics about which you wish to know more or want to explore
      - It is encouraged to write your questions in advance, such as during lectures.

Icebreaker Activity

Assignment 1: Card Game Implementation
  • Task: Implement a card game in Java using Linked Lists.
  • Rules of the Game:
      - Each player draws 5 cards to start.
      - A player can play a card if:
        - The suit matches the most recently played card.
        - The number is either +1 or -1 of the most recently played card.
      - If a player cannot play a card, they must draw another card from the deck.
      - The winner is the player that empties their hand first.

Defining Data Structures

What is a Set?

  • A set is a fundamental data structure that holds a collection of distinct elements. Sets do not allow duplicate values and provide operations to perform various set-related tasks.

Use Cases for Sets

Problems Where Sets are Helpful

  • Sets are beneficial for scenarios such as:
      - Unique item tracking: Storing unique elements like usernames, product IDs, or tags.
      - Membership testing: Quickly checking if an element exists in a collection.
      - Eliminating duplicates: Filtering lists to remove duplicate entries.
      - Mathematical operations: Performing union, intersection, and difference operations on collections of data.

Set Operations

Example with Sets A and B
  • Assuming we have two sets:
      - Set A: A=1,2,3,4,5A = {1, 2, 3, 4, 5}
      - Set B: B=1,3,5,7,9B = {1, 3, 5, 7, 9}
Operations
  • Define the union of sets A and B:
      - x=ABx = A \cup B
  • Define the intersection of sets A and B:
      - y=ABy = A \cap B
  • Define the difference of sets A and B:
      - z=ABz = A \setminus B

Java Implementation Examples

Difference Method

  • Write the Java code for a method to compute the difference of two sets:
      - Method signature: SimpleSet difference(SimpleSet s)
      - You may use the remove(int i) method in the SimpleSet class.
Movie Recommendation Filtering
  • Complete the code for a method that returns a list of recommended movies that have not been watched:
      - Method Signature:
    java public MovieSet filterRecommendations(MovieSet rec, MovieSet watched)   
      - This method will filter the rec set based on the watched set.

Set Application: Checking for Equivalence

  • Implement a custom function to check if two sets are equivalent.
      - Example sets to check:
        - Set 1: 2,1,5,3,7{2, 1, 5, 3, 7}
        - Set 2: 2,1,5,3{2, 1, 5, 3}
      - This entails determining if both sets contain the same elements, irrespective of order or duplicates.