200 likes | 317 Views
6.170 Recitation. Godfrey Tan. Announcements. Problem Set 3: March 4 Quiz 1 Review: March 3, 7:30pm, 34-101 Quiz 1 (L1-L10): March 6 during class 54-100 for usernames a*-j* 34-101 for usernames k*-z* http://web.mit.edu/6.170/www/ Section number & TA’s name on problem set submissions.
E N D
6.170 Recitation Godfrey Tan
Announcements • Problem Set 3: March 4 • Quiz 1 Review: March 3, 7:30pm, 34-101 • Quiz 1 (L1-L10): March 6 during class • 54-100 for usernames a*-j* • 34-101 for usernames k*-z* • http://web.mit.edu/6.170/www/ • Section number & TA’s name on problem set submissions
Object Models Describe relationships between objects • Subtype • Multiplicity • Mutability
Subtype Relationship subtype (pure) exhaustive exclusive (disjoint)
Multiplicity • Uses field arrow: • M to N relationship: m, n, *, !, ?, +
Mutability • Modifiability of fields: | • At the src • At the target • Modifiability of sets • Cardinality: +, *, !, ? • Migration: dynamic, static, fixed
Women OM Example: Sets People Men Monk Player Nice Guy
OM Example: Relationships People Men Women + ! ? Monk Player Nice Guy girlfriends ! girlfriends
OM Example: Blasphemy People Men Women + ! + ? Monk Player Nice Guy girlfriends + ! girlfriends girlfriends
Problem 1: CardValue and CardSuit Static members in CardSuit. Separate CardSuit class. CardSuit constructor: if (this == null) … CardValue & CardSuit constructors private CardValue.equals() CardValue implements the Comparable interface
Problem 2: Card public int compareTo(Object o) { if (o == null) throw new NullPointerException(); if (!(o instanceOf Card)) throw new ClassCastException(); Card c = (Card)o; int suitCompare = getSuit().compareTo(c.getSuit()); if (suitCompare != 0) return suitCompare; return getValue().compareTo(c.getValue()); }
Problem 2: Card public boolean equals(Object otherCardObject) { if (!(otherCardObject instanceof Card)) return false; Card otherCard = (Card)otherCardObject; (1) return (value == otherCard.value) && (suit == otherCard.suit); (2) return (this.compareTo(otherCard) == 0); (3) return this.hashCode() == otherCard.hashCode(); }
Problem 3: Deck public Deck() { cards = new ArrayList(52); for (int i=0; i<4; i++) for (int j=0; j<13; j++) cards.add(new Card( (CardValue)(CardValue.VALUES.get(j)), (CardSuit) (CardSuit.SUITS.get(i)))); } CardValue.VALUES.size() instead of 13 CardSuit.SUITS.size() instead of 4
Problem 3: Deck public void removeCard(Card c) { cards.remove(c); } public void removeCard(Card c) { if ((c == null) || !cards.contains(c)) return; cards.remove(c); } ------------------------------------------------------------------------------ public void shuffle() { Collections.shuffle(cards); }
Problem 4: TwoPair public static PokerRanking evaluateHand(Hand h) { if (!validHand(h)) return null; Iterator it = h.listCardsAcesHigh(); SortedSet pairs = PokerRanking.findNOfAKinds(it, 2); if (isValidHand(h) && pairs.size() == 2) { Card pair1 = (Card)pairs.first(); Card pair2 = (Card)pairs.last(); removeNCardsWithValue(it, 2, pair1.getValue()); removeNCardsWithValue(it, 2, pair2.getValue()); SortedSet discard = findNOfAKinds(it, 1); return new TwoPair(pair1, pair2, discard); } else return null; }
Problem 4: TwoPair public static PokerRanking evaluateHand(Hand h) { if (!isValidHand(h)) return null; SortedSet pairsSet = findNOfAKinds(h.listCardsAcesHigh(), 2); if ((pairsSet.isEmpty()) || (pairsSet.size() < 2)) return null; Card firstPair = (Card) pairsSet.first(); Card secondPair = (Card) pairsSet.last(); SortedSet discardableCards = convertToSortedSet(h.listCardsAcesHigh()); removeNCardsWithValue(discardableCards.iterator(), 2, firstPair.getValue()); removeNCardsWithValue(discardableCards.iterator(), 2, secondPair.getValue()); return TwoPair(firstPair, secondPair, discardableCards); }
instanceof v.s. getClass ClassB extends ClassA ClassA a = new ClassA() ClassB b = new ClassB() ClassA ab = new ClassB() a instaneof ClassA b instanceof ClassA a.getClass().equals(b.getClass()) ab instanceof ClassA; ab instanceof ClassB
Designing a Graph ADT What is an ADT? An ADT includes a data structure and a collection of methods which operate on that data structure. • Think of what a graph really is. • What are they used for? • What functionality to provide? Too much; too little • Simplicity: easy to use? • Flexibility: easy to upgrade; easy to maintain? • Implementation Efficiency: easy to implement? • Performance: better to use Sets, Lists, Maps, arrays, etc.? • Familiarize with APIs; think before you code. • Visit me in office hours or send email.