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

Worksheet 2.1 Java Nitish Garg

The program allows users to enter details of cards including their symbol and number. It stores the cards in a TreeMap with symbol as the key and a list of cards as the value. It then prints the distinct symbols in alphabetical order and for each symbol prints the card details, number of cards, and their sum.

Uploaded by

madhu jha
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)
77 views6 pages

Worksheet 2.1 Java Nitish Garg

The program allows users to enter details of cards including their symbol and number. It stores the cards in a TreeMap with symbol as the key and a list of cards as the value. It then prints the distinct symbols in alphabetical order and for each symbol prints the card details, number of cards, and their sum.

Uploaded by

madhu jha
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

CHANDIGARH UNIVERSITY

UNIVERSITY INSTITUTE OF ENGINEERING


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Submitted By: Piyush Kumar Submitted To:

Subject Name Project Based Learning in Java

Subject Code 20CSP-338

Branch CSE

Semester 5
WORKSHEET – 2.1
Name: Madhu Kumar Jha Branch: B.E CSE
UID: 20BCS2877 Section/Group: 807-B
Date of Submission: 17-10-2022 Subject Name: PBLJ Lab

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:

package com.company.luv2code;

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