1 / 23

TeachScheme, ReachJava

TeachScheme, ReachJava. Adelphi University Friday morning July 16, 2010. Class composition. Define a class LogEntry to represent a runner's daily log. It contains the Date of the run, the distance in miles, the time in minutes, and a free-form comment. Include a constructor

ccorbett
Download Presentation

TeachScheme, ReachJava

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. TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010

  2. Class composition Define a class LogEntry to represent a runner's daily log. It contains the Date of the run, the distance in miles, the time in minutes, and a free-form comment. Include • a constructor • several examples • a toString method • an avgSpeed method • an addComment method (which takes in a String and returns a LogEntry just like the old one but with the String added onto whatever comments were already there).

  3. Class composition Define a class Circle to represent a circle on the screen. It contains a center (of type Posn), a radius (double), and a color (String). Include • a constructor • several examples • a toString method • an area method • a contains method that takes in another Posn and returns a boolean indicating whether that Posn is inside the circle • a scale method that takes in a double scaling factor and returns a new Circle like this one but with the radius multiplied by the scaling factor.

  4. Class composition Define a class Rectangle to represent a rectangle on the screen. It contains a top-left corner (of type Posn), a width and height (both double), and a color (String). Include • a constructor • several examples • a toString method • an area method • a contains method that takes in another Posn and returns a boolean indicating whether that Posn is inside the rectangle • a scale method that takes in a double scaling factor and returns a new Circle like this one but with the width and height multiplied by the scaling factor.

  5. Definition by choices Define a data type Shape which is either a Circle or a Rectangle. Since Circle and Rectangle both have constructors, Shape doesn't need one.

  6. Definition by choices interface Shape { } … class Circle implements Shape { … }

  7. interface Shape { } class Circleimplements Shape { Posn center; double radius; String color; … } class Rectangle implements Shape { Posn topLeft; double width; double height; String color; … }

  8. What can you do with this? • A variable of type Shape can hold either a Circle or a Rectangle: Shape shape1 = new Circle(new Posn(3,4),5,"blue"); Shape shape2 = new Rectangle(new Posn (50,20), 30, 40, "orange");

  9. What can't you do with this? shape1.area() doesn't compile! Why not? In Java, every variable has two types: the static type from its declaration, and the dynamic type from what it actually contains. shape1 was declared as a Shape, so that's its static type. Static type is used to decide what's a legal call and what isn't.

  10. To fix this… interface Shape { public double area (); public boolean contains (Posn other); public Shape scale (double factor); }

  11. What can you do with this? Now you can call the area, contains, and scale methods on a Shape variable shape1.area() // should return c. 78.54 shape2.area() // should return 1200 shape1.scale(2.0) // should return// new Circle(new Posn(3,4), 10, "blue") etc.

  12. time check

  13. Lists in Java A StringList is either an EmptyStringList or a NonEmptyStringList (ESL or NESL for short). An ESL has no parts. A NESL has two parts: first (a String) and rest (a StringList).

  14. Lists in Java Write classes ESL and NESL, and interface StringList. For each class, provide • a constructor • examples • a toString method

  15. Lists in Java Write the following methods on StringLists: • countStrings : nothing -> int • contains : String -> boolean • countMatches : String -> int

  16. Recall add-up function (define (add-up nums) (cond [(empty? nums) 0] [(cons? nums) (+ (first nums) (add-up (rest nums)))]))

  17. Trace add-up function (add-up (list 3 5 2 1)) (+ 3 (add-up (list 5 2 1))) (+ 3 (+ 5 (add-up (list 2 1)))) (+ 3 (+ 5 (+ 2 (add-up (list 1))))) (+ 3 (+ 5 (+ 2 (+ 1 (add-up empty))))) ; lots of pending +'s (+ 3 (+ 5 (+ 2 (+ 1 0)))) (+ 3 (+ 5 (+ 2 1)))) (+ 3 (+ 5 3)) (+ 3 8) 11

  18. Another approach (define (add-up-accum nums so-far) (cond [(empty? nums) so-far] [(cons? nums) (add-up-accum (rest nums) (+ (first nums) so-far))])) (add-up-accum (list 3 5 2 1) 0) (add-up-accum (list 5 2 1) (+ 3 0)) (add-up-accum (list 5 2 1) 3) (add-up-accum (list 2 1) (+ 5 3)) (add-up-accum (list 2 1) 8) (add-up-accum (list 1) (+ 2 8)) (add-up-accum (list 1) 10) (add-up-accum empty (+ 1 10)) (add-up-accum empty 11) 11 ; never more than 1 pending +

  19. Another approach Of course, add-up-accum is less convenient to use. Easy fix: (define (add-up nums) (add-up-accum nums 0))

  20. Another example ; multiply-positives : list-of-numbers -> number (define (multiply-positives nums) (mp-accum nums 1)) (define (mp-accum nums so-far) (cond [(empty? nums) so-far] [(cons? nums) (mp-accum (rest nums) (cond [(> (first nums) 0) (* (first nums) so-far)] [else so-far]))]))

  21. Generalize Both functions look pretty similar. They differ in the answer to the "empty?" case, and how to combine (first nums) with so-far

  22. In Java… See projects June26 v1 through June26 v5 v1: addUp written by structural recursion v2: addUp written by accumulative recursion v3: addUp becomes static, in a separate class v4: addUp written using for-each loop v5: addUp written using while-loop

  23. Are we done yet? • Fill out end-of-day survey • Fill out end-of-workshop survey • Eat • Go home • Sleep • Teach

More Related