0% found this document useful (0 votes)
24 views6 pages

PBLJ Java 2.2

The document describes an experiment that involves writing a program to collect and store cards by symbol, having the user input the number of cards and each card's symbol and number, and then storing the cards in a map grouped by symbol and printing the distinct symbols, cards, number of cards, and sum for each symbol. The program requires IntelliJ IDEA and a JVM and collects card details from the user in a for loop to populate the map before printing the results.

Uploaded by

Kriti Bharat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

PBLJ Java 2.2

The document describes an experiment that involves writing a program to collect and store cards by symbol, having the user input the number of cards and each card's symbol and number, and then storing the cards in a map grouped by symbol and printing the distinct symbols, cards, number of cards, and sum for each symbol. The program requires IntelliJ IDEA and a JVM and collects card details from the user in a for loop to populate the map before printing the results.

Uploaded by

Kriti Bharat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment - 2.

Student Name:KRITI BHARAT UID:20BCS2055


Branch:CSE Section/Group:601/B
Subject:PBLJ Semester:5th

Aim:

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.

Apparatus Required:

IntelliJ Idea
JVM

Code:

import java.util.*;

public class cards {

public static void main(String[] args){


Scanner input = new Scanner(System.in);
List<Integer> valueList = new ArrayList<Integer>();
TreeMap<String, List<Integer>> mapObj = new TreeMap<String, List<Integer>>();
int totalCards, index, value, sum = 0, count = 0;

System.out.println("ENTER NUMBER OF CARDS : ");


totalCards = input.nextInt();
String symbol;

for(index = 1; index <= totalCards; index++){


System.out.println("ENTER CARD" + " " + index);
symbol = input.next();
value = input.nextInt();

if(mapObj.containsKey(symbol)){
valueList = mapObj.get(symbol);
valueList.add(value);
}else{
valueList = new ArrayList<Integer>();
valueList.add(value);
mapObj.put(symbol, valueList);
}
}

System.out.println("DISTINCT SYMBOLS ARE :");


for(Map.Entry getData : mapObj.entrySet()){
System.out.print(getData.getKey() + " ");

}
System.out.println();

for(Map.Entry getData : mapObj.entrySet()){


System.out.println("CARDS IN " + getData.getKey() + " SYMBOL :");
ArrayList<Integer> temp = (ArrayList<Integer>) getData.getValue();
Iterator itr= temp.iterator();
while(itr.hasNext())
{
count++;
int val = (int) itr.next();
System.out.print(getData.getKey());
System.out.println(" " + val);
sum += val;
}

System.out.println("NUMBER OF CARDS : " + count);


System.out.println("SUM OF NUMBERS : " + sum);
sum = 0;
}
}
}
Output:

You might also like