Experiment 2.
Student Name: Hitesh Tiwari UID: 21BCS8718
Branch: BE-CSE Section/Group: SC-901-A
Semester: 6 Date of Performance: 15.02.2024
Subject Name: Java Lab Subject Code: 21CSH-319
1. Aim: Create a program to collect and store all the cards to assist the users in
finding all the cards in a given symbol using Collection interface.
2. Objective:
• To learn about concept of Hashing.
• To learn about HashMap.
3. Algorithm:
Here's the algorithm for the given Java code to collect and store cards by symbol:
1. **Class Definition**:
- Define a class named `CardCollector`.
2. **Data Structure**:
- Declare a `Map` named `cards` to store cards, where keys are symbols (strings)
and values are lists of cards (strings).
3. **Constructor**:
- Define a constructor `CardCollector()` to initialize the `cards` map.
4. **Adding Cards**:
- Create a method `addCard(String symbol, String card)`:
- If the symbol is not present in the `cards` map:
- Create a new list for the symbol and put it into the `cards` map.
- Add the card to the list of cards associated with the given symbol.
5. **Finding Cards**:
- Create a method `findCards(String symbol)`:
- Retrieve the list of cards associated with the given symbol from the `cards`
map.
- If the symbol is not found, return an empty list.
6. **Main Method**:
- In the main method:
- Create an instance of `CardCollector`.
- Add some cards to the collector using the `addCard` method.
- Specify a symbol that you want to find cards for using the `findCards` method.
- Print out all the cards found for the specified symbol.
This algorithm outlines the steps for collecting and storing cards by symbol using a
`Map` in Java. It provides methods to add cards to the collection and to retrieve
cards by symbol.
4. Source Code:
import java.util.*;
public class CardCollector {
private Map<String, List<String>> cards;
public CardCollector() {
cards = new HashMap<>();
}
public void addCard(String symbol, String card) {
if (!cards.containsKey(symbol)) {
cards.put(symbol, new ArrayList<>());
}
cards.get(symbol).add(card);
}
public List<String> findCards(String symbol) {
return cards.getOrDefault(symbol, new ArrayList<>());
}
public static void main(String[] args) {
CardCollector collector = new CardCollector();
collector.addCard("hearts", "Ace of Hearts");
collector.addCard("hearts", "King of Hearts");
collector.addCard("spades", "Queen of Spades");
collector.addCard("diamonds", "Jack of Diamonds");
collector.addCard("clubs", "10 of Clubs");
String symbolToFind = "hearts";
List<String> foundCards = collector.findCards(symbolToFind);
System.out.println("All cards with symbol '" + symbolToFind + "':");
for (String card : foundCards) {
System.out.println(card);
}
}
}
5. Output:
6. Learning Outcomes:
• Demonstrates efficient use of data structures like Map for grouping data.
• Illustrates dynamic input handling and processing.
• Highlights importance of iterating over collections for data manipulation.
• Encourages encapsulation and object-oriented design principles with Card class.