CSE / ENGR 142 Programming I

Slides:



Advertisements
Similar presentations
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Advertisements

BGI graphics library And Visual Studio.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
OOP&M - laboratory lectures1 OOP&M – LAB3 LABtres: drawing.
C++ Graphics Primitives April 15th. void clearscreen(int c) –clear the screen to background color c –If c = 1 screen black.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Visual C++ Programming: Concepts and Projects Chapter 6A: Methods (Concepts)
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
CS 450: Computer Graphics PIXEL AdDRESSING AND OBJECT GEOMETRY
L-1 Lecture 13: Functions and Design © 2000 UW CSE University of Washington Computer Programming I.
Copyright © Curt Hill Turtles The beginning of media computation.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Functional Abstraction Reduces Complexity.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6A Methods (Concepts)
Agenda For Feb PowerPoint Presentation on Java Methods. 3. Finish Happy Face Assignment (Due by the.
Lec 15 Writing an Applet Class. Agenda Writing an Applet class Java Graphics class.
L-1 Lecture 12: Functions and Design © 2000 UW CSE University of Washington Computer Programming I.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Arrays Chapter 7.
Pixels, Colors and Shapes
User-Written Functions
Chapter 15 Recursion.
Chapter 7: User-Defined Functions II
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Chapter 15 Recursion.
Building Java Programs
Building Java Programs
Example: Card Game Create a class called “Card”
CompSci 230 Software Construction
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
User-Defined Functions
Basic Graphics Drawing Shapes 1.
Abstract Data Types and Encapsulation Concepts
Local Variables variables which are declared within a
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
SSEA Computer Science: Track A
More About Recursion Java.
Arrays in Java What, why and how Copyright Curt Hill.
A+ Computer Science METHODS.
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
Review Session Biggest discrepancy questions
Chapter 7: User-Defined Functions II
Building Java Programs
Building Java Programs
Defining Classes and Methods
CS297 Graphics with Java and OpenGL
Announcements How to save your work? Quiz solution 5/5/2019
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Lawrence Snyder University of Washington
The beginning of media computation Followed by a demo
2D Shapes Rectangle Circle Triangle Rectangle. What shape is the door? Rectangle.
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

CSE / ENGR 142 Programming I Functions and Design © 1999 UW CSE 10/25/99

Drawing a House 10/25/99

Drawing a House 10/25/99

Drawing a (Similar) House Snyder: "like Sesame Street: noticing what things are similar and what things are different." 10/25/99

Draw House (Pseudo-code) draw_house (color, ll_x, ll_y, num_windows) draw body as a colored rectangle draw roof as a colored triangle if num_windows is one draw door draw window if num_windows is two Use the word "top down" somewhere The role of color and num_windows is clear. Not shown, but worth pointing out, is that llx and lly also change where each figure is drawn. 10/25/99

Functional Decomposition Draw House Draw Body Draw Roof Draw Door Draw Window Rectangle Triangle Rectangle Circle Rectangle Line This is a "calling tree" or "static call graph." Each function is shown, with an arrow down to each function called. 10/25/99

Functional Decomposition Draw House Draw Roof Draw Body Draw Door Draw Window Triangle Rectangle Circle Line Each function shown only once (preferred) 10/25/99

Analysis to Design to Programming Analyze the problem Then design a "big-picture" solution A functional decomposition shows how the pieces fit together Then design individual functions May depend on low-level ("primitive") functions available Final programming may be very detailed "Programming well involves both the big-picutre and the fine details. This is one things that makes it difficult." 10/25/99

. Graphics Primitives Y X Many systems offer a library of graphics primitives Typical functions: clearscreen, draw circle, rectangle, line, ellipse, etc. Typical parameters: location, color, fill, etc. Requires a coordinate system (0, 0) X Y (a,b) . Most "real" coordinate systems (e.g. Java) place (0,0) at the upper left of the screen, and use only positive coordintates. GP142 has (0,0) in the center of the screen. "In math, coordinates are real numbers. In computer graphics systems, they are usually integers (pixels)." 10/25/99

Typical 'rectangle' and 'line' void rectangle (int color, int x1, int y1, int x2, int y2); void line (int x1, int y1, int x2, int y2); (x2, y2) (x2, y2) (x1, y1) (x1, y1) 10/25/99

Big Picture Again Draw House Draw Roof Draw Body Draw Door Draw Window Triangle Rectangle Circle Line Fill in the pieces one at a time 10/25/99

Window Constants Our analysis of how to describe a window MID_X WIN_H WIN_W WIN_H MID_Y Actually, lots of ways using lines and rectangles that a window could be drawn. 10/25/99

Map Analysis to C Code void draw_window(int x, int y) Identify and declare constants Choose parameters Utilize primitives Get the picky details right, too! void draw_window(int x, int y) /* (x,y) is the lower left corner of the window */ { rectangle( WHITE, x, y, x + WIN_W, y + WIN_H); line( x+MID_X, y, x + MID_X, y + WIN_H); line( x,y + MID_Y, x + WIN_W, y + MID_Y); } 10/25/99

Keep Filling in Pieces Draw House Draw Roof Draw Body Draw Door Draw Window Triangle Rectangle Circle Line Analyze and code remaining functions. Does the order matter? 10/25/99

Draw House (gory details) void draw_house (int color, int ll_x, int ll_y, int windows) { int roof_ll_x, roof_ll_y ; /* Draw Body */ draw_body (color, ll_x, ll_y) ; /* Draw Roof */ roof_ll_x = ll_x - OVERHANG ; roof_ll_y = ll_y + BODY_HEIGHT ; draw_roof (color, roof_ll_x , roof_ll_y) ; /* Draw Door and Window(s) */ if (windows == 1) draw_door (ll_x + DOOR_OFFSET_1, ll_y) ; draw_window (ll_x + WINDOW_OFFSET_1, ll_y + WINDOW_RAISE) ; } else if (windows == 2) { draw_door (ll_x + DOOR_OFFSET_2, ll_y) ; draw_window (ll_x + WINDOW_OFFSET_2A, ll_y + WINDOW_RAISE) ; draw_window (ll_x + WINDOW_OFFSET_2B, } 10/25/99

Next Step: A Neighborhood We could write 6 different functions... Smarter: call 1 function 6 times... 10/25/99

Summary of Functional Decomposition Look for common elements (similarities) Parameterize for special features (differences) Determine which functions will use others Draw a graph to show their relationships This is very chracterister of Computer Science as a whole. 10/25/99

Review: Function Terminology function name int factorial ( int n ) { 0! is 1 1! is 1 2! is 1 * 2 3! is 1 * 2 * 3 . . . Review: Function Terminology function name int factorial ( int n ) { int product, i ; product = 1 ; for ( i = n ; i > 1 ; i = i - 1 ) { product = product * i ; } return (product) ; [formal] parameter local variables return type & value 10/25/99

Review: Local Variables main int main(void) { int i, y ; i = 3 ; y = factorial (i + 1) ; return (0) ; } i y factorial n product i 10/25/99

Local Variables: Summary Formal parameters and variables declared in a function are local to it: cannot be directly accessed by other functions Allocated (created) on function entry. De-allocated (destroyed) on function return. Formal parameters are initialized by copying value of actual parameter. Reminder: no global variables in 142! Following this slide used to be two slides about global variables,. The warning has been moved back to the end of lecture F. 10/25/99