0% found this document useful (0 votes)
5 views

ch02-IntroductionToProgramming

Chapter 2 introduces programming concepts, focusing on naming conventions for data, functions, and file types. It emphasizes the use of the Jython Environment for Students (JES) for programming, including installation and basic commands. The chapter also covers creating and manipulating functions, as well as handling media files like pictures and sounds.

Uploaded by

Hasnain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ch02-IntroductionToProgramming

Chapter 2 introduces programming concepts, focusing on naming conventions for data, functions, and file types. It emphasizes the use of the Jython Environment for Students (JES) for programming, including installation and basic commands. The chapter also covers creating and manipulating functions, as well as handling media files like pictures and sounds.

Uploaded by

Hasnain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

Chapter 2: Introduction to

Programming

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Chapter Learning Objectives

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Installation
 Installing JES and starting it up
 Go to http://www.mediacomputation.org and get the version of
JES for your computer.
 If you know that you have a Java compiler (e.g., a “JDK” or an “IDE”)
 Windows users:
 Just copy the folder
 Double-click JES application
 If trouble, try jes2.bat or jes-customjava.bat
 Mac users:
 Just copy the folder
 Double-click JES application
JES 5

There is always Help


 Lots and lots of excellent help

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Much of programming is about
naming
We name our data
Data: The “numbers” we manipulate
We call our names for data variables
We name our recipes
Quality of names determined much as in
Philosophy or Math
Enough words to describe what you need to
describe
Understandable

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Naming our Encodings
We even name our encodings
Sometimes referred to as types
Numbers without decimals are called integers.
Numbers with decimal points are called
floating point or floats.
Collections of letters are called strings.
Some programming languages are strongly
typed
A name has to be declared to have a type,
before any data is associated with it
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
34,654.01
12.998
31,364 12 Floats

Integers 1.01 0.01

-12
Barbara Ericson
Mark
Inside the computer, 85 5th Street NW
these are all just bits
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights
Strings
reserved.
Our programs work with
a variety of names
You will name your functions
Just like functions you knew in math, like sine
and gcd (Greatest Common Divisor)
You will name your data (variables)
You will name the data that your functions
work on
Inputs, like the 90 in sine(90)
Key: Names inside a function only have
meaning while the function is being executed
by the computer. (More on this later.)

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Names for things that are not
in memory
A common name that you’ll deal with is a file
name
 The program that deals with those is called the operating
system, like Windows, MacOS, Linux
A file is a collection of bytes, with a name, that
resides on some external medium, like a hard
disk.
 Think of it as a whole bunch of space where you can put your
bytes
Files are typed, typically with three letter
extensions
 .jpg files are JPEG (pictures), .wav are WAV (sounds)
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
We will program in JES
JES: Jython Environment for Students
A simple editor (for entering in our programs
or recipes): We’ll call that the program area
A command area for entering in commands
for Python to execute.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


JES - Jython Environment for
Students

Program Area

Command Area

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights


reserved.
Use Window Layout to get the view you want

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Tour of JES
Save and Save As
Cut/Copy/Paste with shortcut keys
Help

If JES runs slow, close other applications.


Web browsers (like Firefox or Internet Explorer)
and iTunes and chat tools and… all take up memory.
Closing some of them saves memory for JES.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Python understands commands
We can name data with =
We can print values, expressions, anything
with print

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Names can be (nearly)
whatever we want
Must start with a letter
Be careful not to use command names as
your own names
print = 1 won’t work
Case matters
“Print” is not the same as “print”
“myPicture” is not the same as “mypicture”

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


>>> print 34 + 56 Adding integers
90
>>> print 34.1/46.5 Dividing floats
0.7333333333333334
>>> print 22 * 33 Multiplying integers
726
>>> print 14 - 15 Subtracting integers
-1
>>> print "Hello" Printing a string
Hello
>>> print "Hello" + "Mark" Adding (concatenating)
HelloMark two strings
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Values and names with same value
are interchangeable
>>> print 12 * 3 >>> name = "Mark"
36 >>> print name
>>> value = 12 Mark
>>> print value >>> print name * 3
12 MarkMarkMark
>>> print value * 3 >>> print "Mark" * 3
36 MarkMarkMark

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


= is Assignment, not Equality
When you see “=“ in Python, read it as:
“Figure out the value on the right.”
“Give it the name on the left.”
So: size = 12.5
“Figure out the value on the right.” That’s 12.5
“Give it the name on the left.” That’s size
So: total = size * 34
“Figure out the value on the right.” That’s 425
(size * 34 is 12.5 * 34)
“Give it the name on the left.” That’s total
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights
reserved.
If you only use integers (numbers without decimal points),
Jython thinks you only want integers.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Which one comes closest to but doesn’t reach 1?
1. print 4 / 6

2. print 4 / 6.0

3. print 4 % 3

4. print 4 % 3.5

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights


reserved.
Command Area Editing
Up/down arrows walk through command
history
You can edit the line at the bottom
Just put the cursor at the end of the line before
hitting Return/Enter.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights


reserved.
JES Functions
A bunch of functions are pre-defined in JES
for sound and picture manipulations
pickAFile()
makePicture()
makeSound()
show()
play()
Some of these functions accept input values

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


What to do to show a picture
1. Find a file with a picture.
2. Pick it.
3. Get the bytes from that file into memory
and label it as a type: “picture”
4. Show the picture

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


