6.170 Recitation
-
Upload
fritz-oconnor -
Category
Documents
-
view
28 -
download
3
description
Transcript of 6.170 Recitation
6.170 Recitation
Godfrey Tan
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
Announcements
Problem 5: MDD
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
OM Example: SetsPeople
Men
Player Nice Guy
Women
Monk
OM Example: Relationships
People
Women
girlfriends
!
?
girlfriends!
+
Men
Player Nice GuyMonk
OM Example: BlasphemyPeople
Women
girlfriends
!
?
girlfriends!
+
girlfriends
+
+
Men
Player Nice GuyMonk
MDD OM
Defines Structure of a program(static: compile time)
Abstract state of a program(dynamic: runtime)
Composed of Nodes and arcs Nodes and arcs
A Node represents
Program module A named set of data items
An arc represents
Dependecy between modules
Relation between sets
What not Does not capture the state of a program
Sets are not types. OM may not accurately reflect the modular structure of the program.
Useful When errors are detected; when changes need to be made
Can help check the correctness of the program.
Document class Design Specification Requirements Specification
Static members in CardSuit.Separate CardSuit class. CardSuit constructor: if (this == null) … CardValue & CardSuit constructors
private CardValue.equals() CardValue implements the Comparable
interface
Problem 1: CardValue and CardSuit
Problem 2: Cardpublic 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: Cardpublic 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: Deckpublic 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 13CardSuit.SUITS.size() instead of 4
Problem 3: Deckpublic 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: TwoPairpublic 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: TwoPairpublic 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. getClassClassB extends ClassAClassA a = new ClassA()ClassB b = new ClassB()ClassA ab = new ClassB()a instaneof ClassAb instanceof ClassAa.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.