Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 1321. CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester."— Presentation transcript:

1 CS 1321

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…. 0 X y

9 0 X y So what’s this?

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

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

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 = 1 X = 2 X = 3 X = 4 X = 9 X = 27 X-Values Y-Values 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 )

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

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: P2: (4,2) P1: (10,6)  Y = 6 - 2  X = 10 - 4 X Y __________ Distance =  (  X) 2 + (  Y) 2 Wanted: Distance between points P1 and P2

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 ) ; open a canvas (draw-solid-line color) – and are posn’s –Color is a symbol 'white 'yellow 'red 'blue 'green 'black (draw-solid-rect color) – and are numbers giving the height and width (draw-circle color) – is a number giving the radius (draw-solid-disk color) (stop); close the current canvas

26 Draw a Simple Tree (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 150 20 100

27 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))) 100 120 200

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 Draw a Re-Usable Tree ;; 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)))) y x 50 (x – 10) (y + 50) 20 100 ;; 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)))

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

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

39 Last but not least: PREDICATES: (badphoto? ) * Would it surprise you to know there was a (posn? …) function?

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


Download ppt "CS 1321. CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester."

Similar presentations


Ads by Google