24
CSC 200 Study Guide – Fall 2010 Chapter 5 Advanced Picture Techniques Chapter Learning Objectives The media learning goals for this chapter are: To implement controlled color changes, like red-eye removal, sepia tones, and posterizing. To use blending to combine images. To use background subtraction to separate foreground from background images and to understand when and how it will work. To use chromakey to separate foreground from background images. To be able to add text and shapes to existing pictures. To use blurring to smooth degradation. The computer science goals for this chapter are: To use conditionals. To be able to choose between using vector and bitmapped image formats. To be able to choose when one should write a program for a task versus using existing applications software. Study Smart, Not Hard Learn the information as we go through the semester. Spend a hour or so a day a week before the exam to study. It is impossible to cram all this information into your brain if you wait until night before the exam. Read all the short programs in this chapter to make sure that you understand exactly what they do. You should understand them much easier now than you did earlier this semester. 5.1 Replacing Colors: Red-Eye, Sepia Tones and Posterizing In this section, you learn how to replace colors with another color to create interesting effects. Program 36 def turnRed(): (page 106) See Figure 5.1 on page 106 Katie with red couch. This program goes through the entire katieFancy.jpg image one pixel at a time and replaces brown pixels with red. Problem is the brown checked pattern couch colors change too. CSC 200 Page 1

CSC 200 Study Guide – Fall 2010 · To use iteration (in for loops) for ... CSC 200 Study Guide – Fall 2010 . Chapter 9 Building Bigger Programs . ... Computer Science goals for

  • Upload
    vothuy

  • View
    215

  • Download
    2

Embed Size (px)

Citation preview

CSC 200 Study Guide – Fall 2010

Chapter 5 Advanced Picture Techniques Chapter Learning Objectives

The media learning goals for this chapter are: • To implement controlled color changes, like red-eye

removal, sepia tones, and posterizing. • To use blending to combine images. • To use background subtraction to separate foreground

from background images and to understand when and how it will work.

• To use chromakey to separate foreground from background images.

• To be able to add text and shapes to existing pictures. • To use blurring to smooth degradation.

The computer science goals for this chapter are: • To use conditionals. • To be able to choose between using vector and bitmapped image formats. • To be able to choose when one should write a program for a task versus

using existing applications software.

Study Smart, Not Hard

Learn the information as we go through the semester. Spend a hour or so a day a week before the exam to study. It is impossible to cram all this information into your brain if you wait until night before the exam. Read all the short programs in this chapter to make sure that you understand exactly what they do. You should understand them much easier now than you did earlier this semester.

5.1 Replacing Colors: Red-Eye, Sepia Tones and Posterizing

In this section, you learn how to replace colors with another color to create interesting effects. Program 36 def turnRed(): (page 106) See Figure 5.1 on page 106 Katie with red couch. This program goes through the entire katieFancy.jpg image one pixel at a time and replaces brown pixels with red. Problem is the brown checked pattern couch colors change too.

CSC 200 Page 1

Chapter 5 Study Guide

CSC 200 Page 2

Program 37 def turnRedinRange(): (page 108) See Figure 5.2 Katie and brown couch on page 107 This program goes changes only the rectangle around Katie’s face so her hair changes from brown to red. Note the x range and y range.

5.1.1 Reducing Red-Eye

“Red-eye” is the effect where the flash from the camera bounces off the back of the subject’s eyes and makes the eye pupil look red in a photograph. (page 108) Program 38 def removeRedEye(pic, startX, startY, endX, endY, replacementColor): (page 108-109) See Figure 5.3 and Figure 5.4 on page 110 for before and after. This algorithm replaces the pixels that are pure red in the square areas of Jenny’s eyes with black.

5.1.2 Sepia-Toned and Posterized Pictures: Using Conditionals to Choose the Color

Sepia toning is a specialized effect to give the photograph a warmer tone and look like an old-fashioned photograph. Sepia is a dark-brown color. (page 110) Program 39 def sepiaTint(picture): (pages 110-112) See Figure 5.5 Landscape scene on page 111. This algorithm first changes the entire picture object to grayscale. Then it changes the red, green and blue values to more sepia/brown tones instead of gray tones. Posterizing is an artistic effect that converts a continuous gradation of colors to several regions of fewer colors with abrupt changes from one to another originally used to create posters. Now it is used to create 50s style images. (page112) Program 40 def posterize(picture): (pages 112-113) See Figure 5.6 Guy on page 112. This algorithm changes the range of red, green and blue values to only 4 values for each so we only have 12 colors in the entire image. Program 41 def grayPosterize(picture): (pages 113-114) See Figure 5.7 Guy in B/W on page 114. This algorithm changes the range of colors to only 2 colors black and white.

