Java 2.1
Java 2.1
Experiment No:2.1
• 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 {
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 = map.entrySet();
it = set.iterator();
while (it.hasNext()) {
int sum = 0;
Map.Entry<Character, ArrayList<Card>> me = it.next();
ArrayList<Card> list = me.getValue();
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