0% found this document useful (0 votes)
12 views5 pages

Java Program for Card Collection

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.

Uploaded by

Ritika Dhanda
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)
12 views5 pages

Java Program for Card Collection

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.

Uploaded by

Ritika Dhanda
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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment No:2.1

Student Name: Ritika UID:21BCS8217


Branch: CSE Section/Group:NTPP_902_A
Semester: 6 Date of Performance: /02/24
Subject Name: JAVA LAB Subject Code: 21CSH-319

• Aim: Create a program to collect and store all the cards to assist the users in finding all thecards in a given
symbol using Collection interface.

• Objective:
Þ To learn about concept of hashing.
Þ To learn about hashmaps.

• Input/Apparatus Used:
Þ Hardware Requirements: Minimum 384MB RAM, 100 GB hard Disk, processor with
2.1 MHz
Þ Software Requirements: - Eclipse, NetBeans, IntelliJ, etc.

• Algorithm:
1. Define a class Card which initialize a card(symbol, number).
2. Ask the user for total number of cards.
3. Create a Hashmap to store the cards ensuring symbols sorted alphabetically.
4. Ask the user to enter the details of all the cards as symbol and it’s number.
5. Display all the cards, number of same symbol cards and sum of their numbers.

• Code:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public class TestMain {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
Map<Character, ArrayList<Card>> map = new TreeMap<>();

[Link]("Enter Number of Cards :");


int n = [Link]();
[Link]();

for (int i = 1; i <= n; i++) {


[Link]("Enter card " + n);
char symbol = [Link]().charAt(0);
int number = [Link]();

Card card = new Card();


[Link](symbol);
[Link](number);
[Link]();

if (![Link](symbol)) {
ArrayList<Card> list = new ArrayList<>();
[Link](card);
[Link](symbol, list);
} else {
ArrayList<Card> list = [Link](symbol);
[Link](card);
}
}
[Link]("Distinct Symbols are :");

Set<Entry<Character, ArrayList<Card>>> set = [Link]();


Iterator<Entry<Character, ArrayList<Card>>> it = [Link]();
while ([Link]()) {
[Link]([Link]().getKey() + " ");
}
[Link]();

set = [Link]();
it = [Link]();

while ([Link]()) {
int sum = 0;
[Link]<Character, ArrayList<Card>> me = [Link]();
ArrayList<Card> list = [Link]();

[Link]("Cards in " + [Link]() + " Symbol");


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

for (Card card : list) {


[Link]([Link]() + " " + [Link]());
sum += [Link]();
}

[Link]("Number of cards : " + [Link]());


[Link]("Sum of Numbers : " + sum);
}

[Link]();
}

• Result/Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}

• Learning Outcomes:
Þ Learnt to implement code in java language.
Þ Learnt to implement concept of HashMaps.
Þ Learnt about collections framework.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Common questions

Powered by AI

Potential errors in the program include input mismatch exceptions when users enter incorrect data types for cards, null pointer exceptions if operations are attempted on uninitialized objects, and runtime exceptions from invalid inputs. These can be handled by implementing try-catch blocks to manage exceptions gracefully, ensuring user inputs are validated before processing, and using default values or prompts to guide correct data entry .

The Card class functions as a data structure to encapsulate card information, specifically the card symbol and number. It supports the overall functionality by providing a standardized way to handle card data, ensuring that each piece of data is easily accessible through methods like setSymbol and setNumber. The class serves as a fundamental building block for creating card objects that are then stored in the collections for sorting and retrieval .

A TreeMap is used in the Java program instead of a HashMap to maintain the natural order of keys, which are card symbols in this case. This ensures that the symbols are stored and displayed in alphabetical order, facilitating easier data management and readability. A standard HashMap does not guarantee any order, while a TreeMap's automatic sorting can improve user experience by providing a predictable and organized output .

The data processing approach in the Java program is efficient in sorting and grouping cards using TreeMaps and ArrayLists. However, its efficiency can be improved by handling input exceptions better and by reducing complexity when summing numbers or counting cards by leveraging Java Streams for cleaner and potentially faster data processing. Additionally, predefining card objects to prevent repeated symbol character retrieval could streamline the structure .

The key learnings from the algorithm involve managing dynamic data entry and retrieval using collections. Students learn to initialize objects, store data in collections, and efficiently retrieve information based on symbols. The algorithm also demonstrates creating a logical sequence for data input, processing, and output, integrating multiple concepts such as object initialization, collection data management, and iteration—all essential skills in programming .

The program's design enhances practical use by providing a clear structure for storing and retrieving card-related data efficiently. Its use of sorted collections supports organized data handling, which is valuable in applications requiring similar functionality, such as inventory systems. However, the design may limit real-world application as it relies on console input/output, lacks error handling, and the manual data entry method is impractical for large-scale applications. Enhancements such as GUI integration, input validation, and automated data ingestion could improve its applicability .

The choice of programming environment affects development and debugging through features like syntax highlighting, code completion, and integrated debugging tools. For instance, Eclipse, NetBeans, and IntelliJ offer integrated development environments (IDEs) that streamline the coding process by providing these tools, which help in identifying errors, optimizing code, and ensuring efficient program execution. However, the effectiveness varies based on user familiarity and specific project needs .

The Java program demonstrates the use of HashMaps by storing card symbols as keys and lists of card objects as values. This allows easy retrieval and sorting of cards based on their symbols. The program uses a TreeMap, a type of Map that sorts the keys alphabetically, ensuring efficient access and organization of data. Once all cards are inputted, the program retrieves and displays distinct symbols and calculates the sum and count of card numbers for each symbol .

The program introduces students to the Java Collections Framework by requiring them to implement a TreeMap and ArrayList. Students learn how to use methods such as put, get, and containsKey for managing data within a map. Additionally, they experience using Iterators to traverse through collections, reinforcing their understanding of handling list data dynamically in Java .

The program demonstrates object instantiation by creating instances of the Card class, which hold data about card symbols and numbers. Object manipulation is showcased through method calls like setSymbol and setNumber that alter the object's state. This is crucial in demonstrating dynamic data handling, as objects are created and modified based on user input, illustrating core principles of object-oriented programming in Java .

You might also like