6.170 Recitation - PowerPoint PPT Presentation

About This Presentation
Title:

6.170 Recitation

Description:

6.170 Recitation. Allen Miu. Quiz 1 Review: March 3, 7:30pm, 34-101. Problem Set 3: March 4 ... public void shuffle() { Collections.shuffle(cards); Problem 4: TwoPair ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 19
Provided by: alle4
Learn more at: http://nms.lcs.mit.edu
Category:

less

Transcript and Presenter's Notes

Title: 6.170 Recitation


1
6.170 Recitation
  • Allen Miu

2
Announcements
  • Quiz 1 Review March 3, 730pm, 34-101
  • Problem Set 3 March 4
  • 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 TAs name on problem set
    submissions

3
Problem 5 MDD
4
Object Models
  • Describe relationships between sets of objects
  • Sets (and subsets)
  • Relationships
  • Set and relationship constraints multiplicity
    and mutability

5
Subtype Relationships
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
MDD Example Sets
People
Men
Player
Nice Guy
Monk
9
MDD Example Relationships
People
Women
Men

!
?
girlfriends
Player
Nice Guy
Monk
!
girlfriends
10
MDD Example Blaspheme
People
Women
Men

!

?
girlfriends
Player
Nice Guy
Monk

!
girlfriends
girlfriends
11
Problem 1 CardValue and CardSuit
  1. Static members in CardSuit.
  2. Separate CardSuit class.
  3. CardSuit constructor if (this null)
  4. CardValue CardSuit constructors private
  5. CardValue.equals()
  6. CardValue implement 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 i0 ilt4 i)
  • for (int j0 jlt13 j)
  • cards.add(new Card( (CardValue)(CardValue.VA
    LUES.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.listCards
    AcesHigh(), 2)
  • if ((pairsSet.isEmpty()) (pairsSet.size()
    lt 2)) return null
  • Card firstPair (Card) pairsSet.first()
  • Card secondPair (Card) pairsSet.last()
  • SortedSet discardableCards
    convertToSortedSet(h.listCardsAcesHigh())
  • removeNCardsWithValue(discardableCards.iterato
    r(), 2, firstPair.getValue())
  • removeNCardsWithValue(discardableCards.iterato
    r(), 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
Write a Comment
User Comments (0)
About PowerShow.com