0% found this document useful (0 votes)
13 views4 pages

Exp 2.1

The document describes an experiment to create a program that collects and stores cards into a hashmap data structure grouped by symbol. The program takes in card symbols and numbers from user input, stores them in a hashmap with the symbol as the key and an arraylist of cards as the value, and finally outputs the symbol, number of cards, and sum of all card numbers for each symbol.

Uploaded by

Cross Eks
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)
13 views4 pages

Exp 2.1

The document describes an experiment to create a program that collects and stores cards into a hashmap data structure grouped by symbol. The program takes in card symbols and numbers from user input, stores them in a hashmap with the symbol as the key and an arraylist of cards as the value, and finally outputs the symbol, number of cards, and sum of all card numbers for each symbol.

Uploaded by

Cross Eks
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
You are on page 1/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.1
Student Name: Kaustabh Pal UID: 21BCS8791
Branch: CSE Section/Group: SC-905(B)
Semester: 6th Date of Performance: 22-02-24
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 and to learn about


HashMap.

3. Input/Apparatus:
Hardware Requirements:- Minimum 384MB RAM, 100 GB hard disk,
processor with 2.1 MHz

Software Requirements:- Eclipse, NetBeans, IntelliJ, etc.

4. Procedure:
import java.util.*;

class Card {

String symbol;

int number;

public Card(String symbol, int number) {

this.symbol = symbol;

this.number = number;

}
@Override

public String toString() {

return "(" + symbol + ", " + number + ")";

public class CardCollectionInteractive {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

HashMap<String, ArrayList<Card>> cardCollection = new HashMap<>();

while (true) {

System.out.println("Enter card details (symbol number), or type 'done' to finish:");

String input = scanner.nextLine();

if (input.equalsIgnoreCase("done")) {

break;

String[] parts = input.split(" ");

if (parts.length != 2) {

System.out.println("Invalid input. Please enter in the format 'symbol number'.");

continue;

String symbol = parts[0];

int number;

try {

number = Integer.parseInt(parts[1]);

} catch (NumberFormatException e) {

System.out.println("Invalid number format.");


continue;

addCard(cardCollection, new Card(symbol, number));

for (String symbol : cardCollection.keySet()) {

ArrayList<Card> cards = cardCollection.get(symbol);

int sum = 0;

for (Card card : cards) {

sum += card.number;

System.out.println("Symbol: " + symbol + ", Number of Cards: " + cards.size() + ",


Sum of Numbers: " + sum);

scanner.close();

public static void addCard(HashMap<String, ArrayList<Card>> cardCollection, Card


card) {

if (cardCollection.containsKey(card.symbol)) {

ArrayList<Card> cards = cardCollection.get(card.symbol);

boolean found = false;

for (Card c : cards) {

if (c.number == card.number) {

found = true;

break;

if (!found) {
cards.add(card);

} else {

ArrayList<Card> cards = new ArrayList<>();

cards.add(card);

cardCollection.put(card.symbol, cards);

5. Output:

You might also like