1 / 20

6.170 Recitation

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.

Télécharger la présentation

6.170 Recitation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 6.170 Recitation Godfrey Tan

  2. 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

  3. Problem 5: MDD

  4. Object Models Describe relationships between objects • Subtype • Multiplicity • Mutability

  5. Subtype Relationship subtype (pure) exhaustive exclusive (disjoint)

  6. Multiplicity • Uses field arrow: • M to N relationship: m, n, *, !, ?, +

  7. Mutability • Modifiability of fields: | • At the src • At the target • Modifiability of sets • Cardinality: +, *, !, ? • Migration: dynamic, static, fixed

  8. Women OM Example: Sets People Men Monk Player Nice Guy

  9. OM Example: Relationships People Men Women + ! ? Monk Player Nice Guy girlfriends ! girlfriends

  10. OM Example: Blasphemy People Men Women + ! + ? Monk Player Nice Guy girlfriends + ! girlfriends girlfriends

  11. 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

  12. 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()); }

  13. 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(); }

  14. 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

  15. 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); }

  16. 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; }

  17. 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); }

  18. 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

  19. 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.

More Related