0% found this document useful (0 votes)
20 views3 pages

Harsh Java 2.2

The document describes an experiment where a student named Harsh Jai created a program to collect unique symbols from a set of cards using a set interface. The program prompts the user to enter card details like the symbol and number, adds each card to a HashSet, and collects the first occurrence of cards in alphabetical order once 4 unique symbols have been entered. The output shows the symbols and numbers of the cards collected.
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)
20 views3 pages

Harsh Java 2.2

The document describes an experiment where a student named Harsh Jai created a program to collect unique symbols from a set of cards using a set interface. The program prompts the user to enter card details like the symbol and number, adds each card to a HashSet, and collects the first occurrence of cards in alphabetical order once 4 unique symbols have been entered. The output shows the symbols and numbers of the cards collected.
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/ 3

Experiment 2.

Student Name: Harsh Jai UID: 21BCS2393


Branch: BE-CSE Section/Group: 609-A
Semester: 6 Date of Performance:27/02/24
Subject Name: Java Lab
Subject Code:21CSH-319

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

2. Objective: Playing cards during travel is a fun filled experience. For this game
they wanted to collect all fourunique symbols. Can you help these guys to collect
unique symbols from a set of cards?Create Card class with attributes symbol and
number. From our main method collect each carddetails (symbol and number)
from the user.Collect all these cards in a set, since set is used to store unique
values or objects.Once we collect all four different symbols display the first
occurrence of card details in alphabeticalorder.

3. Algo. /Approach and output:

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

class Card {

String symbol;
int number;
public Card(String symbol, int number) {
this.symbol = symbol;
this.number = number;
}
}
public class CardGame2 {
public static void main(String[] args) {
collectUniqueSymbols();
}
public static void collectUniqueSymbols() {
Scanner scanner = new Scanner(System.in);
Set<Card> cards = new HashSet<>();
Set<String> symbolsCollected = new HashSet<>();
while (symbolsCollected.size() < 4) {
System.out.println("Enter a card:");
String symbol = scanner.nextLine().trim().toLowerCase();
int number = scanner.nextInt();
scanner.nextLine();
if (!symbolsCollected.contains(symbol)) {
symbolsCollected.add(symbol);
}
cards.add(new Card(symbol, number));
}
System.out.println("Four symbols gathered in " + cards.size() + "
cards.");
System.out.println("Cards in Set are:");
Set<Card> sortedCards = new TreeSet<>((c1, c2) -> {
if (!c1.symbol.equals(c2.symbol)) {
return c1.symbol.compareTo(c2.symbol);
}
return Integer.compare(c1.number, c2.number);
});
sortedCards.addAll(cards);
for (Card card : sortedCards) {
System.out.println(card.symbol + " " + card.number);
}

scanner.close();
}
}
Output:

You might also like