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

Java J3 2.2

JAVA experiment 2.2

Uploaded by

aryan20031112
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)
14 views

Java J3 2.2

JAVA experiment 2.2

Uploaded by

aryan20031112
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/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment No: 2.2

Name: Jatin UID: 21BCS9605


Branch: CSE Section: 21BCS_FL-604 B
Semester: 6th Date: 16/02/24
Subject: PBLJ Subject Code: 21CSP-351

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

Objective:
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.

Code:
package project1;
import java.util.*;
public class card implements Comparable<card>{
private char symbol;
private int number;
public card() {}
public card(char symbol, int number){
super();
this.symbol = symbol;
this.number = number;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public char getSymbol() {
return symbol;
}
public void setSymbol(char symbol) {
this.symbol = symbol;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}

@Override
public String toString() {
return "Card [symbol=" + symbol + ", number=" + number + "]";
}

@Override
public int compareTo(card o) {
if (this.symbol < o.symbol)
return -1;
else if (this.symbol > o.symbol)
return 1;
else
return 1;
}

@Override
public int hashCode() {
return String.valueOf(symbol).hashCode();
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
@Override
public boolean equals(Object obj){
if (obj instanceof card) {
card card = (card) obj;
return (card.symbol == this.symbol);
}
else{
return false;
}
}
}

class TestMain {
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:");
for (card card: set)
System.out.println(card.getSymbol() + " " + card.getNumber());

sc.close();
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

You might also like