Java 2.2
Java 2.2
Experiment 2.2
Student Name: Shivam UID: 21BCS7134
Branch: BE-CSE Section/Group: CC-645-B
th
Semester: 6 Date of Performance: 22-02-2023
Subject Name: Project Based Learning in Java with Lab
Subject Code: 21CSP-319
1. Aim: Create a program to collect unique symbol from a set of cards using set interface
2. Algorithm:
Define a Card class:
Create a class named Card with a symbol field.
Implement a constructor to initialize the symbol.
Implement a method to get the symbol.
Main Functionality:
Create a HashSet to store unique symbols.
Iterate through a set of cards.
For each card, add its symbol to the HashSet.
Print the elements of the HashSet to display unique symbols.
3. Code:
import java.util.*;
class Card {
private String symbol;
Card[] cards = {
new Card("Heart"),
new Card("Spade"),
new Card("Diamond"),
new Card("Club"),
new Card("Heart"),
new Card("Diamond")
};
System.out.println("Unique Symbols:");
for (String symbol : uniqueSymbols) {
System.out.println(symbol);
}
}
}
4. Output: