Week 3. parameters, return, math, graphics

Similar documents
Lecture 3. parameters, return, math, graphics

parameters, return, math, graphics nobody expects the spanish inquisition!

Unit 3. parameters and graphics

Python! Created in 1991 by Guido van Rossum (now at Google)

CSc 110, Spring Lecture 7: Graphics, return values and math

Garfield AP CS. Graphics

Building Java Programs

Building Java Programs

Using Graphics. Building Java Programs Supplement 3G

Topic 9 More Graphics. Based on slides bu Marty Stepp and Stuart Reges from

Building Java Programs

Building Java Programs

Parameters. Repetitive figures. A solution? Parameterization. Declaring parameterized methods. Generalizing methods. Readings: 3.1

Building Java Programs

CSc 110, Autumn 2016 Lecture 7: Graphics. Adapted from slides by Marty Stepp and Stuart Reges

Introduction to Programming with Python

Topic 8 graphics. -mgrimes, Graphics problem report 134

Topic 8 Graphics. Margaret Hamilton

Lecture 9: Arrays. Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp. Copyright (c) Pearson All rights reserved.

Programming to Python

Week 2. expressions, variables, for loops

Introduction to Computer Science I

CSc 110, Autumn Lecture 10: return values and math

CSc 110, Spring Lecture 11: return values and math

Introduction to Programming with Python: overview


CSC108: Introduction to Computer Programming. Lecture 1

CS 106B Lecture 2: C++ Functions

TWO-DIMENSIONAL FIGURES

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

Building Java Programs

Building Java Programs

Using the API: Introductory Graphics Java Programming 1 Lesson 8

MaSH Environment graphics

G51PRG: Introduction to Programming Second semester Applets and graphics

CS 312 Midterm 1 Spring 2013

Building Java Programs

Building Java Programs

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Python. Sarwan Singh Assistant Director(S) NIELIT Chandigarh. Education is the kindling of a flame, not the filling of a vessel.

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

9. APPLETS AND APPLICATIONS

CS 312 Midterm 1 Spring 2013

CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random. Adapted from slides by Marty Stepp and Stuart Reges

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

Introduction to Programming

(created by professor Marina Tanasyuk) FUNCTIONS

CS 305j Midterm 1 Fall 2007

Dr. Hikmat A. M. AbdelJaber

CS 112 Introduction to Programming

CS 112 Introduction to Programming

CHAPTER 2. Java Overview

Methods CSC 121 Fall 2014 Howard Rosenthal

Graphics in Swing. Engineering 5895 Faculty of Engineering & Applied Science Memorial University of Newfoundland

2D Graphics. Shape Models, Drawing, Selection. CS d Graphics 1

Building Java Programs

Introduction to Programming

The FacebookPerson Example. Dr. Xiaolin Hu CSC 2310, Spring 2015

Week 2. Classes and Objects

CT 229 Java Syntax Continued

Trigonometric Functions of Any Angle

Lecture 7 A First Graphic Program And Data Structures & Drawing

CIS 162 Project 1 Business Card Section 04 (Kurmas)

AP CS Unit 12: Drawing and Mouse Events

S206E Lecture 13, 5/22/2016, Grasshopper Math and Logic Rules

Chapter 7 Applets. Answers

CISC 1600 Lecture 3.1 Introduction to Processing

Graphic User Interfaces. - GUI concepts - Swing - AWT

Using Methods and Parameters

Building Java Programs

Week 4. Strings, if/else, return, user input

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Admin. CS 112 Introduction to Programming. Recap. Example: Nested Loop. Example: Rewrite

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

CS 305j Midterm 1 Fall 2006

Methods (Deitel chapter 6)

KnowledgeScape, an Object oriented Real time Adaptive Modeling and Optimization Expert Control System for the Process Industries

Assignment 2. Application Development

Building Java Programs

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

CSc 110, Autumn 2016 Lecture 6: Parameters. Adapted from slides by Marty Stepp and Stuart Reges

CSC 551: Web Programming. Fall 2001

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Data Representation and Applets

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Formatting Output & Enumerated Types & Wrapper Classes

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

CIS 110: Introduction to Computer Programming

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

1001ICT Introduction To Programming Lecture Notes

Applets and the Graphics class

Programming Fundamentals

2D Graphics. Shape Models, Drawing, Selection. CS d Graphics 1

Methods CSC 121 Fall 2016 Howard Rosenthal

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Exceptions. raise type(message) raise Exception(message)

C++ Programming Lecture 11 Functions Part I

