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

Java Lab 2.1 Worksheet

The document describes an experiment where a student implemented a program to collect and store card details from the user, including the symbol and number. The program stores the card objects in a map with the 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

Saurabh Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Java Lab 2.1 Worksheet

The document describes an experiment where a student implemented a program to collect and store card details from the user, including the symbol and number. The program stores the card objects in a map with the 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

Saurabh Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT 2.

Student Name: Saurabh mishra UID: 19BCS2072


Branch: Computer Science and Engineering Section/Group: CI_3/B

Semester: 6th Date of Performance: 24 march 2022

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;
}

public char getSymbol()


{
return symbol;
}

public void setSymbol(char symbol)


{
this.symbol = symbol;
}

public int getNumber()


{
return number;
}

public void setNumber(int number)


{
this.number = number;
}

public String toString()


{
return "Card [symbol=" + symbol + ", number=" + number + "]";
}

public int compareTo(Card cd)


{
if (this.symbol < cd.symbol)
return -1;
else if (this.symbol > cd.symbol)
return 1;
else
return 1;
}

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;

public class cardStore


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Map<Character, ArrayList<Card>> map = new TreeMap<>();

System.out.println("Enter Number of Cards:");


int n = sc.nextInt();
sc.nextLine();

for (int i = 1; i <= n; i++)


{
System.out.println("Enter card " + i);
char symbol = sc.nextLine().charAt(0);
int number = sc.nextInt();

Card card = new Card();


card.setSymbol(symbol);
card.setNumber(number);
sc.nextLine();

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<Entry<Character, ArrayList<Card>>> set = map.entrySet();


Iterator<Entry<Character, ArrayList<Card>>> it = set.iterator();
while (it.hasNext())
{
System.out.print(it.next().getKey() + " ");
}
System.out.println();

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();

System.out.println("Cards in " + me.getKey() + " Symbol");

for (Card card : list)


{
System.out.println(card.getSymbol() + " " + card.getNumber());
sum += card.getNumber();
}

System.out.println("Number of cards: " + list.size());


System.out.println("Sum of Numbers: " + sum);
}
sc.close();
}
}

Output:

You might also like