Recitation - PowerPoint PPT Presentation

About This Presentation
Title:

Recitation

Description:

Project to be posted right after Spring Break. PS3/4 Spec ... PS3/4 Rep Exposure. Class Graph { HashMap nodes; // key=node, ... PS3/4 Object Models. Problem OM ... – PowerPoint PPT presentation

Number of Views:270
Avg rating:3.0/5.0
Slides: 23
Provided by: alle4
Learn more at: http://nms.lcs.mit.edu
Category:
Tags: ps3 | recitation

less

Transcript and Presenter's Notes

Title: Recitation


1
Recitation 8
  • Godfrey Tan

2
Administrivia
  • Please be succinct in your answers!
  • PS5 due next Tuesday
  • PS6 due Tuesday after Spring Break
  • Form Project groups!
  • 3-4 people
  • Email me by next Tuesday
  • Project to be posted right after Spring Break

3
PS3/4 Spec
  • Never include implementation detail in the spec
    for public interfaces
  • adds Edges to the HashMap
  • Be specific.
  • // removes edge connecting Nodes src and dst
  • void removeEdge(Node src, Node dst)

4
PS3/4 Rep Exposure
  • Class Graph
  • HashMap nodes // keynode, valueedge list
  • List getEdges(Node n)
  • return (List) nodes.get(n) // die die
  • List getEdges(Node n)
  • return
  • Collections.unmodifiableList((List)
    nodes.get(n))

5
PS3/4 Object Models
  • Problem OM
  • NO implementation details indicate WHAT abstract
    data are maintained
  • Sets reflect the abstract data (say Nodes and
    Edges)
  • Draw by examining specifications
  • Code OM
  • Refinement of POM
  • Indicate important implementation details about
    HOW you maintain data (like Hashtable, ArrayList,
    )
  • Draw by examining source code

6
Notions of Equivalence
  • Behaviorally Equivalent
  • If there is no sequence of (mutative) operations
    that can distinguish them
  • Observationally Equivalent (similar)
  • If no difference can be observed through observer
    operations

7
Behaviorally Equivalent equals
  • Mutable types
  • Can only be equals to itself
  • StringBuffer s new StringBuffer(thing)
  • StringBuffer t new StringBuffer(thing)
  • s.equals(t) ?
  • Immutable types
  • If all the fields in both objects are equals
  • String a thing String b thing
  • a.equals(b) ?

8
Observationally Equivalent similar
  • Mutable types
  • If all the fields in both objects are similar
  • StringBuffer s new StringBuffer(thing)
  • StringBuffer t new StringBuffer(thing)
  • s.similar(t) ?
  • Immutable types
  • If all the fields in both objects are similar
  • String a thing String b thing
  • a.similar(b) ?

9
Implementation
  • public class MyObject extends Object
  • MyObject(boolean mutable)
  • ismutable mutable
  • boolean equals(Object other)
  • if(ismutable)
  • // ?
  • else
  • // ?

10
Implementation
  • public class MyObject extends Object
  • MyObject(boolean mutable)
  • ismutable mutable
  • boolean equals(Object other)
  • if(ismutable)
  • return super.equals(other)
  • else
  • return similar(other)

11
Implmentation
  • public class MyObject
  • boolean similar(Object other)
  • // check whether each field is
  • // similar by value and return the
  • // conjunction () of the results.

12
Implementation
  • public class MyObject extends Object
  • MyObject(boolean mutable)
  • ismutable mutable
  • int hashCode()
  • if(ismutable)
  • // ?
  • else
  • // ?

13
Implementation
  • public class MyObject extends Object
  • MyObject(boolean mutable)
  • ismutable mutable
  • int hashCode()
  • if(ismutable)
  • return super.hashCode()
  • else
  • // sum of each fields hashcodes

14
Remember!
  • a.equals(b) ?
  • a.hashCode() b.hashCode()
  • This must be satisfied even when all the fields
    are primitive types

15
Weakest Precondition
  • For each statement S with pre-assertion P
  • Assume post-assertion Q is true
  • (Substitute S in Q) wp(S,Q)
  • Check P ? wp(S,Q)
  • If false, we have trouble!
  • Example
  • Pre-assertion P y 0
  • Code S x y 1
  • Post-assertion Q x 1

16
Proof
  • Example
  • Pre-assertion P y 0
  • Code S x y 1
  • Post-assertion Q x 1
  • Assume x 1
  • Substitute x y 1
  • y 1 1
  • Check y 0 ? y 1 1
  • TRUE

17
Loops P while b S Q
  • Unrolled-loop analysis hard
  • Solution find a loop invariant I s.t.
  • Base Case P ? I
  • For each iteration where b is true
  • after b and S executed, I still holds
  • When b is false, loop terminates
  • I still true and Q is true

18
Example
  • // remove all elements in the list equal
  • // to the first element
  • int i 1
  • while(i
  • if( list.get(i).equals(list.get(0)) )
  • list.remove(i)
  • else
  • i
  • What is the loop invariant?

19
Example
  • // remove all elements in the list equal
  • // to the first element
  • int i 1
  • while(i
  • if( list.get(i).equals(list.get(0)) )
  • list.remove(i)
  • else
  • i
  • What is the loop invariant?
  • for all 0 list 0

20
PS5
  • Build a back-end for a Foliotracker application
    and a test application
  • For PS6, you will have to build a front-end GUI

21
Components
  • Portfolio
  • Identified by name
  • Contains stock positions
  • Stock Position
  • Identified by ticker symbol
  • Contains of shares
  • Price at which stock is bought (optional)

22
Requirements
  • Must support multiple portfolios
  • Data must be persistent
  • Flexible so that a front-end application can use
    your API easily
  • Get price for a stock
  • polling vs callback vs on-demand
  • Modifying Stock Positions (buy/sell)
Write a Comment
User Comments (0)
About PowerShow.com