5.2 Combining Pixels: Blurring

In this section, we can reduce pixilation by blurring the image. Pixilation occurs when an image is enlarged too much and you can see the individual pixel boxes or jaggies with the naked eye. (page 115)

Chapter 5 Study Guide

CSC 200 Page 3

Program 42 def blur(filename): (pages 115-116) See Figure 5.8 Flower on page 116. This algorithm sets each pixel to a color that is an average of the colors of the pixels around it so the image looks blurry but reduces the jaggy effect of pixilation.

5.3 Comparing Pixels: Edge Detection

Edge detection is a process where we simulate a line drawing by detected the edges of an image and changes the pixels to either black or white. (page 116) Program 43 def lineDetect(filename): (pages 116-118) See Figure 5.9 Butterfly on page 117. This algorithm compares each pixel’s luminance (lightness/darkness) to the pixel below and to the right of it. If there is a significant difference the we make the pixel black, otherwise we leave it white.

5.4 Blending Pictures

In this section, we learn how to blend two images together. Program 44 def blendPictures(): (page 118-119) See Figure 5.10 Barb and Katie blended together. This algorithm takes 50% of the RGB color from the Barb picture and 50% of the %RBG color from the Katie picture to set each pixel color in the middle blended section. Otherwise we copy the pixels “as-is” from each of the images for the left and right sections.

5.5 Background Subtraction

In this section, we try to remove a background and replace it with another background image. Program 45 def swapBack(pic1, back, newBg): (page 120-121) See Figure 5.11 Katie and Figure 5.12 Moon. This algorithm takes an image and the same image with just the background and swaps the old background with the new one. This doesn’t work too well as we have a bleeding effect with shadows and using flash photos. We solve this problem using Chromakey.

5.6 Chromakey

Chromakey is using a solid color (usually bright green) as a background so you can easily replace the background with another image. (page 123)

Chapter 5 Study Guide

CSC 200 Page 4

Program 46 def chromakey(source, bg): (page 123-125) See Figures 5.16 Mark, Figure 5.17 Mark on moon and Figure 5.18 Mark in Jungle. This algorithm takes a source picture and replaces the pixels with the chromakey background color with the pixel colors from the new background picture. Program 47 def chromakey2(source, bg): (page 124) This is an alternative way of writing the code for chromakey. Same results as the previous program.

5.7 Drawing on Images

In this section, we learn how to draw lines, shapes and text on top of an existing picture object. Program 48 def lineExample(): (page 127) See Figure 5.21 Carolina with grid. This algorithm draws a grid of horizontal and vertical lines on an existing image.

5.7.1 Drawing with Drawing Commands

In this section, we learn new drawing commands. See the Command Summary later in this study guide for details on these commands. Program 49 def addABox(): (page 128-129) See Figure 5.22 Beach scene with red box. This algorithm draws a red box on the beach picture. Program 50 def littlePicture(): (page 129-130) See Figure 5.23 Yellow box. This algorithm draws a yellow filled box.

5.7.2 Vector and Bitmap Representations

Vector based images are saved as the drawing instructions. These files are very small compared to bitmap images. (page 130) Traditionally drawing applications created vector images such as MacDraw, Corel Draw, Macromedia Freehand and Adobe Illustrator and Flash. Bitmap based images store every single pixel in grid/map of the image. These files can be very large so compression techniques are using to make the file size smaller. Photoshop (.psd), .bmp, .gif and jpeg files are bitmap images. Traditionally painting applications created bitmap images such as MacPaint, Corel Paint, Windows Paint and Paintbrush and Adobe Photoshop.

Chapter 5 Study Guide

CSC 200 Page 5

Lossy compression techniques throw away some of the details of the image but preserve the significant detail. .jpg uses a lossy compression scheme. Lossless compression keeps all the original details of the image but still compresses the file. .gif and .png use lossless compression schemes. Run-length encoding is an example of lossless compression. See page 130 for details.

5.8 Programs as Specifying Drawing Process

