TeachScheme, ReachJava Adelphi University Monday afternoon July 12, 2010.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
RAPTOR Syntax and Semantics By Lt Col Schorsch
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Exponential Functions Logarithmic Functions
ES 314 Lecture 2 Sep 1 Summary of lecture 1: course overview intro to matlab sections of Chapters 2 and 3.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
CSC 160 Computer Programming for Non-Majors Lecture #8: Animations I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5: Defining Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Sixteen lists and compound data. Recap Names: constants and variables When evaluated, return a specific data objects Can make new names with: [define.
Fourteen lists and compound data. Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, , etc. “Floating-point” numbers ,
Section 2.5: Designing Programs. REVIEW: Design Recipe  Figure out precisely what you need to do.  Tell the computer how to do it.  Check that the.
Exercise A) Find out how tock in that figure works manually. Enter expression expressions such as (tock 0) and (tock 1) into DrScheme's Interactions.
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
Programming a GUI Hanan sedaghat pisheh. For calling GUI, we need a function with no inputs or outputs First We create a m.file m file has the same name.
Positional Number Systems
Maths Notes Number 5. Decimals
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.
Developing Mathematical Thinkers
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
TeachScheme, ReachJava Adelphi University Tuesday afternoon July 13, 2010.
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
Input, Output, and Processing
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
TeachScheme, ReachJava Adelphi University Wednesday morning June 24, 2008.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
CS 100Lecture 11 Welcome to CS 100 n Goal: Learn to write intelligible, correct computer programs n Language: Java (but the language should not be the.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Introduction to Universe Programs CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
Game Maker – Getting Started What is Game Maker?.
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
TeachScheme, ReachJava Adelphi University Thursday afternoon July 15, 2010.
TeachScheme, ReachJava Stephen Bloch, Adelphi University, Garden City, NY John Clements, Cal Poly, San Luis Obispo, CA Kathi Fisler, Worcester Polytechnic.
TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010.
A Case Study: Space Invaders CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Learning PowerPoint Presenting your ideas as a slide show… …on the computer!
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.
Variables, Types, Expressions Intro2CS – week 1b 1.
Using Inheritance to Share Implementations CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
A Sample Program #include using namespace std; int main(void) { cout
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
IDENTIFIERS CSC 111.
Coding Concepts (Data Structures)
A Case Study: Space Invaders
Presentation transcript:

TeachScheme, ReachJava Adelphi University Monday afternoon July 12, 2010

July TeachScheme, ReachJava Animation (big-bang calendar (on-draw show-it) (on-tick rotate-cw 0.5)) ; big-bang : image handler … -> image At every event (in this case, a clock tick every half second), applies handler to old image to get new image Currently using “default” draw handler, show-it, which takes old image and displays it in animation window Try some variations: different images, tick-intervals, "on- tick" handlers

July TeachScheme, ReachJava A more complex animation Write an animation of an image that moves 5 pixels to the right every second We'll need a function that takes in an image and returns the same image 5 pixels to the right Follow the design recipe!

July TeachScheme, ReachJava move-right-5 ; move-right-5 : image -> image

July TeachScheme, ReachJava move-right-5 ; move-right-5 : image -> image (check-expect (move-right-5 calendar) (beside (rectangle 5 0 "solid" "white") calendar)) (check-expect (move-right-5 (circle 3 "solid" "red")) (beside (rectangle 5 0 "solid" "white") (circle 3 "solid" "red")))

July TeachScheme, ReachJava move-right-5 ; move-right-5 : image -> image (define (move-right-5 old-pic) ) (check-expect (move-right-5 calendar) (beside (rectangle 5 0 "solid" "white") calendar)) (check-expect (move-right-5 (circle 3 "solid" "red")) (beside (rectangle 5 0 "solid" "white") (circle 3 "solid" "red")))

July TeachScheme, ReachJava move-right-5 ; move-right-5 : image -> image (define (move-right-5 old-pic) ; old-pican image ) (check-expect (move-right-5 calendar) (beside (rectangle 5 0 "solid" "white") calendar)) (check-expect (move-right-5 (circle 3 "solid" "red")) (beside (rectangle 5 0 "solid" "white") (circle 3 "solid" "red")))

July TeachScheme, ReachJava move-right-5 ; move-right-5 : image -> image (define (move-right-5 old-pic) ; old-pican image (beside (rectangle 5 0 "solid" "white") old-pic)) (check-expect (move-right-5 calendar) (beside (rectangle 5 0 "solid" "white") calendar)) (check-expect (move-right-5 (circle 3 "solid" "red")) (beside (rectangle 5 0 "solid" "white") (circle 3 "solid" "red")))

July TeachScheme, ReachJava Running the animation (big-bang calendar (on-draw show-it ) (on-tick move-right-5 1)) ; Note window dimensions as optional ; arguments to on-draw

Writing draw handlers Worked exercise: develop an animation of a calendar that rotates, sitting at (100,40) on a 150x200 pink rectangular background. Can’t do this using just show-it; need to write our own draw handler. “current image” will be the calendar in its current orientation, but draw handler will put it in right place on right background. July TeachScheme, ReachJava

Writing draw handlers ; place-on-pink : image -> image July TeachScheme, ReachJava

Writing draw handlers ; place-on-pink : image -> image (check-expect (place-on-pink calendar) (place-image calendar (rectangle “solid” “pink”))) (check-expect (place-on-pink (rotate-cw schemelogo)) (place-image (rotate-cw schemelogo) (rectangle “solid” “pink”))) July TeachScheme, ReachJava

Writing draw handlers ; place-on-pink : image -> image (define (place-on-pink current) … ) (check-expect (place-on-pink calendar) (place-image calendar (rectangle “solid” “pink”))) (check-expect (place-on-pink (rotate-cw schemelogo)) (place-image (rotate-cw schemelogo) (rectangle “solid” “pink”))) July TeachScheme, ReachJava

Writing draw handlers ; place-on-pink : image -> image (define (place-on-pink current) ; current image … ) (check-expect (place-on-pink calendar) (place-image calendar (rectangle “solid” “pink”))) (check-expect (place-on-pink (rotate-cw schemelogo)) (place-image (rotate-cw schemelogo) (rectangle “solid” “pink”))) July TeachScheme, ReachJava

Writing draw handlers ; place-on-pink : image -> image (define (place-on-pink current) ; current image (place-image current (rectangle “solid” “pink”))) (check-expect (place-on-pink calendar) (place-image calendar (rectangle “solid” “pink”))) (check-expect (place-on-pink (rotate-cw schemelogo)) (place-image (rotate-cw schemelogo) (rectangle “solid” “pink”))) July TeachScheme, ReachJava

Writing draw handlers (big-bang calendar (on-draw place-on-pink) (on-tick rotate-cw 0.5)) July TeachScheme, ReachJava

July TeachScheme, ReachJava Other kinds of event handlers tick-handler : image -> image Specify with on-tick (optional argument for time interval) mouse-handler : image number(x) number(y) event -> image Specify with on-mouse key-handler : image key -> image Specify with on-key draw-handler : image -> image Specify with on-draw (optional args for window dimensions) I'm lying: they're actually more general than this. Note: on-tick, on-mouse, on-key, and on-draw all take in a function name as their first argument.

Digression for GUI programmers We’re teaching model/view separation: The draw handler is the map from model to view Other event handlers map from old model to new model. For now, the model is always an image. (This changes in a few chapters.) Non-majors with 3 weeks’ programming experience can do this. July TeachScheme, ReachJava

July TeachScheme, ReachJava Another animation Write an animation of a calendar that moves with the mouse on a 500x300 yellow background. Need a mouse-handling function Contract must be ; handle-mouse : image num(x) num(y) event -> image We don't know what an "event" is yet; ignore it.

July TeachScheme, ReachJava calendar-at-mouse ; calendar-at-mouse : image num(x) num(y) event -> image

July TeachScheme, ReachJava calendar-at-mouse ; calendar-at-mouse : image num(x) num(y) event -> image (check-expect (calendar-at-mouse schemelogo "dummy") (place-image calendar (rectangle "solid" "yellow"))) (check-expect (calendar-at-mouse stick-figure "event") (place-image calendar (rectangle "solid" "yellow")))

July TeachScheme, ReachJava calendar-at-mouse ; calendar-at-mouse : image num(x) num(y) event -> image (check-expect (calendar-at-mouse schemelogo "dummy") (place-image calendar (rectangle "solid" "yellow"))) (check-expect (calendar-at-mouse stick-figure "event") (place-image calendar (rectangle "solid" "yellow"))) (define (calendar-at-mouse old-pic x y event) )

July TeachScheme, ReachJava calendar-at-mouse ; calendar-at-mouse : image num(x) num(y) event -> image (check-expect (calendar-at-mouse schemelogo "dummy") (place-image calendar (rectangle "solid" "yellow"))) (check-expect (calendar-at-mouse stick-figure "event") (place-image calendar (rectangle "solid" "yellow"))) (define (calendar-at-mouse old-pic x y event) ; old-pican image ; xa number ; ya number ; eventwhatever )

July TeachScheme, ReachJava calendar-at-mouse ; calendar-at-mouse : image num(x) num(y) event -> image (check-expect (calendar-at-mouse schemelogo "dummy") (place-image calendar (rectangle "solid" "yellow"))) (check-expect (calendar-at-mouse stick-figure "event") (place-image calendar (rectangle "solid" "yellow"))) (define (calendar-at-mouse old-pic x y event) ; old-pican image ; xa number ; ya number ; eventwhatever (place-image calendar x y (rectangle "solid" "yellow")))

July TeachScheme, ReachJava calendar-at-mouse ; calendar-at-mouse : image num(x) num(y) event -> image (define BACKGROUND (rectangle "solid" "yellow")) (check-expect (calendar-at-mouse schemelogo "dummy") (place-image calendar BACKGROUND)) (check-expect (calendar-at-mouse stick-figure "event") (place-image calendar BACKGROUND)) (define (calendar-at-mouse old-pic x y event) ; old-pican image ; xa number ; ya number ; eventwhatever (place-image calendar x y BACKGROUND))

July TeachScheme, ReachJava Running the animation (big-bang calendar (on-draw show-it) (on-mouse calendar-at-mouse))

Recipe for an animation 1What handlers will you need? 2Write their contracts. 3Develop each one, following the design recipe for functions (including testing!) 4Test the whole animation, using big- bang. July TeachScheme, ReachJava

July TeachScheme, ReachJava Numbers Remember, my students are numerophobic. We haven't seen an arithmetic operator yet. (Week 4 of a non-major course) Arithmetic operators follow the same syntax rule as every other function: (operation argument …) The +, -, *, / operations accept 2 or more arguments Example: 3 + 4*5 + 6 becomes (+ 3 (* 4 5) 6) Other built-in functions with obvious names: sqrt, cos, sin, abs, min, max, …

July TeachScheme, ReachJava Examples of arithmetic 3+4 becomes (+ 3 4) 3+(4*5) becomes (+ 3 (* 4 5)) becomes ( ) 3x-7 becomes (- (* 3 x) 7) (presumably x is an already-defined variable) 3*4+5/6 becomes (+ (* 3 4) (/ 5 6)) (but you have to know order of operations to understand the former; the latter is unambiguous)

July TeachScheme, ReachJava Exercise Convert the following from "standard" algebraic notation to Scheme notation: 3+cos(0) 2/(x+1) (-b+√(b 2 -4ac))/2a where a, b, c are variables

July TeachScheme, ReachJava Writing functions on numbers ; cube : number -> number (define (cube num) ; num a number (* num num num)) (check-expect (cube 0) 0) (check-expect (cube 5) 125) (check-expect (cube -6) -216) (check-expect (cube (/ 2 3)) (/ 8 27))

July TeachScheme, ReachJava Kinds of numbers Try (cube (cube (cube ))) Integers behave correctly Try (+ (/ 2 3) (/ 3 4)) Fractions behave correctly (can choose output in either fraction or repeating-decimal form) Abbreviations: -4 = (- 4), 2/3 = (/ 2 3), etc. as long as there are no spaces

July TeachScheme, ReachJava Kinds of numbers Try (sqrt 2) Result is marked as inexact, as are results of subsequent computations using it Note (sqr (sqrt 2)) ≠ 2 (check-expect (sqr (sqrt 2)) 2) will fail! (check-within (sqr (sqrt 2)) ) passes the test Use check-within whenever function result might be inexact.

July TeachScheme, ReachJava Exercises Define a function f that takes in two numbers x and y and returns 3x-2y Define a function discriminant that takes in three numbers a, b, and c and returns b 2 - 4ac As usual, follow the design recipe!

July TeachScheme, ReachJava Animations revisited Previous animations computed the "new image" from the "old image" Often makes more sense to compute on something other than an image -- a "model" "Model" can be an image or a number (or really any data type you choose)

Recipe for an animation, version 2 1What handlers will you need? 2What type will your model be? 3Write the handlers' contracts. 4Develop each one, following the design recipe for functions (including testing!) 5Test the whole animation, using big- bang. July TeachScheme, ReachJava

July TeachScheme, ReachJava Kinds of event handlers tick-handler : model -> model Specify with on-tick (optional argument for clock interval) mouse-handler : model number(x) number(y) event -> model Specify with on-mouse key-handler : model key -> model Specify with on-key draw-handler : model -> image Specify with on-draw (optional arguments for window dimensions) big-bang: model handler … -> model Note: on-tick, on-mouse, on-key, and on-redraw all take in a function name as an argument.

July TeachScheme, ReachJava Animations with numeric models Write an animation of a blue circle that grows in radius by 1 pixel per half second Need a tick handler and a redraw handler Follow the design recipe for both There happens to already be an add1 function which will work as the tick handler

July TeachScheme, ReachJava Growing-circle animation ; blue-circle-of-size : number -> image (check-expect (blue-circle-of-size 0) (circle 0 "solid" "blue")) (check-expect (blue-circle-of-size 12) (circle 12 "solid" "blue")) (define (blue-circle-of-size r) (circle r "solid" "blue")) (big-bang 0 (on-tick add1 1/2) (on-draw blue-circle-of-size))

July TeachScheme, ReachJava Exercise: parametric equations Write an animation that displays a small dot at x coordinate *cos(t/20) and y coordinate *sin(t/20) where t is the number of time steps so far. (Set the tick interval fairly short, e.g. 1/10 second.) Hint: write helper functions x(t) and y(t). Play with the formulae and see what different patterns you can get.

July TeachScheme, ReachJava Exercise Write an animation that displays a digital counter, in 18-point blue numerals. It should start at 0 and increase by 1 every second. Hint: there's a built-in function number->string that converts a number to its decimal representation. Then use text to convert the string to an image.

July TeachScheme, ReachJava Discussion break How is this different from what you've done in the past? How much explaining would it take for your students?

July TeachScheme, ReachJava Animations with numeric models Can now assign animations in which model is a number Examples: radius, x coordinate, or y coordinate, number of sides, … Model can increase & decrease in response to ticks, mouse actions, & keyboard actions

July TeachScheme, ReachJava Animations with randomness (random 8) returns a random integer from 0 through 7 Use this to write more fun and unpredictable animations

July TeachScheme, ReachJava Strings ; string-append : string … -> string ; string-length : string -> number ; substring : string number [number] -> string ; string->number : string -> number ; number->string : number -> string Can now write animations with strings as the model (e.g. adding or chopping characters)

July TeachScheme, ReachJava So far we've… covered the first 5-6 weeks of my non-majors' programming course learned three syntax rules –function call –variable definition –function definition gotten some practice writing, composing, and re-using functions, following a concrete step-by-step recipe learned to write interactive animations with model/view framework and callbacks (will do the same in Java)

Are we done yet? Fill out end-of-day survey at Eat Sleep Come back for another day July TeachScheme, ReachJava 2010