Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science...

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

Transcript of Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science...

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

Computing Science 1P

Lecture 21: Friday 20th April

Simon GayDepartment of Computing Science

University of Glasgow

2006/07

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 2

What's coming up?

Fri 20th April (today): lectureMon 23rd April, 12.00-14.00: FPP demo session; PRIZES!Mon 23rd – Wed 25th April: labs: exam preparation /

lab exam preparationWed 25th April: lecture / tutorialFriday 27th April: lecture: revision / questions

with Peter SaffreyMon 30th April – Wed 2nd May: Lab ExamWed 2nd May: No lectureFri 4th May: No lecture

THE END

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 3

Life after Python

Python is just one of a huge range of programming languagesthat 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.)

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 4

John Backus, inventor of Fortran

John Backus developed Fortran, one ofthe first high-level programming languages, in 1956.

John Backus died on 17th March 2007.

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 5

Python vs Java

The basic ideas of programming, learnt in Python, are alsoessential for Java:

variablesstatements and expressionssequencing, loops, conditionals, functionsdata structures: lists, dictionariesproblem-solving and planningtesting and debugging

But there are some important differences, too.

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 6

Differences between Python and Java

1. The programming environment

2. Java uses static typing

3. Object-oriented programming

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 7

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 machinelanguage, giving you something that can be executedindependently.

Example: most software development

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 8

The programming environment

The Python system we have been using (IDLE) is an interactiveinterpreter: you can type programs straight into the shell window.

The normal way of using Java is with a compiler. There isnowhere you can simply type expressions to see what they do.

Java programmers usually use an integrated developmentenvironment (IDE) such as Eclipse.

In Level 2 CS, you can expect a Java IDE which is morepowerful than IDLE, but may be a little harder to learn.

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 9

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 whichoperations make sense.

1 + “hello” is meaningless (unless we invent some new meaning for + )

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 10

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 whateverbit-patterns are given. Meaningless answers.

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 11

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 typesof the operands are suitable. If not, produce an error message.

Some type errors might remain undetected for a long time.

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 12

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 ofevery + operation. If they are wrong, produce an errormessage and do not generate an executable program.

Sometimes the compiler thinks there is an error but theprogrammer can see that it is OK.

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 13

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.

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 14

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;}

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 15

Object-oriented programming

Our programming with Python:

we have datawe have functions which operate on data

Object-oriented programming:

an object combines data and operations on it(which are called methods)

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 16

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” ]

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 17

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 knowwhat kind of shape s is.

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 18

Classes

Most object-oriented languages (including Java) use the ideaof 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; }}

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 19

class Square { float side;

float area() { return side*side; }

Square(float s) { side = s; }}

Square sq = new Square(5.0);

print sq.area()

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 20

Inheritance

A very important idea in object-oriented programming.

It allows us to define a new kind of object, based on an oldkind of object.

Example: Shape is a generic shape Square is a particular kind of Shape Circle is another particular kind of Shape

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 21

Inheritanceclass 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; }}

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 22

Inheritance

Shape s;

if (...) s = new Square(5.0);else s = new Circle(4.0);

print s.area();

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 23

Object-oriented programming

… turns out to be a very powerful combination of ideas

Classes and inheritance give a very natural way of modellingreal-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.

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

2006/07 Computing Science 1P Lecture 21 - Simon Gay 24

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 classesis not quite the same as in Java.

Java seems to be a better language for teachingobject-oriented programming.

(Python seems better for teaching the fundamental concepts ofimperative programming.)