0% found this document useful (0 votes)
7 views2 pages

Java Program for Unique Card Symbols

The document describes an experiment to create a program that collects unique symbols from a set of cards using a set interface. It defines a Card class with a symbol field and methods to initialize and get the symbol. The main functionality iterates through an array of Card objects, adds each symbol to a HashSet to store only unique symbols, and prints the results.

Uploaded by

harshit143singh
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)
7 views2 pages

Java Program for Unique Card Symbols

The document describes an experiment to create a program that collects unique symbols from a set of cards using a set interface. It defines a Card class with a symbol field and methods to initialize and get the symbol. The main functionality iterates through an array of Card objects, adds each symbol to a HashSet to store only unique symbols, and prints the results.

Uploaded by

harshit143singh
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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.2
Student Name: Shivam UID: 21BCS7134
Branch: BE-CSE Section/Group: CC-645-B
th
Semester: 6 Date of Performance: 22-02-2023
Subject Name: Project Based Learning in Java with Lab
Subject Code: 21CSP-319

1. Aim: Create a program to collect unique symbol from a set of cards using set interface

2. Algorithm:
 Define a Card class:
Create a class named Card with a symbol field.
Implement a constructor to initialize the symbol.
Implement a method to get the symbol.

 Main Functionality:
Create a HashSet to store unique symbols.
Iterate through a set of cards.
For each card, add its symbol to the HashSet.
Print the elements of the HashSet to display unique symbols.

3. Code:
import [Link].*;

class Card {
private String symbol;

public Card(String symbol) {


[Link] = symbol;
}

public String getSymbol() {


return symbol;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public class CardGame{


public static void main(String[] args) {
Set<String> uniqueSymbols = new HashSet<>();

Card[] cards = {
new Card("Heart"),
new Card("Spade"),
new Card("Diamond"),
new Card("Club"),
new Card("Heart"),
new Card("Diamond")
};

for (Card card : cards) {


[Link]([Link]());
}

[Link]("Unique Symbols:");
for (String symbol : uniqueSymbols) {
[Link](symbol);
}
}
}

4. Output:

Common questions

Powered by AI

The Card class demonstrates encapsulation by using a private access modifier for its 'symbol' field, thereby restricting direct access from outside the class. It provides a public constructor to initialize the symbol and a public method 'getSymbol' to retrieve the symbol value, allowing controlled access to the symbol property .

The HashSet solves the problem of storing only unique card symbols from a set of card objects. It achieves this by leveraging the hash table implementation which inherently prevents duplicate entries. When attempting to add a symbol, the HashSet checks if the symbol's hash code already exists, thus ensuring only unique symbols are retained and duplicates are discarded .

Using private access for the symbol field in the Card class enhances program robustness by controlling object integrity. This access level prevents direct modification of the symbol outside the class, reducing unintended changes and protecting the data from external corruption. It also allows controlled access and modifications through public methods, fostering encapsulation and reliable object behavior throughout the program .

The CardGame class demonstrates the application of the Set interface by utilizing a HashSet to collect and print unique card symbols. It iterates through an array of Card objects, adds each card's symbol to the HashSet, and then prints the unique symbols stored in the Set. This illustrates the Set’s functionality of disallowing duplicate elements and automatically handling these unique constraints .

Iteration through the Card objects is necessary to access and process each card's symbol. This is because each card's symbol must be retrieved using the getSymbol() method. Once the symbol is retrieved, it can be added to the HashSet to ensure only unique symbols are collected and duplicates are not added .

To sort the unique symbols alphabetically before printing, the program can be modified to store the unique symbols in a TreeSet instead of a HashSet. TreeSet is a part of the Java Collections Framework that maintains elements in sorted (natural order). By replacing HashSet with TreeSet, upon adding elements, they would automatically be sorted, and the output would then reflect this alphabetical order .

The CardGame demonstrates object-oriented design principles by defining a Card class that encapsulates card-related data (the symbol) and operations (constructing and retrieving symbols). The main program logic in CardGame utilizes these objects to process data, illustrating encapsulation and data abstraction. This modular approach reflects object-oriented design, where data and methods are bundled together .

The program can be extended to count occurrences of each unique symbol by using a HashMap instead of a HashSet. The HashMap would map each symbol to an integer count. During iteration, for each card's symbol, the program would check if the symbol exists in the map. If it does, the count would be incremented; if not, the symbol would be added with a count of one. This allows not only storing unique symbols but also tracking their occurrences .

Using a List instead of a Set to store card symbols would mean duplicate symbols could be stored and displayed. The primary feature of a Set is that it only holds unique elements, preventing duplicates. A List, however, allows duplicates, so symbols like 'Heart' or 'Diamond' would appear more than once in the output, thus not fulfilling the requirement to collect unique symbols .

The existing card program could be improved for dynamic input sizes by using a collection data structure that adjusts size dynamically, such as an ArrayList, for storing Card objects instead of a fixed-size array. Additionally, input could be handled through a scanner that reads from console or a file to create Card objects iteratively, thereby allowing for varying numbers of card inputs beyond a fixed array size limit .

You might also like