Set Theory Visualizer
Build an app that performs set operations — union, intersection, difference, symmetric difference, complement, power set — and displays results clearly.
🎯 Learning Goals
- ▹ Master Union, Intersection, and Complement logic
- ▹ Understand Cardinality and Membership in sets
- ▹ Implement Power Set and Subset checking algorithms
- ▹ Build logic to eliminate duplicates from lists
🌎 Why This Matters
Set theory is the foundation of modern database management (SQL). Every time you filter products on Amazon or search for friends on Instagram, you are using set operations like Intersection and Union. Understanding sets is understanding how information is organized.
📖Understanding Set Theory
Theory MasterclassA set is a well-defined collection of distinct objects. Sets are fundamental to all of mathematics. Notation: A = {1, 2, 3, 4, 5}, B = {3, 4, 5, 6, 7} Union (A ∪ B): All elements in A or B or both. A ∪ B = {1, 2, 3, 4, 5, 6, 7} Intersection (A ∩ B): Only elements in both A and B. A ∩ B = {3, 4, 5} Difference (A - B): Elements in A but not in B. A - B = {1, 2} Symmetric Difference (A △ B): Elements in either A or B but not both. A △ B = {1, 2, 6, 7} Complement (A'): All elements in the universal set that are not in A. Cardinality: The number of elements. |A| = 5 Subset: A ⊆ B if every element of A is also in B. Power Set: The set of all subsets of A. If |A| = n, then |P(A)| = 2ⁿ. For A = {1, 2}: P(A) = {∅, {1}, {2}, {1,2}} — has 2² = 4 elements.
Mathematical Foundation
🎨Part A — Designer View (UI Design)
Open MIT App Inventor → Switch to Designer view. Follow each step below to build the interface.
Set up the screen
Title: "Set Theory Visualizer". Dark theme.
Create Set A input
Label: "Set A — Enter elements separated by commas" TextBox named SetAInput, Hint: "1, 2, 3, 4, 5" Width: Fill Parent
Create Set B input
Label: "Set B — Enter elements separated by commas" TextBox named SetBInput, Hint: "3, 4, 5, 6, 7"
Add Universal Set input (for complement)
Label: "Universal Set U (for complement)" TextBox named UniversalInput, Hint: "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
Add operation buttons
Using HorizontalArrangements, create rows of buttons: Row 1: "A ∪ B" (Union), "A ∩ B" (Intersection) Row 2: "A - B", "B - A" Row 3: "A △ B" (Symmetric Diff), "A'" (Complement) Row 4: "Power Set A", "Subset Check" Row 5: "Cardinality"
Add result display
Large ResultLabel to show the output set. InfoLabel below for extra information (cardinality of result, etc.)
🧩Part B — Blocks View (Logic & Calculation)
Switch to Blocks view. Now add the logic that makes your app actually work.
Parse input into lists
Create a procedure "parseSet" that takes TextBox text and returns a list: 1. Split text at "," 2. Trim whitespace from each element 3. Remove duplicates (a set has no duplicates!) 4. Return the clean list Call this for Set A, Set B, and Universal Set.
Build Union
When UnionButton.Click: 1. Parse Set A and Set B 2. Start with result = copy of A 3. Loop through each element in B: If element is NOT in result → add it 4. Display result as: "A ∪ B = {" + join list with ", " + "}" In App Inventor, use "is in list" block to check membership.
Build Intersection
When IntersectionButton.Click: 1. Parse Set A and Set B 2. Create empty result list 3. Loop through each element in A: If element IS in B → add to result 4. Display: "A ∩ B = {" + result + "}"
Build Difference (A - B)
When DiffABButton.Click: 1. Parse Set A and Set B 2. Create empty result list 3. Loop through each element in A: If element is NOT in B → add to result 4. Display: "A - B = {" + result + "}"
Build Symmetric Difference
When SymDiffButton.Click: 1. Calculate A - B (elements in A not in B) 2. Calculate B - A (elements in B not in A) 3. Union those two results 4. Display: "A △ B = {" + result + "}"
Build Complement
When ComplementButton.Click: 1. Parse Set A and Universal Set 2. A' = elements in Universal but NOT in A 3. Loop through Universal: if not in A, add to result 4. Display: "A' = {" + result + "}"
Build Power Set
This is the most advanced logic: For a set with n elements, generate all 2ⁿ subsets. Simple approach for small sets (up to 5 elements): 1. Generate binary numbers from 0 to 2ⁿ - 1 2. Each binary number represents which elements to include 3. For each binary number, create a subset Alternatively, use a recursive approach or limit to showing subsets for sets with ≤ 5 elements. Show warning: "Power set only works for sets with 5 or fewer elements"
Build Subset Check
When SubsetButton.Click: Check if every element of A is in B: 1. Set isSubset = true 2. Loop through A: if any element NOT in B → isSubset = false 3. Display: "A ⊆ B: Yes" or "A ⊆ B: No"
🧪Testing Your App
- ✓A={1,2,3}, B={3,4,5} → Union={1,2,3,4,5}, Intersection={3}
- ✓A={1,2}, Universal={1,2,3,4} → A'={3,4}
- ✓A={1,2}, B={1,2,3} → A⊆B is True
- ✓Power set of {1,2} = {∅, {1}, {2}, {1,2}} — 4 subsets
- ✓Test with duplicate input: {1,2,2,3} should become {1,2,3}
🚀Bonus Challenges
Extra credit — impress your instructor
- ★Draw a simple Venn diagram on Canvas showing A and B
- ★Add De Morgan's Law verification: (A∪B)' = A'∩B'
- ★Support numeric and text elements (e.g., {apple, banana})
- ★Add Cartesian Product: A × B = {(a,b) : a∈A, b∈B}