Introduction to subject

Download Report

Transcript Introduction to subject

310417 2000 WEEK 1
What is a Computer Program?
An algorithm that can be executed on a computer is a
program. A program usually contains several
algorithms.
A program is DATA plus PROCESSING.
Examples
• the set of calculations required to work out your
tax payable (algorithms for separate parts, too, e.g.
gross income, deductions, Medicare)
• the calculation to work out whether this is a leap
year
1
310417 2000 WEEK 1
A Program (Calculate Interest on
a Bank Account)
calculate interest
get balance
get interest rate
interest = balance * interest rate
1000
balance
2
5.0
interestRate
variables
50
interest
310417 2000 WEEK 1
History of Programming
Languages
5GLs
artificial intelligence
4GLs
ORACLE, SEQUEL, INGRES, ...
HIGH-LEVEL
LANGUAGES
ForTran, COBOL, C, C++,
LISP, Pascal, Java, ...
ASSEMBLER LANGUAGES
MACHINE CODE
3
310417 2000 WEEK 1
Different Programming
Languages
Machine code
000 100C
006 300D
007 200D
100
110
COBOL
FORTRAN
LISP
Pascal
C, C++, Java
4
Assembler
LDA VALUE
ADA TOTAL
STA TOTAL
DW1 TOTAL
DW1 VALUE
ADD VALUE TO TOTAL
TOTAL = TOTAL + VALUE
(set! total (+ total value))
total := total + value
total += value;
310417 2000 WEEK 1
Program Translation
A program written in a high level language must
be translated into machine code (or executable
code) before the computer can run it (i.e.,
execute the instructions). The program as
written is called the source code.
The translation is done by a program called a
compiler or an interpreter.
A compiler attempts to translate an entire
program and gives a list of the problems it
found (if any). This is a batch process.
An interpreter translates one instruction at a time
and causes it to be executed at once. This is an
interactive process.
5
310417 2000 WEEK 1
Translation of Java Programs
Java programs are designed to be able to run on
any kind of computer when downloaded from
the Web.
With most languages, that would mean
downloading source code for the program and
having a compiler translate it into the machine
code for your machine. The user would have to
tell the machine to compile the source code
before running the program.
Java uses bytecodes to solve this problem.
6
310417 2000 WEEK 1
Java Bytecodes
The Java compiler translates the source code to
bytecodes, the machine code for an imaginary
machine. The bytecodes are downloaded, then
translated by an interpreter on the local
machine to its own machine code.
This may sound like no improvement, but it is
easier to write a bytecode interpreter for a
particular machine than a compiler for source
programs. Each type of machine has its own
bytecode interpreter for Java. These are
downloadable from the Web, and included in
web browsers.
7
310417 2000 WEEK 1
Executing a Java Program
source
code
Editor
Compiler
On
remote
computer?
bytecodes
(Maybe)
Bytecode
Interpreter
bytecodes
8
Bytecode
Verifier
copy of
bytecodes
On local
computer
310417 2000 WEEK 1
Objects
We are all familiar with the idea of an object. We
are surrounded by them: cars, books, people,
houses, cats, etc.
Objects have attributes, e.g. colour, size, age,
name.
Objects also have behaviour. They can do things,
e.g. grow, breathe, run, stop.
9
310417 2000 WEEK 1
Attributes and Behaviour
Car
Cat
10
Attributes
Behaviour
Registration number
Make
Model
Year
Colour
Start
Stop
Turn
Accelerate
Flash lights
Name
Breed
Colour
Age
Weight
Sleep
Eat
Purr
Climb
Scratch
310417 2000 WEEK 1
Abstraction and Models
The previous slide is not intended to suggest that
every object will have 5 attributes and 5
behaviours. We can all think of many more
attributes and behaviours of cars and cats.
In a program, we choose the attributes and
behaviours that are relevant to the problem we
are trying to solve.
This process of selecting some aspects of the
objects and ignoring the irrelevant ones is
called abstraction. We create a model that is a
simplification of the real situation, strictly
relevant to the problem to be solved.
11
310417 2000 WEEK 1
Simulation
When we write a program, we are often
simulating something that happens in real life.
The models we create in our program
correspond to real life objects, e.g. customers,
invoices, receipts.
Sometimes the simulation is more explicit, e.g. a
program that models a set of traffic lights to
work out the optimum times for changing them
to keep the traffic flowing smoothly.
The object-oriented style (or paradigm) of
programming is very well suited to simulations.
12
310417 2000 WEEK 1
Object-Oriented Programs
The object-oriented programming paradigm uses
objects to model a situation. A program can be
written in a non-object-oriented way, but the
object-oriented style is becoming increasingly
popular for three main reasons:
• programs are becoming more and more
complex, and the OO style handles this best
• the OO style makes it easy to re-use existing
programs or parts of them
• the OO style makes programs much easier to
maintain.
13
310417 2000 WEEK 1
Classification
We classify objects into groups according to their
common attributes and behaviour.
Let's say we want to classify objects into balls and
not-balls. What are the common attributes and
behaviour of something we would classify as a
ball? How would you recognise a ball if you
saw one?
14
310417 2000 WEEK 1
Classes
In a computer program, we use classes to describe
similar objects. A class is a description of what
an object would look like if we had one. It is
based on common attributes and behaviour of
objects.
Radio
currentStation
volume
turnOn
turnOff
changeStation
increaseVolume
decreaseVolume
15
310417 2000 WEEK 1
Instances of Classes
A class is a template that describes what an object
would look like if we had one.
We instantiate the class to create an instance, i.e.
an object of that class.
The object is an instance of the class.
We can have many instances of the same class.
Each object has the same attributes and the
same behaviour. However, the values of the
attributes might be different.
16
310417 2000 WEEK 1
Identity
We can have several different objects that have
the same attributes and behaviour (even the
same state) but are still different objects.
An object has identity. We can give it a name to
distinguish it from the others (e.g. ball1, ball2).
17
310417 2000 WEEK 1
State of an Object
The state of a particular object is the values of its
attributes at any particular moment. For
example, a cat might have the following state:
Name: Max
Breed: American Shorthair
Colour: Black, brown & white
Age: 3 years, 4 months, 8 days
Weight: 5.1 kg
Some of these attributes change constantly.
Others stay the same for long periods or
forever.
18
310417 2000 WEEK 1
A Person Class
Let's imagine we need a model of a person to
solve some problem. For the purposes of this
problem, it is enough to let a person have just a
name and an age.
Person
name
age
display
getAge
getName
setAge
setName
19
310417 2000 WEEK 1
An Object Hides Its Attributes
name
age
If you want to find out the value of an
attribute of an object, you have to ask the
object to tell you.
20
310417 2000 WEEK 1
Message Passing
To get an object to do something it knows how to
do, you have to send it a message requesting
that behaviour.
getName
name
age
"Jack"
bestFriend
21
310417 2000 WEEK 1
Interfaces
How do we know what we can ask an object to
do?
The object has a public interface that advertises
what it can be asked to do. It also indicates
how to ask for these behaviours.
What the outside world sees is the object's
interface.
display
setAge
getAge
setName
getName
22
310417 2000 WEEK 1
Sending a Message to an Object
There are 3 parts to a message sent to an object:
• the name of the object that is the receiver
• the action that the receiver is requested to take
• in parentheses, any extra information the
receiver needs to know.
In Java, the syntax for asking the person called
bestFriend to tell you its name would be:
bestFriend.getName();
23
310417 2000 WEEK 1
Passing Information in a Message
When an object needs to know some extra
information in order to do what you want, you
can pass it that information with the message.
If you decide that Jack is no longer your best
friend but Sally now is, and want the object
bestFriend to change its name to "Sally", you
need to send it a message that tells it to change
its name, and also what to change it to.
The extra information is called arguments or
parameters.
bestFriend.setName("Sally");
24
310417 2000 WEEK 1
Data Types in Java
Data is the information that a program has to
work with.
Data is of different types. The type of a piece of
data tells Java what can be done with it, and
how much memory needs to be put aside for it.
When we create a variable in Java, we need to
specify:
• the type of the value we want to put in there,
and
• the name we will use for that variable.
25
310417 2000 WEEK 1
Type String
A String is a collection of characters (e.g.
"Seree").
String name = "Seree";
Seree
name
A String value is always written in double
quotes. You can have an empty String, shown
as "".
A String has an interface that includes:
• change itself to upper/lower case
• tell you how long it is (how many characters)
• give you the character at a specified position
26
310417 2000 WEEK 1
Type int
An int is a whole number (e.g. 21). You can do
arithmetic with an int.
int age = 21;
addition +
subtraction multiplication *
division /
modulus %
27
15
age
int
age
2 *
age
age
age = 15;
+ 3
age - 4
/ 2
% 10
310417 2000 WEEK 1
Objectives for Week 1
By the end of this week, students should be able
to:
• explain the difference between an object and a
class, and what instantiation means
• describe the components of an object
• explain the concepts of abstraction, state,
identity, message passing, parameters, interface
• give examples of variables of types String and
int
• download and install BlueJ
• use BlueJ to create objects from existing classes,
inspect their state and invoke their behaviours
28
310417 2000 WEEK 1
310417 Concepts from Week 1
Programming
High level
languages
Software
Problem
solving
Algorithm
Internet
Translation
Compiler
Interpreter
World Wide
Web
Pseudocode
Bytecodes
Data
Processing
29
Browser
310417 2000 WEEK 1
310417 Concepts So Far
High level
languages
Programming
Software
Problem
solving
Algorithm
Objectoriented
Object
Pseudocode
Data
Types
30
Translation
Class
Identity
Instantiation
Attributes
Processing
Java
State
Parameter
Behaviour
Message
passing