In this section, we will draw pictures programmatically by using the computer to do the calculation. Program 51 def grayEffect(): (pages 131-132) See Figure 5.24 grayscale effect This algorithm creates a grayscale optical illusion. Program 52 def coolPic(): (pages 132-133) See Figure 5.25 nested orange rectangles. This algorithm draws a series of nested orange to black rectangles. Program 53 def coolPic(): (pages 133-134) See Figure 5.26 nested blank rectangles. This algorithm draws a series of nested rectangles.

5.8.1 Why Do We Write Programs?

You can use the programming techniques to draw pictures for you even if you cannot draw yourself.

Built-in JES functions

See page 134 for the programming summary for this chapter. • addText(pict, x, y, string)

Adds text to the picture starting at the (x,y) coordinate position • addLine(pict, x1,y1,x2,y2)

Draws a line from (x1,y1) to (x2,y2) • addRect(pict,x1,y1,w,h)

Draws a rectangle with (x1,y1) as the top left corner with the specified width and height

• addRectFilled(pict,x1,y1,w,h,color) Draws a filled rectangle with the selected color using (x1,y1) as the top left corner with the specified width and height

Last Update: December 6, 2010

CSC 200 Study Guide – Fall 2010

Chapter 6 Modifying Sounds Using Loops

Chapter Learning Objectives

Media Learning Goals:

• To understand how we digitize sounds, and the limitations of human hearing that allow us to digitize sounds.

• To use the Nyquist theorem to determine the sampling rate necessary for digitizing a desired sound.

• To manipulate volume. • To create (and avoid) clipping.

Computer Science Goals:

• To understand and use arrays as a data structure. • To use the formula that n bits results in 2n possible patterns in order to figure out

the number of bits needed to save values. • To use the sound object. • To debug sound programs. • To use iteration (in for loops) for manipulating sounds. • To use scope to understand when a variable is available for us.

Key Terms

Make sure that you understand the definition of the following terms. The page is given from the textbook. Each term is either bold or italic in the textbook. Use your textbook and other sources such as webopedia.com , Wikipedia.org or your favorite search engine to add the definitions to the list.

Page Term Definition

140 Shape of wave

141 frequency

140 amplitude

140 Sine wave

140 cycle

140 decibels

141 Pitch

CSC 200 Page 1

Chapter 6 Study Guide

141 Hertz

141 Cycles per second

141 Pitch interval

143 Signal view

144 Spectrum view

144 Fourier transform

145 slice

145 Analog to digital conversion

146 Digital to analog conversion

146 Nyquist theorum

147 Sampling rate

147 Two’s complement notation

148 Clipping

148 Pulse Code Modulation (PCM)

148 Array

148 Element

148 index

155 Loop

155 iterate

160 Tracing a program

160 Debugging

162 Normalizing

Jython / JES Programming Summary

See pages 164-165 for the programming summary for this chapter.

Last Update: December 6, 2010 Study Guide created by Carlotta Eaton For Introduction to Computing and Programming in Python – A Multimedia Approach by Guzdial & Ericson 2nd edition

CSC 200 Page 2

CSC 200 Study Guide – Fall 2010

Chapter 9 Building Bigger Programs

Chapter Learning Objectives

1. To demonstrate two different design strategies: top-down and bottom-up.

2. To demonstrate different testing strategies, such as black box and glass box.

3. To demonstrate several debugging strategies to use when figuring out programming problems.

Computer Science goals for this chapter are:

4. To use methods for accepting user input and generating output for the user. 5. To use a new iteration structure, the while loop.

Instructions

1. Skip Section 9.6 Running Programs Outside of JES. We don't have regular Python nor Jython in the lab.

Chapter Concepts

This chapter covers design strategies for creating large programs. These are the issues that are addressed by the field of Software Engineering.

9.1 Designing Programs Top-Down

There are 5 primary phases to the Software Development Life Cycle (SDLC)

1. Problem Statement – what are you trying to accomplish with the program? What problem are you trying to solve?

2. Program Design – Use top-down or bottom-up design to determine the overall design and structure of the program project.

3. Code – Code the program using a programming language. 4. Test – Test and debug the program. Fix the problems and then test again. 5. Maintenance – This is the phase of the project where you solve problems for

customers that are using your program. This lasts as long the program is supported.

CSC 200 Page 1

Chapter 9 Study Guide

Note: We have an entire class devoted to SDLC, ITP 251 Systems Analysis & Design, where you learn this topic in depth.

