0% found this document useful (0 votes)
26 views5 pages

Card Game Data Collection Program

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Card Game Data Collection Program

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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();
[Link] = symbol;
[Link] = number;
}

public char getSymbol()


{
return symbol;
}

public void setSymbol(char symbol)


{
[Link] = symbol;
}

public int getNumber()


{
return number;
}

public void setNumber(int number)


{
[Link] = number;
}

public String toString()


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

public int compareTo(Card cd)


{
if ([Link] < [Link])
return -1;
else if ([Link] > [Link])
return 1;
else
return 1;
}

package experiment4;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class cardStore


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

[Link]("Enter Number of Cards:");


int n = [Link]();
[Link]();

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


{
[Link]("Enter card " + i);
char symbol = [Link]().charAt(0);
int number = [Link]();

Card card = new Card();


[Link](symbol);
[Link](number);
[Link]();

if (![Link](symbol))
{
ArrayList<Card> list = new ArrayList<>();
[Link](card);
[Link](symbol, list);
}
else
{
ArrayList<Card> list = [Link](symbol);
[Link](card);
}
}
[Link]("Distinct Symbols are:");

Set<Entry<Character, ArrayList<Card>>> set = [Link]();


Iterator<Entry<Character, ArrayList<Card>>> it = [Link]();
while ([Link]())
{
[Link]([Link]().getKey() + " ");
}
[Link]();

set = [Link]();
it = [Link]();
while ([Link]())
{
int sum = 0;
[Link]<Character, ArrayList<Card>> me = [Link]();
ArrayList<Card> list = [Link]();

[Link]("Cards in " + [Link]() + " Symbol");

for (Card card : list)


{
[Link]([Link]() + " " + [Link]());
sum += [Link]();
}

[Link]("Number of cards: " + [Link]());


[Link]("Sum of Numbers: " + sum);
}
[Link]();
}
}

Output:

Common questions

Powered by AI

In the Java program, cards are grouped and stored in a TreeMap where each key represents a unique card symbol (characters). Each key maps to an ArrayList of Card objects that share the same symbol. The program first checks if the TreeMap already contains the symbol; if it does, it adds the card to the existing list; otherwise, it creates a new list associated with the symbol .

The program uses a TreeMap to store symbols as keys, which inherently maintains the symbols in their natural order, i.e., alphabetical order for characters. Therefore, when the program iterates over the entries of the TreeMap to print the distinct symbols, they are printed in alphabetical order without additional sorting needed .

To enhance functionality and usability, the program could be updated to handle ties in symbols within the compareTo method to return 0, ensuring accurate card ordering. Error handling and validation could be added for user inputs to handle invalid data gracefully. The design could also be extended to support additional operations like removing a card, searching for cards by number, or exporting the card details to a file for persistence. Another improvement is to implement the comparator for different criteria, like sorting by numbers when symbols are the same .

The program starts by prompting the user to enter the number of cards. For each card, it prompts the user to input the symbol and the number associated with the card. It creates a Card object, sets its symbol and number, and then checks if the symbol is already a key in the TreeMap. If the symbol is not present, it creates a new ArrayList, adds the card to it, and stores it in the TreeMap; otherwise, it retrieves the existing list for that symbol and adds the card to it .

The compareTo method in the Card class is flawed because it always returns 1 when the symbols are equal, instead of returning 0. This means that the method does not correctly adhere to the contract specified by the Comparable interface, where equal objects must return 0. This flaw would lead to incorrect ordering of card objects and may result in duplicates not being handled correctly in sorted collections that rely on this method, such as Trees or ordered lists .

TreeMap is used to store the card objects with the symbol as its key. It automatically orders the entries by the natural ordering of the keys, which in this case are character symbols. This results in cards being grouped and sorted by symbol in alphabetical order, making it easier to print all distinct symbols and their associated cards in a sorted manner. TreeMap is chosen over other Map implementations due to its ordered nature, which HashMap, for instance, does not provide .

The Card class implements the Comparable interface and overrides the compareTo method to allow comparison of card objects. The method compares the symbol of the current card object with another card object passed as an argument. If the current card's symbol is less than the other card's symbol, it returns -1; if greater, it returns 1. However, there is a mistake in the current implementation as it should return 0 for equal symbols instead of 1, which would otherwise hinder proper sorting .

Each card object captures two key pieces of information: a symbol (character) and a number (integer). These are used in the output display to list all card details for each symbol. The program displays the symbol, prints each card's number, the total count of cards for that symbol, and the sum of numbers for those cards .

The program iterates over each entry in the TreeMap, retrieving the list of cards for each symbol. For each list, it counts the number of cards and iteratively sums their numbers. The output format first lists the symbol, then prints each card's number, followed by the total number of cards in that symbol and the total of their numbers .

Overriding the toString method in the Card class provides a custom string representation that includes both the symbol and number of the card. This is significant for debugging and logging purposes, as it allows for a more informative display of card objects when they are printed directly. It makes the output more readable and meaningful .

You might also like