public void setValue(int value) { if (value < 1 || value > 13) { throw new IllegalArgumentException("Value " + value + " out of valid 1 - 13 range."); } this.value = value; } public void setSuit(Suit suit) { this.suit = suit; }
Set<PlayingCard> set = new HashSet<PlayingCard>();
PlayingCard card = new PlayingCard(2, PlayingCard.Suit.C);
for (int i = 0; i < 5; i++) {
//add 5 cards to set
set.add(card);
card.setValue(card.getValue() + 1);
}
System.out.println(set); // [7C, 7C, 7C, 7C, 7C] (even w/ correct hashCode & equals)
// as a Set Set<PlayingCard> set = new HashSet<PlayingCard>(); PlayingCard card = new PlayingCard(2, PlayingCard.Suit.S); set.add(card); PlayingCard fourC = new PlayingCard(4, PlayingCard.Suit.C); set.add(fourC); set.add(new PlayingCard(11, PlayingCard.Suit.H)); System.out.println(set); // [JH, 2S, 4C] card.setValue(4); System.out.println(set); // [JH, 4S, 4C] System.out.println(card); // 4S System.out.println(set.contains(card)); // false
/** * Stores mutable PlayingCards where they cannot be affected by others! * Once you put a card into a CardProtector, you can't change its value. * Protect all of your cards today! */ public class CardProtector { private PlayingCard storage; //encapsulated! public CardProtector(PlayingCard card) { this.storage = card; } public PlayingCard getCard() { return this.storage; } @Override public String toString() { return "{" + this.storage + "}"; } }
PlayingCard card = new PlayingCard(2, PlayingCard.Suit.S); CardProtector cp = new CardProtector(card); System.out.println(cp); // {2S} card.setValue(11); System.out.println(cp); // {JS} !!
/**
* Stores mutable PlayingCards where they cannot be affected by others!
* Once you put a card into a CardProtector, you can't change its value.
* Protect all of your cards today!
*/
public class CardProtector {
private PlayingCard storage; //encapsulated!
public CardProtector(PlayingCard card) {
this.storage = new PlayingCard(card.getValue(), card.getSuit()); //make my own copy
}
public PlayingCard getCard() {
return this.storage;
}
@Override public String toString() {
return "{" + this.storage + "}";
}
}
PlayingCard card = new PlayingCard(2, PlayingCard.Suit.S);
CardProtector cp = new CardProtector(card);
System.out.println(cp); // {2S}
card.setValue(11);
System.out.println(cp); // {2S} (ha! foiled)
PlayingCard letMeSee = cp.getCard();
letMeSee.setValue(11);
System.out.println(cp); // {JS} (doh!)
/**
* Stores mutable PlayingCards where they cannot be affected by others!
* Once you put a card into a CardProtector, you can't change its value.
* Protect all of your cards today!
*/
public class CardProtector {
private PlayingCard storage; //encapsulated!
public CardProtector(PlayingCard card) {
this.storage = new PlayingCard(card.getValue(), card.getSuit()); //make my own copy
}
public PlayingCard getCard() {
return new PlayingCard(this.storage.getValue(), this.storage.getSuit());
}
@Override public String toString() {
return "{" + this.storage + "}";
}
}
PlayingCard card = new PlayingCard(2, PlayingCard.Suit.S);
CardProtector cp = new CardProtector(card);
System.out.println(cp); // {2S}
card.setValue(11);
System.out.println(cp); // {2S} (ha! foiled)
PlayingCard letMeSee = cp.getCard();
letMeSee.setValue(11);
System.out.println(cp); // {2S} (ha ha!)
A B C D E F A F F T F F F B T F T F T F C F T F F F F D F T F F F F E T T F F T F F F F F F F F
A| -> C B| -> A -> C -> E C| -> B D| -> B E| -> B -> E F|
You own 25 horses but no watch, clock, or way of measuring time.
You want to know which of your horses is the fastest.
You have a track wide enough to run only 5 horses at a time in a race.
What is the fewest number of races you'd need to run to find the fastest horse? 6
What if you want to find the top two fastest horses? (min # of races?) 7
What if you want to find the top three fastest horses? (min # of races?) 7
How would you implement an LRU cache?