Top-Down Design – This is done in Step 2 of the SDLC above. You determine the initial large function/procedure for the program. Then you subdivide this into smaller and smaller functions/procedures. (page 208)

Procedural Abstraction Process: This is the process for doing Top-Down Design. This is shown in more detail in Chapter 15 on page 342.

• State the problem. Figure out what you want to do. • Break the problem into subproblems. • Keep breaking the subproblems into small problems until you know how to write the

program to solve the subproblem. • Your goal is for the main procedure to basically tell all the subprocedures what to do.

Each procedure should do one and only one logical task.

Here is the hierarchy of functions for the Adventure Game in this chapter (not in book).

Ensure that you understand the purpose of all the functions in the Adventure game program and how that work together. (pages 210 – 215).

9.2 Designing Programs Bottom Up

Bottom-Up Design – This is alternative method of doing Step 2 of the SDLC process. With bottom-up design, you start with the small functions/procedures and keep adding more functions/procedures to them to create larger functions/procedures until the process is complete. (page 215)

9.3 Testing Your Program

There are 2 main approaches to testing programs:

1. Glass-box testing – you test every possible path through your program 2. Black-box testing – you test with valid and invalid inputs for the program

especially the boundary edge conditions. (See page 218)

CSC 200 Page 2

Chapter 9 Study Guide

CSC 200 Page 3

9.4 Tips on Debugging

Debugging – the process of figuring out why your program is not doing what it is supposed to do.

There are 2 major types of programming errors:

1. Syntax errors – these errors occur when the code doesn’t follow the programming language rules. These are easier to debug because you usually get error messages about the error.

2. Logic errors – these errors occur when the program runs but does not produce the correct results. These are harder to debug because you have to test to find these errors. (This term is not in the book but it is described.)

9.5 Algorithms and Design

Algortithms – general descriptions of processes that can be implemented by writing code in a programming language.

Jython / JES Programming Summary

See page 228 for the programming summary for this chapter.

Last Update: December 6, 2010 Study Guide created by Carlotta Eaton for Introduction to Computing and Programming in Python – A Multimedia Approach by Guzdial & Ericson 2nd edition

CSC 200 Study Guide – Fall 2010

Chapter 10 Creating and Modifying Text

Chapter Learning Objectives

The media learning goals for this chapter are:

• To generate text in a form-letter style. • To manipulate structured text, such as phone and address

listings. • To generate random structured text.

Computer Science goals for this chapter are:

• To access object components using dot notation. • To manipulate strings. • To read and write files. • To understand file structures like trees. • To write programs that manipulate programs, which leads to powerful ideas like

interpreters and compilers.

Instructions

1. Skip Sections 10.3.2 Lists, 10.3.3 Strings Have No Fonts, 10.4.3 Writing Out Programs, and 10.5 The Python Standard Library. We don't have regular Python nor Jython in the lab.

Chapter Concepts

This chapter covers text, strings, and files.

10.1 Text as Unimedia

Computers are actually unimedia as they only understand binary code 0s and 1s. (page 233)

HTML (HyperText Markup Language) is the formatting language used to markup text to format web pages. (page 233)

10.2 Strings: Making and Manipulating Strings

String – a sequence of characters. (page 234)

CSC 200 Page 1

Chapter 10 Study Guide

Unicode – Unicode is a character set (originally 16 bits) that encodes all the modern spoken and written human languages. (page 235)

Concatenation – put 2 strings together (page 236)

10.3 Manipulating Parts of Strings

Be able to write and understand code using the string notation. (page 237)

10.3.1 String Methods: Introducing Objects and Dot Notation

Strings in JES/Jython/Pytyon are objects. There are many useful string methods. Use the dot notation to use these methods. See the Programming Summary on page 255 for more details.

• (page 238) capitalize • (page 238) startswith(prefix) • (page 238) endswith(suffix) • (page 239) find(searchItem) • (page 239) upper() • (page 239) lower() • (page 239) swapcase() • (page 239) title() • (page 239) isalpha() • (page 239) isdigit() • (page 239) replace(searchItem, replaceItem)

10.3.2 Lists: Powerful, Structured Text Skip this section.

10.3.3 Strings Have No Font Skip this section.

10.4 Files: Places to Put your Strings and Other Stuff

Make sure you know the following terms in this chapter. The page is given from the textbook.

