Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 7 - Friday.  What did we talk about last time?  Software engineering  Testing  Programming languages.

Similar presentations


Presentation on theme: "Week 7 - Friday.  What did we talk about last time?  Software engineering  Testing  Programming languages."— Presentation transcript:

1 Week 7 - Friday

2  What did we talk about last time?  Software engineering  Testing  Programming languages

3

4

5  Object orientated programming (OOP) is a way of organizing code into objects containing  Data  Methods to manipulate the data  OOP is used  To protect data inside of objects from being changed in unexpected ways  To safely reuse code  It's hard to see why OOP is useful until you've programmed a big project  The shapes in Visual Python are examples of objects ball = sphere(color=color.red) ball.pos.x = 10 #data inside the ball ball = sphere(color=color.red) ball.pos.x = 10 #data inside the ball

6  The very earliest programming languages used punch cards for player pianos and looms  A mechanical computer, the Analytical Engine, would have used punch cards, but it was never completed  Work was done on the Engine during the mid to late 19 th century  Ada Lovelace described an algorithm for it and is considered by some to be the first programmer

7  Early electronic computers were built in the 1940s and 1950s  They were programmed directly in the machine language of 1s and 0s that they would understand  Machine language was succeeded by assembly language  Had human readable commands like load, store, and add  But the commands were specific to machines and very low level

8  The third generation of languages is a set of general-purpose, high-level languages still used today  Pioneered in the 1960s and 1970s  Although they are written with a computer now, programs in these languages were written on punch cards before PCs existed  Examples: CC  FORTRAN  LISP

9  Java and Python are actually newer additions to the third generation of general purpose languages  Fourth generation languages are for specific purposes  SQL for talking to databases  Unix shell for interacting with a Unix file system  Fifth generation languages are given constraints on a problem and use artificial intelligence to solve it  Prolog is the best known example

10 LanguageDescriptionTyping C/C++Fast and powerful systems language, prone to nasty bugs Static JavaPlatform independent OO language, uses virtual machine Static Objective-CApple language for writing Mac, iPhone, and iPad apps Static C#Microsoft language for applications and the web, like Java Static PythonInterpreted language with unusual syntax, easy to read Dynamic PerlScripting language, good at processing text Dynamic RubyInterpreted language, popular for making websites Dynamic PHPInterpreted, C-like language that runs on web servers Dynamic JavaScriptInterpreted language that runs on web browsers Dynamic Visual BasicMicrosoft language, good at making GUIs Static SQLLanguage for talking to databases Static

11  We can classify languages on a spectrum from low to high  High level languages allow you to give more abstract commands that are more like human thought processes or mathematics  Low level languages are closer to the computer world and give explicit instructions for the hardware to follow MLJavaC++C Assembly Language Machine Code LowHigh

12  We use a program called a compiler to turn a high level language into a low level language  Usually, the low level language is machine code  Python uses an interpreter instead of a compiler  An interpreter runs the program line by line instead of turning it into a machine code file that the OS can run

13 Computer! Solve a problem; Compile 010101010 010100101 001110010 Execute Source Code Machine Code Hardware

14

15  Functions allow you to package up some code to run over and over  Functions usually take some input (like numbers or text) so that they can be customized  Functions often give back an answer (like the square root of a number)  Also called methods in some other languages  The customizable purple blocks played the role of functions in Scratch

16  More modular programming  Break a program into separate tasks  Each task could be assigned to a different programmer  Code reusability  Use code over and over  Even from other programs  Less code duplication  Improved readability  Each function can do a few, clear tasks

17 def name( arg1, …, argn ): statement1 statement2 … statementm Function name Name of last argument Name of 1 st argument Required syntax Code done by function (must be indented)

18  Given two numbers, find the smaller: def smaller(a, b): if a < b: return a else: return b def smaller(a, b): if a < b: return a else: return b

19  It is possible to divide functions into two types:  Functions that return values  Functions that don't return values  Functions that do return values give an answer:  It's as if the function is replaced by whatever answer is returned x = 3 y = 4 small = smaller(x, y) #small contains 3 x = 3 y = 4 small = smaller(x, y) #small contains 3

20  A function doesn't have to return an answer:  This function only prints things to the screen  A function that doesn't return anything does some task but doesn't give back an answer def callForHelp(times): for i in range(times): print("Help!") def callForHelp(times): for i in range(times): print("Help!")

21  Like most code in Python, the code inside of a function executes line by line  Of course, you are allowed to put if statements and loops inside methods  You can also put in return statements  A function will stop executing and jump back to wherever it was called from when it hits a return  The return statement is where you put the value that will be given back to the caller

22  Defining a function is only half the game  You have to call functions to use them  Calling a function means giving it the parameters (or arguments) it needs and then waiting for its answer  By now, you have done many function calls  print()  You can call your own functions the same way

23  You type the name of the function and then its arguments in parentheses  If it's a value returning function, you can store the answer it gives back  The arguments you give can be values or variables  You have to give the function the right number of arguments or the program will have an error callForHelp(3) #calls for help 3 times result = smaller(9, 2)

24  Every function starts with def  Then comes the name of the function  Followed by the names of the arguments in parentheses  Followed by a colon ( : )  Like loops and if statements, the code inside a function is indented one level  Call the function in other code by typing the name and the correct number of arguments in parentheses

25  Lets say we want to make a function that will draw three balls stacked on top of each other  The bottom one is green, the middle is orange, and the top is red  We want the function to take a radius and a position  The bottom ball will be drawn at the position with the radius given  The middle ball will be above it with half the radius  The top ball will be above that with one quarter the radius

26  Here's the function stacks() which takes a radius and a position and draws the balls def stacks(radius, position): sphere(radius=radius, pos=position, color=color.green) newPosition=vector(position.x, position.y + 3*radius/2,position.z) sphere(radius=radius/2, pos=newPosition, color=color.orange) newPosition=vector(position.x, position.y + 9*radius/4, position.z) sphere(radius=radius/4, pos=newPosition, color=color.red) def stacks(radius, position): sphere(radius=radius, pos=position, color=color.green) newPosition=vector(position.x, position.y + 3*radius/2,position.z) sphere(radius=radius/2, pos=newPosition, color=color.orange) newPosition=vector(position.x, position.y + 9*radius/4, position.z) sphere(radius=radius/4, pos=newPosition, color=color.red)

27  Now we can call the function several times  Each time, it will draw a stack with the size and location we specify  Result shown to the right stacks(1, vector(0,0,0)) stacks(.5, vector(2,3,1)) stacks(.2, vector(-2,1,2)) stacks(1, vector(0,0,0)) stacks(.5, vector(2,3,1)) stacks(.2, vector(-2,1,2))

28

29

30  Moore's law  Multicore computers  Complex decisions

31  Finish Project 2  Due tonight before midnight!  Reading Python Chapter 7  Think about what you want to do for your Final Project  Proposal due by 10/18


Download ppt "Week 7 - Friday.  What did we talk about last time?  Software engineering  Testing  Programming languages."

Similar presentations


Ads by Google