0% found this document useful (0 votes)
32 views

Java 2.1

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Java 2.1

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 com.w3epic.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

import com.w3epic.bean.Card;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public class TestMain {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
Map<Character, ArrayList<Card>> map = new TreeMap<>();

System.out.println("Enter Number of Cards :");


int n = sc.nextInt();
sc.nextLine();

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


System.out.println("Enter card " + n);
char symbol = sc.nextLine().charAt(0);
int number = sc.nextInt();

Card card = new Card();


card.setSymbol(symbol);
card.setNumber(number);
sc.nextLine();

if (!map.containsKey(symbol)) {
ArrayList<Card> list = new ArrayList<>();
list.add(card);
map.put(symbol, list);
} else {
ArrayList<Card> list = map.get(symbol);
list.add(card);
}
}
System.out.println("Distinct Symbols are :");

Set<Entry<Character, ArrayList<Card>>> set = map.entrySet();


Iterator<Entry<Character, ArrayList<Card>>> it = set.iterator();
while (it.hasNext()) {
System.out.print(it.next().getKey() + " ");
}
System.out.println();

set = map.entrySet();
it = set.iterator();

while (it.hasNext()) {
int sum = 0;
Map.Entry<Character, ArrayList<Card>> me = it.next();
ArrayList<Card> list = me.getValue();

System.out.println("Cards in " + me.getKey() + " Symbol");


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

for (Card card : list) {


System.out.println(card.getSymbol() + " " + card.getNumber());
sum += card.getNumber();
}

System.out.println("Number of cards : " + list.size());


System.out.println("Sum of Numbers : " + sum);
}

sc.close();
}

• 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

You might also like