0% found this document useful (0 votes)
16 views3 pages

Card Collection Program in Java

The document describes an experiment to write a program that collects and stores card details entered by the user. The program takes in the card symbol and number from the user and stores it in a HashMap with the symbol as the key and a list of card numbers as the value. It then prints out the distinct symbols in alphabetical order, along with the number of cards and their sum for each symbol. The aim is to group the cards by symbol for easy retrieval. The steps involve creating a HashMap to store the cards, a method to add cards to the map, and a display method to print the mapped data.

Uploaded by

Sumit Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Card Collection Program in Java

The document describes an experiment to write a program that collects and stores card details entered by the user. The program takes in the card symbol and number from the user and stores it in a HashMap with the symbol as the key and a list of card numbers as the value. It then prints out the distinct symbols in alphabetical order, along with the number of cards and their sum for each symbol. The aim is to group the cards by symbol for easy retrieval. The steps involve creating a HashMap to store the cards, a method to add cards to the map, and a display method to print the mapped data.

Uploaded by

Sumit Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXPERIMENT -2.

Name : Sumit Kumar

UID : 19BCS2141

Subject name : PROJECT BASED LEARNING IN JAVA LAB

Subject code : CSP - 358

Section : 19_NTPP_CI_2_A

Date of Submission : 18-03-2022

AIM:
1. Write a program to collect and store all the cards to assist the users in
finding all the cards in a given symbol.
This cards game consist of N number of cards. Get N number of cards
details from the user and store the values in Card object with the attributes
symbol and number.
Store all the cards in a map with symbol as its key and list of cards as its
value. Map is used here to easily group all the cards based on their symbol.
Once all the details are captured print all the distinct symbols in
alphabetical order from the Map. For each symbol print all the card details,
number of cards and their sum respectively.

STEPS FOR EXPERIMENT/PRACTICAL/CODE:

1. import [Link].*;
public class CollectCards {
protected ArrayList<Integer> array;
protected Map<String,ArrayList<Integer>> card;
public CollectCards(){
[Link] = new ArrayList<Integer>();
[Link] = new HashMap<String,ArrayList<Integer>>();
}
public void addCard(String name,int number) {
if([Link](name)) {
[Link](name).add(number);
}else {
[Link](number);
[Link](name,[Link]);
}
}
public void display(){
for([Link]<String,ArrayList<Integer>> card :
[Link]()){

[Link]([Link]()+"\t"+[Link]());
}
}
public static void main(String[] args){
Scanner input = new Scanner([Link]);
CollectCards card = new CollectCards();
do{
[Link]("Enter the card details");
[Link]([Link](),[Link]());
[Link]("Want to repeat again!!!");
}while([Link]().equals("Yes"));
[Link]();
}
}

LEARNING OUTCOMES:

1. Understand the concept Hash table .


2. Understand the concept of Hash map.
3. Able to solve all related question .

RESULT /OUTPUT/SUMMARY:
1.

Common questions

Powered by AI

The primary learning outcomes highlighted in the CollectCards experiment include understanding the concept of hash tables, comprehending how hash maps function, and developing the ability to solve related programming questions . These outcomes support skill development in Java programming by enhancing knowledge of essential data structures like HashMap, which are fundamental in managing and organizing data efficiently. Such skills are crucial for writing effective and efficient code, particularly in applications that require dynamic data manipulation and retrieval . These learning goals align with practical skill acquisition, providing both theoretical understanding and implementation experience.

The use of a map data structure, specifically a HashMap, is significant in the card game program as it allows for efficient organization of card information according to symbols. By using symbols as keys, the map can quickly be queried to retrieve all cards associated with a particular symbol, thus enabling organization and access in constant time complexity . Furthermore, maps facilitate grouping cards under their respective symbols, which simplifies tasks such as printing distinct symbols and associated card information in alphabetical order. This structure is particularly beneficial in handling operations that deal with large datasets, where efficiency in data retrieval and grouping is crucial .

