Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Getting Input in Python Assumes assignment statement, typecasts.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
© Calvin College, Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create.
The Web Warrior Guide to Web Design Technologies
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
Processing Processing is a simple programming environment that was created to make it easier to develop visually oriented applications with an emphasis.
Chapter 10.
PHP (2) – Functions, Arrays, Databases, and sessions.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
CS 201 Functions Debzani Deb.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Introduction to Methods
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Games and Simulations O-O Programming in Java The Walker School
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Imperative Programming
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Introduction to Python
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Marie desJardins University of Maryland, Baltimore County.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 9, 2014 Carolyn Seaman Susan Martin University of Maryland, Baltimore.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introduction to Processing. 2 What is processing? A simple programming environment that was created to make it easier to develop visually oriented applications.
CIS 3.5 Lecture 2.2 More programming with "Processing"
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
More on GLUT Programming Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, September 15, 2003.
Test 2 Review. General Info. All tests are comprehensive. You are still responsible for the material covered prior to the first exam. You will be tested.
Simulation and Modeling: Predator-Prey Processing Lab IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 23, 2014 Carolyn Seaman Susan.
Computer Science I Looping. User input. Classwork/Homework: Incorporate looping & user input into a sketch.
G RAPHICS & I NTERACTIVE P ROGRAMMING Lecture 2 More Programming with Processing.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 25, 2014 Carolyn Seaman Susan Martin.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
C++ Functions A bit of review (things we’ve covered so far)
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
User-Written Functions
FOP: Buttons and Events
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
More programming with "Processing"
Defining Classes and Methods
Presentation transcript:

Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University of Maryland, Baltimore County

Today’s Class Processing concepts: Review: Functions Images: PImage type, loadImage(), image() Keyboard input: keyPressed(), key Mouse location: mouseX, mouseY New: Clock time and program time: millis(),...Top-down design Arrays: collections (lists) of objects Testing and debugging Demonstrated through a live coding session...

What Does That Mean?? “It looks like you’re mixing “active” and “static” modes.” “static” mode is when your Processing program is just a list of instructions – those instructions are run once and then the program ends “active” mode is when you define the setup() and/or draw() functions and the program runs continuously looking for input If you have any functions defined, you can’t have commands outside those functions – you need to use setup() and draw() If you want anything to happen after setup() runs (e.g., when a key is pressed), you must have a draw() function (even if it’s empty)

Functions Why do you need functions? Abstraction – focus on details once, then forget about them Repetition – do the same thing multiple times Clarity – give a set of complex commands a meaningful name Modularity – building a program out of coherent pieces rather than one big mess What is a function? A function is a set of commands, bundled together with a name, some parameters, and a return value, designed to complete one task Sometimes called a procedure if it doesn’t have a return value In some languages, called a method How do you use functions? ……

Creating a Function ( ) { } Where: is the type of data that the function returns as an answer is often void which means that the function does not return any value as an answer (this kind of function is sometimes called a procedure) is a name you choose to describe what the function does is a list of pieces of data that the function needs to do its job, i.e. its input; each parameter has a type and a name, i.e. is a list of commands and control statements that carry out the task the function is supposed to perform; must include a return statement if the function returns a value (i.e. if it’s not void )

Calling a Function ( ) ; Where is the name the function was given when it was created is a list of values for each parameter, in the order that the parameters were listed when the function was created

Example Creating the function product : float computePower (float x, int a) { float answer = 1; int i; for (i = 1; i <= a; i++) { answer = answer * x; } return (answer); } Calling the function compute_power : float radius = 5.5; float area = 3.14 * computePower (radius, 2); Since the function has a type (not void ), there has to be at least one return statement Parameter values have to be of the same types, in the same order, as the parameters in the function definition.

Example Creating the function printInstructions : void printInstructions (int iterations) { int i; println (“Stir the batter once.”); for (i = 2; i <= iterations; i++) { println (“Now stir it again.”); } Calling the function printInstructions : printInstructions (5); Since the function does not have a type ( void ), there is no return statement Parameter values have to be of the same types, in the same order, as the parameters in the function definition. Parameter values can be variables, constants, or expressions. Calling a function that doesn’t have a return value constitutes a program statement all by itself; doesn’t have to be embedded in an expression.

Handling Images in Processing Images: PImage – a special data type for variables that hold “raster images” (basically a pixel array) PImage loadImage (FILENAME) – a built-in function that you can call to load an image from a file (whose name is given as a string input). Returns an object of type PImage, so you would call it this way: PImage imageVariable;... imageVariable = loadImage (“mypicture.png”); void image (IMAGE, X, Y) – a built-in function that draws the PImage named IMAGE at location [X,Y] void image (IMAGE, X, Y, WIDTH, HEIGHT) – if given five arguments, the last two specify the desired width and height of the drawn image (otherwise the image is drawn “actual size”)

Keyboard Input void keyPressed() – function that is automatically called whenever a key is pressed while a Processing program is running To handle keyboard input, you would add this function to your Processing program At any time, you can refer to the special variable key (of type “char”) to see what key was pressed Example (which should look familiar...): void keyPressed() { if (key == ‘s’) { save (“garden.png”); } }

Mouse Location At any time while a Processing program is running, the special variables mouseX and mouseY will hold the X and Y coordinates of the mouse pointer’s location Example (what will it do?!): void keyPressed() { text (key, mouseX, mouseY); }

Timing Your Programs Processing has two concepts of time: clock time and program time Clock time is the real-world time year() – current year, an integer (e.g., 2013) month() – current month, from 1 to 12 day() – current day of the month, from 1 to 31 hour() – hour of the day, from 0 (midnight) to 23 (11pm) minute() – minute of the hour, from 0 to 59 second() – second of the minute, from 0 to 59 Program time is how long the program has been running millis() – the number of milliseconds elapsed since the program started

Arrays As discussed in St. Amant Chapter 4, we often need to store much more information than we could capture in a bunch of variables Suppose we wanted to plant a garden of 25 plants and then be able to interact with them – change the type, water them, fertilize them, etc. We probably wouldn’t want 25 different variables, one for each plant! Especially since we might end up wanting 100 plants instead... Arrays are used to create lists or sets of similar type variables that can be individually accessed and manipulated

Defining and Using Arrays If we want a list of numPlants plants, each with an X and Y location, we can define two arrays, one to hold the X values and one to hold the Y values: int numPlants = 5; int ;... float[] plantX = new float[numPlants]; float[] plantY = new float[numPlants];... for ( i=0 ; i<numPlants ; i++ ) { plantX[i] = *i; plantX[i] = *i; } Where will the plants be planted (that is, what are the X and Y locations? – note that nothing has actually been drawn by the code... we have to DO something with the X and Y values for anything to happen!) The plants are “indexed” from 0 to numPlants-1 “new TYPE[LENGTH]” creates an empty array with space for LENGTH objects of type TYPE

Live Design & Coding CrashBasics.pde Functionality: A frog appears anywhere the user types an “f” After a fixed period of time, the frog disappears Top-down design Implementation and debugging