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

Similar documents
Functions in Scheme. From Scheme to Java. Functions in Java. Functions in Java. method that is in the class // Determines whether it s < n lbs

Class Diagrams Nesting Variants to Refine Contracts Common Functionality in Abstract Classes Nesting without Abstract

Animal Classes. Door Classes. Some Maze Classes. Class Diagrams. Nesting Variants to Refine Contracts. Common Functionality in Abstract Classes

From FP to OOP. Start with data: class Posn { int x; int y; Posn(int x, int y) { this.x = x; this.y = y; } }

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Wednesday, November 5

Loops in Scheme, II. (early slides assume map/filter)

Tracking Rumors. Suppose that we want to track gossip in a rumor mill

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

Object Oriented Programming in C#

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Chapter 20: Binary Trees

ormap, andmap, and filter

How to Design Programs

Why OO programming? want but aren t. Ø What are its components?

CSE 374 Programming Concepts & Tools

The Design Recipe Fall 2017

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

FRAC: Language Reference Manual

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Graphs. Directed graphs. Readings: Section 28

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Outline. Helper Functions and Reuse. Conditionals. Evaluation Rules for cond. Design Recipe with cond. Compound Data

Chapter 2: Using Data

Graphs. Readings: Section 28. CS 135 Fall : Graphs 1

CS111: PROGRAMMING LANGUAGE II

3. Java - Language Constructs I

Module 5: Lists. Readings: HtDP, Sections 9, 10.

The Design Recipe Fall 2018

COMP520 - GoLite Type Checking Specification

Outline. Data Definitions and Templates Syntax and Semantics Defensive Programming

Module 3: New types of data

Conversions and Overloading : Overloading

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Visual C# Instructor s Manual Table of Contents

Programming with Math and Logic

Basic Object-Oriented Concepts. 5-Oct-17

CSSE 220. Interfaces and Polymorphism. Check out Interfaces from SVN

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Justification for Names and Optional Parameters

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

PIC 20A The Basics of Java

Principles of Object Oriented Programming. Lecture 4

A First Book of ANSI C Fourth Edition. Chapter 12 Structures

A Fast Review of C Essentials Part I

1B1b Classes in Java Part I

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

COMP520 - GoLite Type Checking Specification

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

Web Application Development

GOLD Language Reference Manual

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CS115 - Module 3 - Booleans, Conditionals, and Symbols

CS115 - Module 8 - Binary trees

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

CS115 - Module 4 - Compound data: structures

"Good" vs. "Bad" Expressions. ; interp-expr FAE... -> FAE-Value

CS1102: What is a Programming Language?

Unit 4: Client View of a Component Methods

CSE 431S Type Checking. Washington University Spring 2013

Preliminaries. Part I

1d: tests knowing about bitwise fields and union/struct differences.

Full file at

Classes, Objects, and OOP in Java. June 16, 2017


Quiz. Question #1: What is the value of the following expression? {+ 1 2} Wrong answer: 0 Wrong answer: 42. Answer: 3 1-4

Introduction to Programming, Aug-Dec 2006

CS558 Programming Languages

CS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018

Object Oriented Programming

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Distributed Programming and Remote Procedure Calls (RPC): Apache Thrift. George Porter CSE 124 February 19, 2015

Topics. bool and string types input/output library functions comments memory allocation templates classes

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

CSU211 Exam 2 Fall 2007

17. Classes. Overloading Functions. Operator Overloading. Function Overloading. operatorop

Lecture 2: Variables & Assignments

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Repetition. Add in Objects. Position Objects. Monkey Eat Bananas Repetition Simple Loops and Conditional Loops for Alice 3

Monkey Eat Bananas Repetition Simple Loops and Conditional Loops for Alice 3

Objective-C. Stanford CS193p Fall 2013

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

CS 403/503 Exam 4 Spring 2015 Solution

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

Computational Expression

Quiz 1: Functions and Procedures

1 Lexical Considerations

Lecture 4: Inheritence and Abstract Classes

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

QUark Language Reference Manual

Lexical Considerations

Onion Language Reference Manual. By: Andrew Aday, (aza2112) Amol Kapoor (ajk2227), Jonathan Zhang (jz2814)

Functional programming in mainstream languages. CS 3110 Lecture 26 Andrew Myers

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Haskell Scripts. Yan Huang

Transcription:

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