Printing distinct symbols in alphabetical order is significant as it affects how data is stored and retrieved. This requirement necessitates considering data structures that support efficient ordering, such as TreeMaps, which naturally store keys in sorted order, simplifying retrieval processes . Using a HashMap, as in the current program, requires additional steps, such as sorting keys after insertion, which impacts performance and complexity. This design choice influences how the program processes data, balancing between raw performance and functionality, such as easy ordering. Ensuring this requirement aligns with output needs ultimately informs the choice of algorithms and data structures, underlining the importance of understanding trade-offs in program design .

Potential improvements to the CollectCards program could include implementing a GUI to enhance user interaction and making the program more intuitive. Additionally, a feature to remove cards or search for a specific card by its number could increase functionality . Implementing a sorting algorithm to automatically sort cards within each symbol's list might improve the presentation of data. Furthermore, incorporating error handling to manage invalid inputs more gracefully could enhance robustness . These enhancements could increase the program's usability and efficiency, providing a more comprehensive tool for managing card collections.

The CollectCards program promotes understanding of hash tables and hash maps by providing a practical application where these data structures are essential to solving the problem of organizing and retrieving card data based on symbols. By implementing a map to store card lists, beginners can see firsthand how hash maps work in efficiently associating keys with values . The program's design involves key operations like adding, checking, and iterating entries, which are fundamental interactions that demonstrate the practicality and efficiency of hash tables. This hands-on experience reinforces theoretical concepts, making them easier to comprehend and apply in real-world coding scenarios .

The CollectCards program utilizes Java collections like ArrayList and HashMap to manage card information. Initially, it creates an ArrayList to store the card numbers and a HashMap to keep the symbol as the key and the list of card numbers as the value . During card addition, if the symbol already exists in the map, the program appends the new number to the existing list; if not, it creates a new entry with the symbol and a list containing the card number . This allows efficient grouping and retrieval of cards based on symbols, enabling the program to print distinct symbols in alphabetical order along with their card details . Overall, this approach leverages the dynamic nature of ArrayLists and the associative capabilities of HashMaps to manage and retrieve information effectively.

The CollectCards program demonstrates several OOP principles, notably encapsulation and abstraction. Encapsulation is exhibited through the use of private or protected class members, such as the array and card map, which are not directly accessible from outside the class. Instead, these variables are manipulated through public methods, like addCard and display, ensuring controlled access and modification . Abstraction is implemented by the way the class hides complex operations behind simple interfaces. Users interact with a straightforward API (addCard and display methods) without needing to understand the underlying implementation details, such as how cards are stored and retrieved in a map or array list . This separation of interface from implementation details embodies abstraction and maintains the integrity of the class design.

The CollectCards program captures card details in a loop where the user inputs the card's symbol and number, which are then stored in a map if the symbol is new, or appended to the list associated with an existing symbol . To ensure display in alphabetical order, the program can utilize the natural ordering of keys in a TreeMap (potential improvement), but in its current form, it relies on manual sorting or using Java's collection utilities to sort the keys before displaying . During the display phase, the program iterates over the sorted keys and retrieves associated lists for output, ensuring both symbol order and card details are accurately presented.

Using an ArrayList offers several advantages over a traditional array for storing card numbers. ArrayLists are dynamic in size, meaning they can grow as more card numbers are added, which eliminates the need to define the size upfront as with arrays . This flexibility is beneficial in situations where the number of cards is determined at runtime. Furthermore, ArrayLists provide methods like add(), remove(), and contains() which simplify list operations. However, potential limitations include slightly slower performance due to the overhead of dynamic resizing and internal mechanisms when compared to arrays, which are more memory efficient since they lack this overhead .

Beginners might face challenges such as understanding the correct usage of maps, managing dynamic input efficiently, and ensuring the program correctly categorizes and retrieves card data. A common confusion might arise from initializing and manipulating data structures correctly in Java . To overcome these challenges, beginners can be encouraged to break down the problem into smaller tasks, such as first understanding how to input and store data using simpler structures, and then progressing to maps. Pair programming or seeking mentorship can also be beneficial, allowing learners to gain insights through collaboration. Additionally, studying example code and practicing similar problems can reinforce the concepts needed for successful implementation .

You might also like