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

Java 2.2

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)
9 views

Java 2.2

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
You are on page 1/ 2

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 java.util.*;

class Card {
private String symbol;

public Card(String symbol) {


this.symbol = 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) {


uniqueSymbols.add(card.getSymbol());
}

System.out.println("Unique Symbols:");
for (String symbol : uniqueSymbols) {
System.out.println(symbol);
}
}
}

4. Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy