Lecture 21, Friday 20th April

Download Report

Transcript Lecture 21, Friday 20th April

Computing Science 1P
Lecture 21: Friday 20th April
Simon Gay
Department of Computing Science
University of Glasgow
2006/07
What's coming up?
Fri 20th April (today):
Mon 23rd April, 12.00-14.00:
Mon 23rd – Wed 25th April:
Wed 25th April:
Friday 27th April:
Mon 30th April – Wed 2nd May:
Wed 2nd May:
Fri 4th May:
lecture
FPP demo session; PRIZES!
labs: exam preparation /
lab exam preparation
lecture / tutorial
lecture: revision / questions
with Peter Saffrey
Lab Exam
No lecture
No lecture
THE END
2006/07
Computing Science 1P Lecture 21 - Simon Gay
2
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.)
2006/07
Computing Science 1P Lecture 21 - Simon Gay
3
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.
2006/07
Computing Science 1P Lecture 21 - Simon Gay
4
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.
2006/07
Computing Science 1P Lecture 21 - Simon Gay
5
Differences between Python and Java
1. The programming environment
2. Java uses static typing
3. Object-oriented programming
2006/07
Computing Science 1P Lecture 21 - Simon Gay
6
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
2006/07
Computing Science 1P Lecture 21 - Simon Gay
7
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.
2006/07
Computing Science 1P Lecture 21 - Simon Gay
8
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 + )
2006/07
Computing Science 1P Lecture 21 - Simon Gay
9
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.
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?
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.
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?
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.
2006/07
Computing Science 1P Lecture 21 - Simon Gay
12
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.
2006/07
Computing Science 1P Lecture 21 - Simon Gay
13
Example: sum of the integers from 1 to n
Python:
Java:
def sum(n):
s = 0
while n > 0:
s = s + n
n = n - 1
return s
2006/07
int sum(int n) {
int s = 0;
while (n > 0) {
s = s + n;
n = n - 1;
}
return s;
}
Computing Science 1P Lecture 21 - Simon Gay
14
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)
2006/07
Computing Science 1P Lecture 21 - Simon Gay
15
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” ]
2006/07
Computing Science 1P Lecture 21 - Simon Gay
16
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.
2006/07
Computing Science 1P Lecture 21 - Simon Gay
17
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; }
}
2006/07
Computing Science 1P Lecture 21 - Simon Gay
18
class Square {
float side;
float area() { return side*side; }
Square(float s) { side = s; }
}
Square sq = new Square(5.0);
print sq.area()
2006/07
Computing Science 1P Lecture 21 - Simon Gay
19
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
2006/07
Computing Science 1P Lecture 21 - Simon Gay
20
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; }
}
2006/07
Computing Science 1P Lecture 21 - Simon Gay
21
Inheritance
Shape s;
if (...)
s = new Square(5.0);
else
s = new Circle(4.0);
print s.area();
2006/07
Computing Science 1P Lecture 21 - Simon Gay
22
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.
2006/07
Computing Science 1P Lecture 21 - Simon Gay
23
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.)
2006/07
Computing Science 1P Lecture 21 - Simon Gay
24