Page Term Definition

243 Base name

243 File suffix (extension)

243 Root directory

243 Path

CSC 200 Page 2

Chapter 10 Study Guide

CSC 200 Page 3

243 Tree

243 Root

243 Branches

243 Leaves

243 Parent

243 children

244 Binary files

Skip the code on page 244 on manipulating lists.

10.4.1 Opening and Manipulating Files

There are many useful functions to manipulate files. Use the dot notation to use these functions. See the Programming Summary on page 255 for more details.

• (page 245) read() • (page 245) readlines() • (page 245) write(text) • (page 245) close

10.4.2 Generating Form Letters

Program 91 def formLetter(gender, lastName, city, eyeColor): (page 246) This program creates a simple form letter as shown on pages 247-248.

10.4.3 Writing Out Programs Skip this section.

10.5 The Python Standard Library Skip this entire section including 10.5.2 and 10.5.3.

Jython / JES Programming Summary

See page 225 for the programming summary for this chapter for the terms in section 10.3.1 in this study guide. Last Update: December 6, 2010 Study Guide created by Carlotta Eaton for Introduction to Computing and Programming in Python – A Multimedia Approach by Guzdial & Ericson 2nd edition

CSC 200 Study Guide – Fall 2010

Chapter 14 Speed

Chapter Learning Objectives

• To choose between compiled and interpreted programming languages, based on an understanding of machine language and how computers work.

• To know the categories of algorithms based on their complexity and to avoid intractable algorithms.

• To consider processor choice based on an understanding of clock rates.

• To make decisions about computer storage options when aiming for optimizing speed.

Instructions

Skip all the programming code samples in this chapter as they seem to confuse rather than enlighten.

14.1 Focusing on Computer Science

In this chapter, you learn the factors that contribute the speed of a computer program including hardware, programming language, compilers, interpreters and algorithms. These are some of the topics studied in the field of Computer Science.

14.2 What Makes Programs Fast?

14.2.1 What Computers Really Understand

14.2.2 Compilers and Interpreters

Programming languages are traditionally either compiled or interpreted.

1. Initially, interpreted languages were translated to machine language line-by-line; that is, each line was interpreted as it was about to be executed, and if a loop or subroutine caused certain lines to be executed multiple times, they would be reinterpreted every time.

2. A compiled language transforms code written in a programming language into machine code (also called object code). The most common reason for wanting to transform source code is to create an executable program (.exe file).

CSC 200 Page 1

Chapter 14 Study Guide

A program translated by a compiler tends to be much faster than an interpreter executing the same program. Theoretically, any language may be compiled or interpreted, so this designation is applied purely because of common implementation practice and not some essential property of a language. Most modern programming languages include a combination of interpreters and compilers.

See a great analogy of compiled vs. interpreted with English and French (middle of page 325).

14.2.3 What Limits Computer Speed?

Algorithm – is a description of the process for a computer program to solve a problem (Page 327)

Big-Oh Notation (also called Big-O notation) refers to the magnitude of processing and running time of an algorithm. This helps programmers decide the most efficient algorithm for the problem they are solving. (page 328)

Sorting Algorithms

There are a dozen popular sorting algorithms. Three that we discussed or used with the sorting students class activity are:

1. bubble sort – The bubble sort is one of the easiest sort algorithms to code, but it is slow and has a complexity of O(n2) (page 329)

2. quicksort – The quicksort is a divide and conquer algorithm that is harder to code, but many programming languages have this sort built-in to the language. If you use the median as the pivot point, then the complexity is O(n log n). (page 329)

3. selection sort - The algorithm finds the minimum value, swaps it with the value in the first position, and repeats these steps for the remainder of the list. It has a complexity of O(n2). Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations. (Not in book)

14.2.4 Making Searching Faster

Searching Algorithms

There are several searching algorithms. Two discussed in the book are:

1. Linear search (also called sequential search) – Linear search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found. This is the easiest search to code, but it is not very efficient, and the complexity is O(n/2). (page 329)

2. Binary search – Binary search is a divide and conquer algorithm that requires a

CSC 200 Page 2

Chapter 14 Study Guide

list to be in sorted order. Binary search works by comparing an input value to the middle element of the array. The average performance is O(log n). (page 330)

14.2.5 Algorithms That Never Finish or Can’t Be Written

Computer scientists classify problems into 3 basic classes according to their complexity: (page 332)

