20
6.170 Recitation Godfrey Tan

6.170 Recitation

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: 6.170 Recitation

6.170 Recitation

Godfrey Tan

Page 2: 6.170 Recitation

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

Page 3: 6.170 Recitation

Problem 5: MDD

Page 4: 6.170 Recitation

Object Models

Describe relationships between objects

Subtype Multiplicity Mutability

Page 5: 6.170 Recitation

Subtype Relationship

subtype (pure)

exhaustive

exclusive (disjoint)

Page 6: 6.170 Recitation

Multiplicity Uses field arrow: M to N relationship: m, n, *, !, ?,

+

Page 7: 6.170 Recitation

Mutability Modifiability of fields: |

At the src At the target

Modifiability of sets Cardinality: +, *, !, ? Migration: dynamic, static, fixed

Page 8: 6.170 Recitation

OM Example: SetsPeople

Men

Player Nice Guy

Women

Monk

Page 9: 6.170 Recitation

OM Example: Relationships

People

Women

girlfriends

!

?

girlfriends!

+

Men

Player Nice GuyMonk

Page 10: 6.170 Recitation

OM Example: BlasphemyPeople

Women

girlfriends

!

?

girlfriends!

+

girlfriends

+

+

Men

Player Nice GuyMonk

Page 11: 6.170 Recitation

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

Page 12: 6.170 Recitation

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

Page 13: 6.170 Recitation

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());

}

Page 14: 6.170 Recitation

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

Page 15: 6.170 Recitation

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

Page 16: 6.170 Recitation

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

Page 17: 6.170 Recitation

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

Page 18: 6.170 Recitation

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

Page 19: 6.170 Recitation

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

Page 20: 6.170 Recitation

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.