pickAFile() leads to
The File Picker!

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Picture Functions
makePicture(filename) creates and returns a
picture object, from the JPEG file at the
filename
show(picture) displays a picture in a window
We’ll learn functions for manipulating
pictures later, like getColor, setColor, and
repaint

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Sound Functions
makeSound(filename) creates and returns a
sound object, from the WAV file at the
filename
play(sound) makes the sound play (but
doesn’t wait until it’s done)
blockingPlay(sound) waits for the sound to
finish
We’ll learn more later like getSample and
setSample

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Demonstrating simple JES
>>> myfilename = pickAFile()
>>> print myfilename
/Users/guzdial/mediasources/barbara.jpg
>>> mypicture = makePicture(myfilename)
>>> print mypicture
Picture, filename
/Users/guzdial/mediasources/barbara.jpg height
294 width 222
>>> show(mypicture)

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Demonstrating simple JES
>>> print pickAFile()
/Users/guzdial/mediasources/barbara.jpg
pickAFile() returns a filename,
>>> print makePicture(pickAFile()) which can be used as input to
Picture, filename makePicture() to make a
/Users/guzdial/mediasources/barbara.jpg height
294 width 222
picture or makeSound() to
>>> show(makePicture(pickAFile())) make a sound.
>>> print show(makePicture(pickAFile()))
None
>>> print pickAFile()
Printing a picture just proves
/Users/guzdial/mediasources/hello.wav there’s a picture there.
>>> print makeSound(pickAFile())
Sound of length 54757
>>> print play(makeSound(pickAFile()))
show() and play() don’t return
None anything, so they print None.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


COMPLETELY THE SAME:
Values, names for those values, functions that return
those values

>>> file=pickAFile()
>>> print file
C:\Documents and Settings\Mark Guzdial\My
Documents\mediasources\barbara.jpg
>>> show(makePicture(file))
>>> show(makePicture(r"C:\Documents and
Settings\Mark Guzdial\My Documents\
mediasources\barbara.jpg"))
>>> show(makePicture(pickAFile()))

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Picking, making, showing a picture

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Grabbing media from the Web
Right-click
(Windows) or
Control-Click (Mac)
Save Target As…
Can only do JPEG
images
(.jpe, .jpg, .jpeg)
Most images on the Internet
are copyright. You can
download and use them for
your use only without
permission.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Writing a recipe:
Making our own functions
To make a function, use
the command def
Then, the name of the
function, and the names of
the input values between
parentheses (“(input1)”)
End the line with a colon
(“:”)
The body of the recipe is
indented (Hint: Use two
spaces)
 That’s called a block

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


Blocking is indicated for you in JES
Statements that are
indented the same,
are in the same
block.
Statements that are
in the same block as
where the line where
the cursor is are
enclosed in a blue
box.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


The Most Common JES Bug:
Forgetting to Load
Your function does NOT
exist for JES until you
load it
 Before you load it, the
program is just a bunch
of characters.
 Loading encodes it as an
executable function
Save and Save As
 You must Save before
Loading An “Unloaded”
 You must Load before function doesn’t exist
you can use your function yet.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Making functions the easy way
Get something working by typing commands
Enter the def command.
Copy-paste the right commands up into the
recipe

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


def pickAndPlay():
myfile = pickAFile()
mysound = makeSound(myfile)
play(mysound)

Note: myfile and mysound, inside pickAndPlay(), are


completely different from the same names in the command
area.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


def pickAndShow():
myfile = pickAFile()
mypict = makePicture(myfile)
show(mypict)

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
def playSound():
myfile = "FILENAME" You can always replace
mysound = makeSound(myfile) data (a string of
play(mysound) characters, a number,
whatever) with a name
def showPicture(): (variable) that holds
myfile = "FILENAME" that data—or vice
mypict = makePicture(myfile) versa.
show(mypict)
Put r in front of Windows filenames: r
“C:\mediasources\pic.jpg”
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
What to do about Windows
filenames?
Python doesn’t like you to use “\” in
filenames,
like “C:\mediasources\barbara.jpg”
What to do?
Option #1: Put r in front of Windows filenames:
r“C:\mediasources\pic.jpg”
Option #2: Use forward slashes. Python will
translate it for you:
“C:/mediasources/pic.jpg”

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights


reserved.
def playNamed(myfile): What functions do you
mysound = makeSound(myfile) need? What should be
play(mysound) their input?

def showNamed(myfile): In general, have enough


mypict = makePicture(myfile) to do what you want,
show(mypict) easily, understandably,
and in the fewest
commands.

We’ll talk more about


what that means later.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights
reserved.
What can go wrong?
Did you use the exact same names (case,
spelling)?
All the lines in the block must be indented,
and indented the same amount.
Variables in the command area don’t exist in
your functions, and variables in your
functions don’t exist in the command area.
The computer can’t read your mind.
 It will only do exactly what you tell it to do.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.


MOST IMPORTANT THING TO DO TO PASS THIS
CLASS!
DO THE EXAMPLES!
Try them out for yourself. Try to replicate
them. Understand them
 EVERY WEEK, TYPE IN AT LEAST TWO OF THE EXAMPLES
FROM CLASS
To understand a program means that you
know why each line is there.
You will encounter all the simple-but-
confusing errors early—BEFORE you are
rushing to get homework done!!

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights


reserved.

You might also like