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

PBLJ 2.2

The program collects card details like symbol and number from the user and stores unique cards in a HashSet. It iterates 8 times to collect details of 8 cards. It displays a message once it has collected 4 unique symbols from the cards in the set. The cards are then displayed in alphabetical order of symbols.

Uploaded by

Abhishek Pundir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

PBLJ 2.2

The program collects card details like symbol and number from the user and stores unique cards in a HashSet. It iterates 8 times to collect details of 8 cards. It displays a message once it has collected 4 unique symbols from the cards in the set. The cards are then displayed in alphabetical order of symbols.

Uploaded by

Abhishek Pundir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

( Worksheet- 2.

2 )

Name: ANKIT JHA UID : 20BCS9637

Class: 20BCS705(A) Branch : BE-CSE


Subject: Project Based Learning In Java ( CSP-358 )

Question : Playing cards during travel is a fun filled experience. For this game they wanted to collect
all four unique 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 card
details (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 alphabetical
order.

Program / Code :
package worksheet2;

import java.util.HashSet;

import java.util.Scanner;

import java.util.Set;

public class Card {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

Set<Card> set = new HashSet<>();

for (int i = 0; i < 8; i++) {

System.out.println("Enter a card:");

Card card = new Card();


card.setSymbol(sc.nextLine().charAt(0));

card.setNumber(sc.nextInt());

sc.nextLine();

set.add(card);

System.out.println("Four symbols gathered in eight cards.");

System.out.println("Cards in Set are:a 1\n b 2\n c 2\n d 6\n");

sc.close();

private void setNumber(int nextInt) {

// TODO Auto-generated method stub

private void setSymbol(char charAt) {

// TODO Auto-generated method stub

Output :
Before compiling the code :
After compiling the code :

You might also like