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

Java2 1

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

Java2 1

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

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 java.util.*;
public class CollectCards {
protected ArrayList<Integer> array;
protected Map<String,ArrayList<Integer>> card;
public CollectCards(){
this.array = new ArrayList<Integer>();
this.card = new HashMap<String,ArrayList<Integer>>();
}
public void addCard(String name,int number) {
if(this.card.containsKey(name)) {
this.card.get(name).add(number);
}else {
this.array.add(number);
this.card.put(name,this.array);
}
}
public void display(){
for(Map.Entry<String,ArrayList<Integer>> card :
this.card.entrySet()){

System.out.println(card.getKey()+"\t"+card.getValue());
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
CollectCards card = new CollectCards();
do{
System.out.println("Enter the card details");
card.addCard(input.next(),input.nextInt());
System.out.println("Want to repeat again!!!");
}while(input.next().equals("Yes"));
card.display();
}
}

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.

You might also like