Worksheet 2.2
Worksheet 2.2
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:
import java.util.*;
public Card() {}
super();
this.symbol = symbol;
this.number = number;
}
return symbol;
this.symbol = symbol;
return number;
this.number = number;
else return 1;
}
public int hashCode() {
return String.valueOf(symbol).hashCode();
} else {
return false;
System.out.println("Enter a card:");
card.setSymbol(sc.nextLine().charAt(0));
card.setNumber(sc.nextInt());
sc.nextLine();
set.add(card);
OUTPUT:
Time complexity for this code is O(n)