From Scheme to Java So far, we ve translated data definitions: ; A snake is ; (make-snake sym num sym) (define-struct snake (name weight food)) class Snake { String name; double weight; String food; Snake(String name, double weight, String food) { this.name = name; this.weight = weight; this.food = food; 1
Functions in Scheme ; A snake is ; (make-snake sym num sym) (define-struct snake (name weight food)) ; snake-lighter? : snake num -> bool ; Determines whether s is < n lbs (define (snake-lighter? s n) (< (snake-weight s) n)) (snake-lighter? (make-snake Slinky 10 rats) 10) (snake-lighter? (make-snake Slimey 5 grass) 10) 2
Functions in Java class Snake { String name; double weight; String food; Snake(String name, double weight, String food) { this.name = name; this.weight = weight; this.food = food; // Determines whether it s < n lbs return this.weight < n; new Snake("Slinky", 10, "rats").islighter(10) 3
Functions in Java class Snake { String name; double weight; String food; Snake(String name, double weight, String food) { this.name = name; this.weight = weight; A function this.food = food; becomes a method that is in the class // Determines whether it s < n lbs return this.weight < n; new Snake("Slinky", 10, "rats").islighter(10) 4
Methods in Java Comparing just the function and method: ; snake-lighter? : snake num -> bool ; Determines whether s is < n lbs (define (snake-lighter? s n) (< (snake-weight s) n)) // Determines whether it s < n lbs return this.weight < n; 5
Methods in Java Comparing just the function and method: ; snake-lighter? : snake num -> bool ; Determines whether s is < n lbs (define (snake-lighter? A method s n) in (< (snake-weight s) Snake n)) has an implicit Snake this // Determines whether argument it s < n lbs return this.weight < n; 6
Methods in Java Comparing just the function and method: ; snake-lighter? : snake num -> bool All other ; Determines whether s is < n lbs arguments are (define (snake-lighter? s n) explicit, and the (< (snake-weight s) n)) type is next to the name, as in double n // Determines whether it s < n lbs return this.weight < n; 7
Methods in Java Comparing just the function and method: ; snake-lighter? : snake num -> bool ; Determines whether s is < n lbs (define (snake-lighter? s n) (< (snake-weight s) n)) The result type is boolean // Determines whether it s < n lbs return this.weight < n; 8
Methods in Java Comparing just the function and method: ; snake-lighter? : snake num -> bool Since ; Determines the method takes whether a Snake s is < n lbs and (define double (snake-lighter? and produces a s n) boolean, (< (snake-weight the contract is s) n)) Snake double -> boolean and we don t write it as a comment // Determines whether it s < n lbs return this.weight < n; 9
Methods in Java Comparing just the function and method: ; snake-lighter? : snake num -> bool ; Determines whether s is < n lbs (define (snake-lighter? s n) Purpose comment is as (< (snake-weight in Scheme, s) n)) but comments start with // // Determines whether it s < n lbs return this.weight < n; 10
Methods in Java Comparing just the function and method: ; snake-lighter? : snake num -> bool ; Determines whether s is < n lbs (define (snake-lighter? s n) (< (snake-weight s) Instead n)) of (snake-weight s) use // Determines whether this.weight it s < n lbs return this.weight < n; 11
Methods in Java Comparing just the function and method: ; snake-lighter? : snake num -> bool ; Determines whether s is < n lbs (define (snake-lighter? s n) (< (snake-weight s) n)) Explicitly designate the result with // Determines returnwhether it s < n lbs return this.weight < n; 12
Inside the class declaration... Methods in Java, Step-by-Step // Determines whether it s < n lbs return this.weight < n; 13
Methods in Java, Step-by-Step Inside the class declaration... // Determines whether it s < n lbs return First this.weight the purpose, starting < n; with // 14
Inside the class declaration... Methods in Java, Step-by-Step // Determines whether it s < n lbs return this.weight < n; Then the result type 15
Inside the class declaration... Methods in Java, Step-by-Step // Determines whether it s < n lbs return this.weight < n; Then the method name (not capitalized, by convention) 16
Inside the class declaration... Methods in Java, Step-by-Step // Determines whether it s < n lbs return this.weight < n; Start arguments with ( 17
Inside the class declaration... Methods in Java, Step-by-Step // Determines whether it s < n lbs return this.weight < n; Arguments except for this use a type for each argument, and separate multiple arguments with, 18
Inside the class declaration... Methods in Java, Step-by-Step // Determines whether it s < n lbs return this.weight < n; End arguments with ) 19
Inside the class declaration... Methods in Java, Step-by-Step // Determines whether it s < n lbs return this.weight < n; Then a { 20
Inside the class declaration... Methods in Java, Step-by-Step // Determines whether it s < n lbs return this.weight < n; Body using Java notation, put return before a result 21
Inside the class declaration... Methods in Java, Step-by-Step // Determines whether it s < n lbs return this.weight < n; Put ; after a result 22
Inside the class declaration... Methods in Java, Step-by-Step // Determines whether it s < n lbs return this.weight < n; End with 23
Method Calls in Java Original tests: (snake-lighter? (make-snake Slinky 10 rats) 10) new Snake("Slinky", 10, "rats").islighter(10) 24
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); slinky.islighter(10) 25
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); Constant definition starts with the constant s type slinky.islighter(10) 26
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); slinky.islighter(10) Then the name 27
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); slinky.islighter(10) Then = 28
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); Then an slinky.islighter(10) "should be" expression false 29
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); slinky.islighter(10) End with ; 30
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); slinky.islighter(10) Method call starts with an expression for the implicit this argument 31
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); slinky.islighter(10) Then. 32
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); slinky.islighter(10) Then the method name 33
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); slinky.islighter(10) Then ( 34
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); slinky.islighter(10) Then expressions for the explicit arguments separated by, 35
Method Calls in Java Equivalent, using constant definitions: (define SLINKY (make-snake Slinky 10 rats)) (snake-lighter? SLINKY 10) Snake slinky = new Snake("Slinky", 10, "rats"); slinky.islighter(10) Then ) 36
Templates In ; A snake is ; (make-snake sym num sym) (define-struct snake (name weight food)) ; func-for-snake : snake ->... (define (func-for-snake s)... (snake-name s)... (snake-weight s)... (snake-food s)...) 37
Templates Same idea works for class Snake { String name; double weight; String food; Snake(String name, double weight, String food) { this.name = name; this.weight = weight; this.food = food;... methodforsnake(...) {... this.name... this.weight... this.food... 38
More Examples Implement a feed method for Snake which takes an amount of food in pounds and produces a fatter snake Implement a feed method for Dillo and Ant Implement a feed method for Animal 39
Lists in Java Translate the list-of-num data definition to Java and implement a length method 40