Transcription:

Week 3 parameters, return, math, graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0

Parameters def name(parameter, parameter,..., parameter): statements Parameters are declared by writing their names (no types) >>> def print_many(word, n):... for i in range(n):... print(word) >>> print_many("hello", 4) hello hello hello hello 2

Exercise Recreate the lines/boxes of stars example from lecture: ************* ******* *********************************** ********** * * ********** ***** * * * * ***** 3

Exercise Solution stars.py 1 2 3 4 5 6 7 8 9 10 11 12 13 # Draws a box of stars with the given width and height. def box(width, height): print width * "*" for i in range(height - 2): print("*" + (width - 2) * " " + "*") print width * "*" # main print(13 * "*") print(7 * "*") print(35 * "*") box(10, 3) box(5, 4) 4

Default Parameter Values def name(parameter=value,..., parameter=value): statements Can make parameter(s) optional by specifying a default value >>> def print_many(word, n=1):... for i in range(n):... print(word) >>> print_many("") >>> print_many("", 4) Exercise: Modify stars.py to add an optional parameter for the character to use for the outline of the box (default "*"). 5

Parameter Keywords name(parameter=value,..., parameter=value) Can specify the names of parameters as you call a function This allows you to pass the parameters in any order >>> def print_many(word, n):... for i in range(n):... print(word) >>> print_many(word="", n=4) >>> print_many(n=3, word="ni!") Ni! Ni! Ni! 6

Math commands from math import * Function name Description ceil(value) rounds up cos(value) cosine, in radians degrees(value) convert radians to degrees floor(value) rounds down log(value, base) logarithm in any base log10(value) logarithm, base 10 max(value1, value2,...) largest of two (or more) values Constant Description e 2.7182818... pi 3.1415926... min(value1, value2,...) smallest of two (or more) values radians(value) round(value) sin(value) sqrt(value) tan(value) convert degrees to radians nearest whole number sine, in radians square root tangent 7

Returning Values def name(parameters): statements... return value >>> def ftoc(temp):... tempc = 5.0 / 9.0 * (temp - 32)... return tempc >>> ftoc(98.6) 37.0 8

DrawingPanel Instructor-provided drawingpanel.py file must be in the same folder as your Python program At the top of your program, write: from drawingpanel import * Panel's canvas field behaves like Graphics g in Java 9

DrawingPanel Example draw1.py 1 2 3 4 5 6 from drawingpanel import * panel = DrawingPanel(400, 300) panel.set_background("yellow") panel.canvas.create_rectangle(100, 50, 200, 300) 10

Drawing Methods Java Python drawline panel.canvas.create_line(x1, y1, x2, y2) drawrect, fillrect drawoval, filloval drawstring setcolor setbackgroun d panel.canvas.create_rectangle(x1, y1, x2, y2) panel.canvas.create_oval(x1, y1, x2, y2) panel.canvas.create_text(x, y, text="text") (see next slide) panel.set_background(color) Notice, methods take x2/y2 parameters, not width/height 11

Colors and Fill Python doesn't have fillrect, filloval, or setcolor. Instead, pass outline and fill colors when drawing a shape. List of all color names: http://wiki.tcl.tk/16166 Visual display of all colors drawcolors.py 1 2 3 4 5 6 from drawingpanel import * panel = DrawingPanel(400, 300) panel.canvas.create_rectangle(100, 50, 200, 200, outline="red", fill="yellow") panel.canvas.create_oval(20, 10, 180, 70, fill="blue") 12

Polygons Draw arbitrary polygons with create_polygon Draw line groups by passing more params to create_line drawpoly.py 1 2 3 4 5 6 from drawingpanel import * panel = DrawingPanel(200, 200) panel.canvas.create_polygon(100, 50, 150, 0, 150, 100, fill="green") panel.canvas.create_line(10, 120, 20, 160, 30, 120, 40, 175) 13

E x e r c i s e Let s create a car in Python: import java.awt.*; public class DrawCar { public static void main(string[] args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setbackground(color.light_gray); Graphics g = panel.getgraphics(); g.setcolor(color.black); g.fillrect(10, 30, 100, 50); g.setcolor(color.red); g.filloval(20, 70, 20, 20); g.filloval(80, 70, 20, 20); g.setcolor(color.cyan); g.fillrect(80, 40, 30, 20); } } 14

E x er ci se Draw a car in Python 15

E x er ci se Now, let s use parameters so that we can place the cars all over the DrawingPanel. 16

E x er ci se Animate it using panel.sleep() 17