TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009.
TeachScheme, ReachJava
description
Transcript of TeachScheme, ReachJava
![Page 1: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/1.jpg)
TeachScheme, ReachJava
Adelphi University
Friday morning
July 16, 2010
![Page 2: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/2.jpg)
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).
![Page 3: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/3.jpg)
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.
![Page 4: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/4.jpg)
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.
![Page 5: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/5.jpg)
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.
![Page 6: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/6.jpg)
Definition by choices
interface Shape
{
}
…
class Circle implements Shape
{
…
}
![Page 7: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/7.jpg)
interface Shape { }
class Circleimplements Shape
{
Posn center;
double radius;
String color;
…
}
class Rectangle implements Shape
{
Posn topLeft;
double width;
double height;
String color;
…
}
![Page 8: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/8.jpg)
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");
![Page 9: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/9.jpg)
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.
![Page 10: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/10.jpg)
To fix this…
interface Shape
{
public double area ();
public boolean contains (Posn other);
public Shape scale (double factor);
}
![Page 11: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/11.jpg)
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.
![Page 12: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/12.jpg)
time check
![Page 13: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/13.jpg)
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).
![Page 14: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/14.jpg)
Lists in Java
Write classes ESL and NESL, and interface StringList. For each class, provide
• a constructor
• examples
• a toString method
![Page 15: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/15.jpg)
Lists in Java
Write the following methods on StringLists:
• countStrings : nothing -> int
• contains : String -> boolean
• countMatches : String -> int
![Page 16: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/16.jpg)
Recall add-up function
(define (add-up nums)
(cond [(empty? nums) 0]
[(cons? nums)
(+ (first nums)
(add-up (rest nums)))]))
![Page 17: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/17.jpg)
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
![Page 18: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/18.jpg)
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 +
![Page 19: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/19.jpg)
Another approachOf course, add-up-accum is less convenient to use.Easy fix:
(define (add-up nums)(add-up-accum nums 0))
![Page 20: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/20.jpg)
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]))]))
![Page 21: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/21.jpg)
Generalize
Both functions look pretty similar. They differ in
the answer to the "empty?" case, andhow to combine (first nums) with so-far
![Page 22: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/22.jpg)
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
![Page 23: TeachScheme, ReachJava](https://reader035.fdocuments.in/reader035/viewer/2022062804/568148f5550346895db61399/html5/thumbnails/23.jpg)
Are we done yet?
• Fill out end-of-day survey
• Fill out end-of-workshop survey
• Eat
• Go home
• Sleep
• Teach