Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.

Similar presentations


Presentation on theme: "Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07."— Presentation transcript:

1 Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07

2 Computing Science 1P Lecture 21 - Simon Gay2 What's coming up? Fri 20 th April (today):lecture Mon 23 rd April, 12.00-14.00:FPP demo session; PRIZES! Mon 23 rd – Wed 25 th April:labs: exam preparation / lab exam preparation Wed 25 th April:lecture / tutorial Friday 27 th April:lecture: revision / questions with Peter Saffrey Mon 30 th April – Wed 2 nd May:Lab Exam Wed 2 nd May:No lecture Fri 4 th May:No lecture THE END

3 2006/07Computing Science 1P Lecture 21 - Simon Gay3 Life after Python Python is just one of a huge range of programming languages that have been introduced during the last 50 years. If you continue with Computing Science in your second year, you will learn a different language: Java. (You will also learn C in third year, and perhaps others.)

4 2006/07Computing Science 1P Lecture 21 - Simon Gay4 John Backus, inventor of Fortran John Backus developed Fortran, one of the first high-level programming languages, in 1956. John Backus died on 17th March 2007.

5 2006/07Computing Science 1P Lecture 21 - Simon Gay5 Python vs Java The basic ideas of programming, learnt in Python, are also essential for Java: variables statements and expressions sequencing, loops, conditionals, functions data structures: lists, dictionaries problem-solving and planning testing and debugging But there are some important differences, too.

6 2006/07Computing Science 1P Lecture 21 - Simon Gay6 Differences between Python and Java 1. The programming environment 2. Java uses static typing 3. Object-oriented programming

7 2006/07Computing Science 1P Lecture 21 - Simon Gay7 The programming environment Recall the difference between an interpreter and a compiler. An interpreter looks at your program one statement at a time, and does whatever it should do. Example: the interpreter for the drawing language, in the Unit 13 exercise. A compiler translates your whole program into machine language, giving you something that can be executed independently. Example: most software development

8 2006/07Computing Science 1P Lecture 21 - Simon Gay8 The programming environment The Python system we have been using (IDLE) is an interactive interpreter: you can type programs straight into the shell window. The normal way of using Java is with a compiler. There is nowhere you can simply type expressions to see what they do. Java programmers usually use an integrated development environment (IDE) such as Eclipse. In Level 2 CS, you can expect a Java IDE which is more powerful than IDLE, but may be a little harder to learn.

9 2006/07Computing Science 1P Lecture 21 - Simon Gay9 Static typing Types are very important in almost all programming languages. Examples in Python: int, string, float, list, … All data naturally has a type and this determines which operations make sense. 1 + “hello” is meaningless (unless we invent some new meaning for + )

10 2006/07Computing Science 1P Lecture 21 - Simon Gay10 Static typing The programmer might try 1 + “hello” (by mistake?) so what should the language do? Three possibilities. 1. Untyped languages (e.g. machine language) Ignore the problem. Just do the calculation of + on whatever bit-patterns are given. Meaningless answers.

11 2006/07Computing Science 1P Lecture 21 - Simon Gay11 Static typing The programmer might try 1 + “hello” (by mistake?) so what should the language do? 2. Dynamically typed languages (e.g. Python) At runtime, before doing a + operation, check that the types of the operands are suitable. If not, produce an error message. Some type errors might remain undetected for a long time.

12 2006/07Computing Science 1P Lecture 21 - Simon Gay12 Static typing The programmer might try 1 + “hello” (by mistake?) so what should the language do? 3. Statically typed languages (e.g. Java) At compile time, check the types of the operands of every + operation. If they are wrong, produce an error message and do not generate an executable program. Sometimes the compiler thinks there is an error but the programmer can see that it is OK.

13 2006/07Computing Science 1P Lecture 21 - Simon Gay13 Static typing in Java Java requires many type declarations. The type of every variable must be declared. The type of every function parameter must be declared. The result type of every function must be declared.

14 2006/07Computing Science 1P Lecture 21 - Simon Gay14 Example: sum of the integers from 1 to n Python: def sum(n): s = 0 while n > 0: s = s + n n = n - 1 return s Java: int sum(int n) { int s = 0; while (n > 0) { s = s + n; n = n - 1; } return s; }

15 2006/07Computing Science 1P Lecture 21 - Simon Gay15 Object-oriented programming Our programming with Python: we have data we have functions which operate on data Object-oriented programming: an object combines data and operations on it (which are called methods)

16 2006/07Computing Science 1P Lecture 21 - Simon Gay16 Objects and methods We have already seen that Python uses objects and methods. d = { “a”:10, “b”:20, “c”: 30 } d.keys() >>> [ “a”, “b”, “c” ]

17 2006/07Computing Science 1P Lecture 21 - Simon Gay17 Example of objects: shapes Imagine a program working with shapes: squares, circles, … Each shape could be represented by an object. Possible methods: area, perimeter It’s useful to be able to write s.area() even if we don’t know what kind of shape s is.

18 2006/07Computing Science 1P Lecture 21 - Simon Gay18 Classes Most object-oriented languages (including Java) use the idea of a class. Think of a class as a type for objects. class Square { float side; float area() { return side*side; } Square(float s) { side = s; } }

19 2006/07Computing Science 1P Lecture 21 - Simon Gay19 class Square { float side; float area() { return side*side; } Square(float s) { side = s; } } Square sq = new Square(5.0); print sq.area()

20 2006/07Computing Science 1P Lecture 21 - Simon Gay20 Inheritance A very important idea in object-oriented programming. It allows us to define a new kind of object, based on an old kind of object. Example: Shape is a generic shape Square is a particular kind of Shape Circle is another particular kind of Shape

21 2006/07Computing Science 1P Lecture 21 - Simon Gay21 Inheritance class Shape { float area() { return 0.0; } } class Square extends Shape { float side; float area() { return side*side; } Square(float s) { side = s; } } class Circle extends Shape { float radius; float area() { return pi*radius*radius; } Circle(float r) { radius = r; } }

22 2006/07Computing Science 1P Lecture 21 - Simon Gay22 Inheritance Shape s; if (...) s = new Square(5.0); else s = new Circle(4.0); print s.area();

23 2006/07Computing Science 1P Lecture 21 - Simon Gay23 Object-oriented programming … turns out to be a very powerful combination of ideas Classes and inheritance give a very natural way of modelling real-world entities. “ x is just like y except that…” First object-oriented language: Simula (1962-1967) Also influential: Smalltalk (1980) Became standard from late 1980s onwards.

24 2006/07Computing Science 1P Lecture 21 - Simon Gay24 Object-oriented programming in Python … is possible, and we have already seen that lists and dictionaries are objects. Tkinter is very object-oriented. Because of Python’s dynamic type system, the role of classes is not quite the same as in Java. Java seems to be a better language for teaching object-oriented programming. (Python seems better for teaching the fundamental concepts of imperative programming.)


Download ppt "Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07."

Similar presentations


Ads by Google