1. Class P (polynomial) – problems that can be solved with an algorithm that has a polynomial complexity (like sorting). These are "efficiently solvable" or "tractable" problems.

2. Class NP (nondeterministic polynomial time) – problems that seem intractable, but it has been proven that a solution does exist. The Traveling Salesman problem is an example.

3. Intractable - Problems that can be solved but not fast enough for the solution to be useful.

The Venn Diagram above shows that Class P problems are a subset of Class NP problems.

Alan Turing proved that there are some algorithms that cannot be written such as a solution to the Halting Problem. (page 333) He used the concept of a Turing Machine to make such proofs back in 1936.

The field of artificial intelligence is the study of using computers to simulate human intelligence.

14.2.6 Why is Photoshop Faster than JES?

To run a JES program, the computer goes through several processes as shown below. (page 327)

To run most applications such as Photoshop, Word, Excel, etc. the computer goes through the first step only once. When you run the program the computer merely executes the .exe file. It doesn’t need to be translated, compiled nor interpreted.

CSC 200 Page 3

Chapter 14 Study Guide

14.3 What Makes a Computer Fast?

Computer storage (sorted by speed, fastest to slowest) (#1 - #3 in book page 335)

1. Cache memory – fastest storage, located on the CPU chip 2. RAM – volatile storage (temporary) is the computer’s main memory 3. Hard drive – non-volatile storage space where you store the computer files 4. Auxilary storage – flash drives, CDs, DVDs etc are auxilary to the computer 5. LAN storage – files are stored on a local area network drive 6. Cloud storage – files are stored on a server on the Internet; Examples are google

docs, dropbox, etc.

See the Key Terms section below for details of this section.

Key Terms

In addition to the terms above, make sure that you understand the definitions of the following terms. The page is given from the textbook. Each term is either bold or italic in the textbook. Use your textbook and other sources such as webopedia.com , Wikipedia.org or your favorite search engine to add the definitions to the list.

Page Term Definition

322 Machine language

322 processor

322 Assembly language (spelled wrong in book)

323, 325 compiler

323 translating

324 interpreter

326 Virtual machine

329 Sorting algorithms

333 Moore’s Law

334 Clock rate

334 GHz

CSC 200 Page 4

Chapter 14 Study Guide

CSC 200 Page 5

334 CPU

334 Dual core

334 Quad core

334 Parallel processor

335 Cache memory

335 SDRAM, RAM

335 Hard disk

336 System bus

336 swapping

336 Swap space

337 Display speed

Jython / JES Programming Summary

No programming language items in this chapter.

References

Several links are provided to Wikipedia articles that were used in addition to the textbook to create this study guide.

Last Update: December 6, 2010 Study Guide created by Carlotta Eaton for Introduction to Computing and Programming in Python – A Multimedia Approach by Guzdial & Ericson 2nd edition

CSC 200 Study Guide – Fall 2010

Chapter 15 Functional Programming

Chapter Learning Objectives

• To write programs more easily with more functions. • To use functional programming to make powerful programs

quickly. • To understand what makes functional programming different

from procedural or imperative programming. • To be able to use else in Python. • To be able to use global in Python.

Instructions

• We covered most of the key concepts from this chapter in Chapter 9 Building Bigger Programs. Missing information from this chapter is included here.

• The only pages required from this chapter are page 339-340, 342 and page 348.

Chapter Concepts

There are 4 basic programming language paradigms.

1. Procedural Programming Model (also called Imperative Model) is the oldest and most common model. These languages allow the programmer to express algorithms as a hierarchy of tasks. The imperative paradigm is characterized by sequential execution of instructions, the use of variables that represent memory locations, and the use of assignment statements that change the value of the variables. Languages include FORTRAN, COBOL, BASIC, C, Pascal, and Ada.

2. Functional Programming Model is based on the mathematical concept of the function. The solution to a problem is expressed in terms of mathematical functions. The basic mechanism is the evaluation of functions; there are no variables and no assignment statements. There are no looping constructions; repetition is expressed in terms of recursion. Functional programming has its roots in Lambda calculus. Functional models are emphasized in primarily in academic research instead of commercial software languages. Languages include Lisp, Scheme, Haskell and ML. Mathematica (symbolic Math) software program is used in some math classes and is based on the functional model.

3. Logic Programming Model is based on the principles of symbolic logic. The model comprises a set of facts about objects and a set of rules about the relationships among the objects. The underlying problem-solving algorithm uses

CSC 200 Page 1

Chapter 15 Study Guide

the rules of logic to deduce the answer from the facts and rules. Logic models are used primarily in the field of Artificial Intelligence. Programming language include PROLOG and Lisp.

4. Object-Oriented Programming Model is the newest paradigm. This model views the world as a collection of interacting objects. Each object is defined by a class, and the actions are defined by methods. These languages allow the programmer to express algorithms using a hierarchy of objects. Languages include SIMULA, Smalltalk, C++, Visual Basic .NET, and Visual C++.

Remember that a programming language can support more than one programming paradigm. For example programs written in C++ can be purely procedural or purely object-oriented or contain elements of both.

If you are creating a procedural program, then you need to use the Procedural Abstraction Process:

• State the problem. Figure out what you want to do. • Break the problem into subproblems. • Keep breaking the subproblems into small problems until you know how to write

the program to solve the subproblem. • Your goal is for the main procedure to basically tell all the subprocedures what to

do. Each procedure should do one and only one logical task.

Recursion is writing functions or procedures that call themselves. Instead of writing loops, you write a function that loops by calling itself. To avoid endless recursion you need to specify what to do to end the process. (See page 348)

Short History of Programming Languages

1. Machine Language -program instructions were written in binary 2. Assembly Language - program instructions were 4 letter word mnemonics such

as LOAD, ADD, STOR etc. Every computer processor has a different language. 3. Procedural (High-Level) Languages - Program instructions were more English

like statements. Languages included FORTRAN, COBOL and LISP. 4. Structured Programming (High-Level) Languages - These languages included

better programming techniques for a more disciplined approach to programming. Languages include Pascal, Modula2, BASIC, C and C++. C++ was a language that allowed a programming access to low-level statements and was the most popular industry language during this timeframe (1971 - 1989).

5. Object-Oriented Programming Languages - With the advent of GUI interfaces, Windows O/S and Mac, programmers needed more flexibility to create graphical interfaces. Object-Oriented languages made GUIs easier to create. Languages include C++, SIMULA, and Smalltalk.

6. Internet Programming Languages - The Internet brought new challenges for programmers, so new languages were created including Java, Javascript, and PERL.

CSC 200 Page 2

CSC 200 Study Guide – Fall 2010

Chapter 16 Object-Oriented Programming Chapter Learning Objectives

• To use object-oriented programming to make programs easier to develop in teams, more robust, and easier to debug.

• To understand such features of object-oriented programs as polymorphism, encapsulation, inheritance and aggregation.

• To be able to choose between different styles of programming for different purposes.

Study Smart, Not Hard

Learn the information as we go through the semester. Spend a hour or so a day a week before the exam to study. It is impossible to cram all this information into your brain if you wait until night before the exam. Read all the short programs in this chapter to make sure that you understand exactly what they do. You should understand them much easier now than you did earlier this semester.

16.1 History of Objects

See Chapter 15 Study Guide for more details on the history of programming languages. Object-Oriented Programming Model (OOPs) is the newest language paradigm. This model views the world as a collection of interacting objects. Each object is defined by a class, and the actions are defined by methods. These languages allow the programmer to express algorithms using a hierarchy of objects. Alan Kay invented the term object-oriented programming and created the first object-oriented language called SmallTalk. Alan earned the 2003 Turing Award for his pioneering world in OOPs. (Note: Book has wrong date) One of his mentors (during graduate school) was Ivan Sutherland, who had done pioneering graphics programs including Sketchpad. “Software is actually a simulation of world. By making software model the world, it becomes clearer how to make software.” Adele Goldberg also worked on the SmallTalk project and was a co-author of the first book about SmallTalk.

CSC 200 Page 1

Chapter 16 Study Guide

CSC 200 Page 2

Benefits of Object-Oriented Programming

1. Manage complexity - each object is self-contained so it is much easier to debug errors and manage the project.

2. Supports robustness - Robustness is the ability of a computer system to cope with errors during execution or the ability of an algorithm to continue to operate despite abnormalities in input or calculations.

3. Supports reuse - This means you can reuse objects (and code) more easily than traditional programming methods. Inheritance in OOPs naturally creates reusability.

16.2 Working with Turtles

(Page 361) Dr. Seymour Papert, at MIT, created a system called Logo that programmed turtles on a screen to help young children think and solve problems in the late 1960s. (Not in book) Karel the Robot was another popular Logo type language created and used at Stanford University by Richard E. Pattis in 1981. This language used a robot called Karel instead of turtles. This program was taught in Computer Science classes at NRCC and RU in the 1990s. See the Karel the Robot link for example code. It looks very similar to JES. In this section, you learn how to use the Turtle objects available in JES.

1. To start a turtle program in JES, you must create the world, and create an instance of the world.

2. Next you create and name an instance of a turtle. 3. Now you can send messages to the turtle and make it move on the screen

and draw.

See the Built-in JES for Turtle Objects (later in this study guide) for details on creating worlds, turtle and methods.

16.3 Teaching Turtles New Tricks

In this section, you learn how to create a subclass of a turtle and teach it new tricks. See Programs 146 and 147 on pages 369 – 370 for details on creating a subclass called smartTurtle from the built-in turtle class/object.

16.4 An Object-Oriented Slide Show

Chapter 16 Study Guide

CSC 200 Page 3

• Skip this entire section (pages 371 – 380) except for the definition of encapsulation (on page 373) and polymorphism (on page 376).

16.4.3 Why Objects?

In this section, more benefits of OOPs are described. Object-oriented programming is great for GUI applications, interactive games and interactive applications. Some benefits are listed in Section 16.1 (earlier in this study guide). Other benefits are PIE, polymorphism, inheritance, and encapsulation.

Built-in JES for Turtle Objects

• makeWorld() (page 362) creates a new world instance for turtles to explore.

• makeTurtle(worldName) (page 363) creates a new turtle instance that appears in the center of the world

• .forward() or .forward(pixels) (page 363) method that moves the turtle forward 100 pixels (default) or the number of pixels specified

• .turnLeft() or turnRight() (page 364) method that turns the turtle left or right

• .turn(degrees) (page 364) method that turns the turtle the specified number of degrees. Remember 360 degrees is a full spin.

• .penUp() or .penDown() (page 366) method that for the turtle to pick up or put down a drawing pen

• .moveTo(x,y) (page 367) method that moves a turtle to the specified coordinate position

• .setPenWidth(width) (page 367) method that sets the turtle pen to the specified width

• .setColor(color) (page 367) method that changes the turtle pen to the specified color

Key Terms Make sure that you understand the definition of the following terms. The page is given from the textbook. Each term is either bold or italic in the textbook. Use your textbook and other sources such as webopedia.com , Wikipedia.org or your favorite search engine to add the definitions to the list.

Remember OOP is easy as PIE. A language must suuport Polymorphism, Inheritance and Encapsulation to be classified as a OOP language.

Chapter 16 Study Guide

CSC 200 Page 4

Page Term Definition

360 Object-oriented programming

See previous page of this study guide.

361 object

The primary items in an object oriented program. These are the nouns in the system you are modeling.

class The formal definition of an object. Similar to the function definitions we have been using all semester.

361

Instance

An instance is an occurrence or an object in a program. Analogy: The class is the recipe for an object such as a cake and the baked cake would be the instance.

361

Method

Methods are the verbs in an OOP project. The methods are associated with a particular object. For example if the object was rabbit, then methods could be hop, dig(hole), eat(carrots), wiggle(nose).

376

Polymorphism

Polymorphism means that you can have methods with the exact same name that will do the correct process for different objects or even in the same object. See Program 147 smartTurtle (page 147) for an example. We have 2 methods named drawSquare but it executes the correct method based on the number of inputs.

381

Inheritance

Inheritance is a powerful feature where an object can inherit all the methods and properties of a parent object. See Program 146 smartTurtle (page 369) for an example. All the methods that a turtle object can do such as moveTo, forward, turnRight etc. are available to the smartTurtle because it is a subclass(child) of the turtle object built into JES.

373

Encapsulation

Encapsulation means that the object properties and methods are all defined in a single class (section of code). This makes it much easier to debug and program. See Program 147 smartTurtle (page 147) for an example. All the methods for the smartTurtle are contained within the class code.

Jython / JES Programming Summary

• Ignore the Programming Summary in the book as these are the graphic commands from the slide show section that we skipped.

Last Update: December 6, 2010 Study Guide created by Carlotta Eaton For Introduction to Computing and Programming in Python – A Multimedia Approach by Guzdial & Ericson 2nd edition