CSC323 2010S, Class 04: Talking About Objects: Using UML Overview: * Python Followup. * Beautiful Code: Multidimensional Arrays in NumPy. * Thinking about object-oriented programming. * The UML: * What and why. * Classes and interfaces. * Processes and computation. * Q&A. Admin: * Beautiful Code reading for Tuesday: Chapter Four: Finding Things * Other reading for Tuesday: Chapter one of Head First OOAD. * I'm going to try a mostly-lecture format for our exploration of today's beautiful code. Let me know what you think. * Thursday Extras today at 4:30 in 3821 * EC: Friday's CS Table: Pair Programming. Python Followup * Two tasks: Binary Search and Rationals * Why? * Practice programming in Python * Some encouragement to explore a language * Gives me an opportunity to talk about things related to these problems * How did you go about writing binary search? * Thought about it in English, and then translated those English statements to Python statements * Thought about recursive Binary Search and then translated those thoughts to Python * Thought about max/min/mid in English and then translated those thoughts to Python * Write test suites * Dug out my Scheme/Java/C code from last semester and translated that code * What should your test suite look like? * Make a few arrays of even and odd size * Search for each element in those arrays * Try a few things that aren't in those arrays * Make a few sorted arrays of random numbers in some range * Try a variety of numbers and READ THE OUTPUT * Try a few sample searches that came to mind * Some things that sounded good * Automated and systematic * Some things that sounded bad * READ THE OUTPUT! * The traditional systematic test of binary search For each array size from 0 to 32 Build an array of even integers, starting at 0 For each i in the range 0 .. size Make sure that binary search returns index i for 2*i For each i in the range 0 .. size Make sure that binary search return -1 for 2*i+1 Make sure that binary search returns -1 for -1 * See examples Beautiful Code: Iterating NumPy's Multidimensional arrays Background: * NumPy is a library for numerical processing in Python * Lots of numeric tasks need multidimensional arrays * Key issue in multidimensional arrays: You need to step through the elements in the array * The folks who are writing NumPy, to provide fast operations (C) * The clients who are writing code to use NumPy (Python) Background: Represent Arrays * Given a flattened row-major order array, it's easy to find the index of anything * See formulas on whiteboard or outline Design Issue: Suppose you want to support slicing in multidimensional arrays. * Slicing: Taking a part of an array * Starting and ending index for each dimension * A stride for each dimension * Two likely ways to represent a sliced array * Copy: With a new array * Share: With a link to the old array and information about the slicing * Computation of the position of a value is a bit more complicated, but still straightforward. (multiply by stride then add starting offset) * Advantages/disadvantages? * Copy/A: Slightly easier to compute the index * Copy/D: Slicing is computationally intensive and uses lots of memory * Stride/D: Careless implementations can create slow chains of slices (so we hope implementations are not careless) * Stride/AD: Making changes to the copy changes the original * The "aliasing" problem * One strategy: We could make slices immutable * Stride/D: 'Small' arrays may not have local referencesa * Decision: Sharing is good On to iteration * Iteration of a non-sliced array is trivial-It's the same as iterating a one-dimensional array for (i = 0; i < len; i++) { dosomethingwith(*array) array++ } * Iteration of a sliced array is straightforward: You need to keep track of the current position, and update when you reach the end of the last dimension Where's the beauty in obvious, straightforward code? * Using an iterator object to encapsulate the position of the iteration is incredibly useful * Old-style iteration: Copy and paste the sample iteration code, that we've slaved over Fill in dosomethingwith * New-style iteration, using the iterator Iterator i = new Iterator(array) while (!i.atEnd()) { dosomethingwith(i.next()) } * We would have wanted to create iterators anyway, to make life easier for the Python programmers Would there be another approach to the "copy and paste" problem? * We could use a higher-order procedure On to The UML * A visual language for describing object-oriented systems * Visual: Lots of pictures, a few words * We like to communicate visually, and often do so more efficientlya * Language: Syntax and semantics * Developed relatively early in the days of industry * Problem: Each big-name in OO Design used different pictures * Solution: Agree upon a single design * Hire all the big names at one firm (Rational) * Put it out into a larger arena where many firms can agree * The *Unified* Modeling Language * Used in three primary ways * Sketching: A bit of information about some part of the system * Not all the details * But enough to convey important information * Blueprinting: Detailed enough information about a system that a lower-level programmer can implement it * Programming * Used at various points in the development of a system * Prospectively * Retrospectively * The UML is a language: It needs a formal spec to avoid ambiguity * The formal spec is HARD to understand * The UML is LARGE: It covers every aspect of ooad * We will use the UML for * Sketching * Classes/interfaces and their relationships * Processes: sequences of events You're describing a single class: What kinds of things do you expect to need to describe (include issues with relationships to other classes) Consider the Dinosaur class * Class hierarchy: It is a subclass of the Ancient Reptiles class and may be a superclass of other animals * It has some fields (e.g., eating habits) * Many of these fields are other classes of objects * It has some methods (e.g., roar) * It may send messages to other objects (e.g., "i_ate_you(puny_human)") Class hierarchies are not usually as simple as "we have some classes and some subclasses" * There are interfaces * There are abstract classes * And to make ourselves feel superior, we use fancy notation to help distinguish all of this