Java Lab 2.1 Worksheet
Java Lab 2.1 Worksheet
Subject Name: Project Based Learning in Java Lab Subject Code: CSP-358
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 consists 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.
Code Implementation:
package cardPackage;
public class Card implements Comparable<Card>
{
private char symbol;
private int number;
public Card() {}
public Card(char symbol, int number)
{
super();
this.symbol = symbol;
this.number = number;
}
package experiment4;
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 cardPackage.Card;
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();
Output: