1 / 40

1321

1321. CS. CS1321: Introduction to Programming. Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester. Today’s Menu. I. Administrivia A. TURN IN YOUR ASSIGNMENT B. Note: WebWork is used for Hw5

solana
Download Presentation

1321

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1321 CS

  2. CS1321:Introduction to Programming Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester

  3. Today’s Menu I. Administrivia A. TURN IN YOUR ASSIGNMENT B. Note: WebWork is used for Hw5 II. Introduction to Compound Data (Section 6) A. Introduction B. Demonstration using Graphics! III. Creating your own Structures

  4. Administrivia Homework 4 . . . is now due.

  5. Administrivia Homework 5 will be handed in using the WebWork system. Note the due date and time, on the assignment.

  6. Administrivia Teachpacks. A few students had trouble with teachpacks. Please note: 1) You must install DrScheme completely. 2) You must read directions carefully.

  7. Questions?

  8. Everyone knows what this is…. y 0 X

  9. y So what’s this? 0 X

  10. We can describe information about this location, such as its x & y coordinates. y X=2 Y=5 0 X

  11. So now we start to have many of these points in space. Visually we have no problems keeping track of information about individual points. y X=2 Y=5 0 X

  12. But what happens when we store this information in a computer? Do computers have little grids inside them on which they keep track of points? NO

  13. So let’s say we store stuff textually X = 1 Y = 13 No problems yet, right?

  14. But as our problem grows more complex… X = 1 Y = 13 X = 3 Y = 15 X = 4 Y = 2 X = 27 Y = 1 X = 2 Y = 16 X = 9 Y = 1 We’re still ok, right? We can just follow the X-Y pattern…

  15. But what happens when… X-Values Y-Values X = 1 X = 2 X = 3 X = 4 X = 9 X = 27 Y = 1 Y = 1 Y = 2 Y = 13 Y = 15 Y = 16

  16. Wouldn’t it be easier to group things together? (X = 1, Y = 13) (X = 3,Y = 15) (X = 4,Y = 2) (X = 27, Y = 1) (X = 2, Y = 16) (X = 9, Y = 1)

  17. Structures The whole idea of structures is to group together data that belongs together (such as the x & y coordinates of a point in space). This gives us a single unit that we can shuttle back and forth between functions without fear of losing the meaning of our data.

  18. Example: The Posn A posn is a scheme structure built into Beginning DrScheme. It contains two fields or locations within a posn where data is stored. These fields have names: x & y. So a posn just stores information about a position!

  19. How do I make one? You call on a built-in function: (make-posn <X-value> <Y-value>)

  20. So How Do I Use it? Well, the two fields in a posn are called x & y, so oddly enough…. (posn-x <posn>) & (posn-y <posn>)

  21. So let’s write an Example that uses a posn structure * I’m not going to put in all the Design Recipe comments here. Structures do change what your Design Recipes look like, but we’ll get into that on Thursday.

  22. distance-to-0 function (define (distance-to-0 in-posn) (sqrt (+ (square (posn-x in-posn)) (square (posn-y in-posn)))))

  23. How about the distance between any two points? Recall: Wanted: Distance between points P1 and P2 P1: (10,6) Y = 6 - 2 P2: (4,2) Y X = 10 - 4 __________ Distance = ( X)2 + ( Y)2 X

  24. Using posn Structures(incomplete use of template…) • (define (distance dx dy) • (sqrt (+ (* dx dx) (* dy dy)))) • ;; Contract: between: posn posn -> number • ;; Purpose: find the distance between two points • ;; use sqrt( dx*dx + dy*dy) • ;; Examples: ( 0 0 ) to ( 0 100 ) -> 100 • ;; ( 0 0 ) to ( 100 100 ) -> 141 • ;; Definition: • (define (between a b) • (distance (- (posn-x b) • (posn-x a)) • (- (posn-y b) • (posn-y a))))

  25. Built-in Drawing Tools • (start <x> <y>) ; open a canvas • (draw-solid-line <a> <b> color) • <a> and <b> are posn’s • Color is a symbol • 'white 'yellow 'red 'blue 'green 'black • (draw-solid-rect <a> <w> <h> color) • <w> and <h> are numbers giving the height and width • (draw-circle <a> <r> color) • <r> is a number giving the radius • (draw-solid-disk <a> <r> color) • (stop); close the current canvas

  26. Draw a Simple Tree 100 150 • (define (drawTree x) • (and • (draw-solid-disk • (make-posn 100 100) 50 'green) • (draw-solid-rect • (make-posn 90 150) 20 100 'black))) 100 50 90 20 100

  27. 100 120 100 200 Add Another Tree • (define (drawTree x) • (and • (draw-solid-disk • (make-posn 100 100) 50 'green) • (draw-solid-rect • (make-posn 90 150) 20 100 'black) • (draw-solid-disk • (make-posn 200 120) 50 'green) • (draw-solid-rect • (make-posn 190 170) 20 100 'black)))

  28. Programming Principle • Whenever you are tempted to copy and paste some code: • (draw-solid-rect • (make-posn 200 120) 50 'green) • (draw-solid-rect • (make-posn 190 170) 20 100 'black) • Stop and think: "Is there a better way?" • Let's abstract out the drawing of the tree from its location…

  29. y (y + 50) x 50 (x – 10) 20 100 Draw a Re-Usable Tree ;; tree : posn -> drawing (define (tree p) (and (draw-solid-disk p 50 'green) (draw-solid-rect (offset p (make-posn -10 50)) 20 100 'black))) • ;; offset : posn posn -> posn • ;; take an initial position p and offset delta; return a • ;; new posn whose x and y components are the sums of the • ;; x and y components of p and delta. • (define (offset p delta) • (make-posn (+ (posn-x p) • (posn-x delta)) • (+ (posn-y p) • (posn-y delta))))

  30. Questions?

  31. Making Your Own Structures So posns are pretty useful and all, but they’re kind of boring. Why not create our own structures? There are plenty of things out there that we could represent with structures….

  32. People – First Name, Last Name, Age, Weight Buildings – Name, Address, Height, Residential or Business, Number of occupants CDs – Artist, Genre, Record Company, Number of Songs Or maybe even…

  33. Embarrassing Photographs! Can you believe it? This is one of the other instructors!

  34. Our Structure Should Store… The First Name of the Individual involved The Last Name of the Individual involved The Date the picture was taken And… The Embarrassment Level of that particular photograph

  35. define-struct (define-struct <name of structure> (<first field name> <second field name> … <last field name> ))

  36. We now have the ability to store information about badphotos. NOTE: We have NOT stored any information about any particular photo. All that we’ve done is create a kind of TEMPLATE for how we WILL store info about badphotos. (Hint: that’s foreshadowing for next lecture!)

  37. define-struct buys us some stuff Whenever we define a new struct, we also automatically define a whole slew of functions that we can use to manipulate that new struct. Consider it a function bonus!!

  38. Some function freebies! CONSTRUCTOR: (make-badphoto <first> <last> <date> <level>) * Doesn’t this look kind of similar to make-posn? ACCESSORS: (badphoto-first <badphoto>) (badphoto-last <badphoto>) (badphoto-date <badphoto>) (badphoto-level <badphoto>)

  39. Last but not least: PREDICATES: (badphoto? <thing of unknown type>) * Would it surprise you to know there was a (posn? …) function?

  40. dan Let me give one of these badphotos a name Now, let’s access some data dan dan

More Related