Computer Program

advertisement
CST 221
OBJECT ORIENTED
PROGRAMMING(OOP)
(2 CREDITS)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY
FACULTY OF SCIENCE & TECHNOLOGY
UNIVERSITY OF UWA WELLASSA
1
Introduction to Computer
Programming
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY
FACULTY OF SCIENCE & TECHNOLOGY
UNIVERSITY OF UWA WELLASSA
2
• Computer Program
A set of instructions written in a computer understandable format.
• Computer Programming Language
An artificial language that can be used to control the behavior of a
computer.
• Source Code
Any series of statements written in a computer programming
language.
• Computer Programming
The process of writing, testing, and maintaining the source code of
computer programs.
3
•High Level Programming Languages
The source code cannot be directly understood by a computer.
Therefore it must be converted into a computer understandable
(executable) form before it can run.
• The translation of the source code into machine code can be
done in two ways
Compilation – translates the entire high level language source code(into
an executable file) before the execution.
Interpretation – translates the high level language source code line by
line at the execution time.
4
Compilers and Interpreters
•Computers cannot directly understand the source code.
Instead they can understand their own language known as
“Machine Language” or “Machine Code”.
•Different computer platforms (hardware + operating system) have
different machine languages.
For example: each of Microsoft Windows on Intel Pentium hardware and
Mac OS X on Apple Macintosh hardware would not understand each others
machine language.
•Therefore a programming language's source code needs to be
converted into Machine Code before it can be use to control the
behavior of a computer.
•Need another computer program to make this conversion
Compiler.
Interpreter.
5
Compilers and Interpreters…
Compiler
•Compilers convert the entire source code into machine
before the execution.
•The machine code is executed only after all the source
code has been converted.
•Since, different computer platforms have different
machine languages, source code compiled into machine
code for one architecture might not run on another.
Interpreter
•Does not convert the entire source code at once.
•Converts one source code statement, executes the resulting
machine code and then coverts the next source code
statement and so on.
6
Computer Program Can be treated in 3 basic processes.
• Steps available in a program
1. How and from where to get input data.
2. What are the operations and how to perform them on the input data.
3. How and where to output the resulting data.
• Data Can be
1. Numeric
considered in 3 types
2. Character
3. Boolean
7
Data Types
Data Type
Size
Range
byte
8 bits
-256 to 255
short
16 bits
-32,768 to 32767
int
32 bits
-2,147,483,648 to 2,147,483,647
long
64 bits
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,808
float
32 bits
double
64 bits
character
16 bits
boolean
Represent numbers with fractional precision
Can store one character
Can hold logical values; true and false
8
Data Types…
Is a classification identifying one of various types of data
consists of
1. set of data values that can be under the data type.
2. set of operations that can be applied to those data values.
Example: Numeric Data Types
1. Integer
contains positive and negative whole numbers including
zero.
2. Real
All the numbers on the number scale including all the
Integers and all the fractions.
9
Data Types…
Operations that can be performed on numeric data
−Integer : Addition (+), Subtraction (-), Multiplication (*), Division
(/) and Modulus (%).
−Real : Addition (+), Subtraction (-), Multiplication (*) and Division
(/)
10
Operations, Operators and Operands.
Ex: an arithmetic operation
operator
10+5=15
operand
.
Operation
Arithmetic Relational
Logical
11
Operations, Operators and Operands..
Operators
1. Arithmetic Operators
2. Relational Operators
12
Operations, Operators and Operands..
Result of a relational operation is always true or false.
Ex:
3= = 4 false
3<=4 true
3>=4 false
3<>4 true
3<4 true
3>4 true
3. Logical Operators
.
13
Other Operators
•Assignment Operators
Assignment operators are used to assign the values of
an expression to a variable.
Ex:
a=10;
.
14
Other Operators…
•Increment and Decrement Operators
. The Increment Operator ++ adds 1 to the operand while
subtract 1.They can by used in the following ways.
m++
++m is equivalent to m = m + 1; (or m += 1;)
m– –
– –m is equivalent to m = m – 1; (or m –=1;)
15
Other Operators…
• Bitwise Operator
These
. operators are used for testing the bits, or shifting them to
the right or left.
16
Other Operators…
• Special Operators
Java. supports some special operators of interest such as instanceof
operator and member selection operator (.)
−Instanceof Operator
The instanceof is an object reference operator and
returns true if the object on the left hand side is an
instance of the class given on the right-hand
side.
This
allows
to
determine whether the object
belongs to a particular class or not.
E.g.
st1 instanceof Student
is true if the object st1 belongs to the class Student ;
17
otherwise it is false.
Other Operators…
−Dot Operator
The dot operator (.) is used to access the instance
.
variables and methods of class objects.
E.g.
student1.age // Reference to the variable age
employee1.calSalary() // Reference to the method
calSalary()
18
Expressions
An expression is any legal combination of symbols that represents a value.
Each programming language and application has its own rules for what is legal
and illegal.
For example, in C programming language x+5 is an expression.
Every expression consists of at least one operand and can have one or more
operators. Operands are values, whereas operators are symbols that represent
particular actions.
Ex:
In the expression x+5 x and 5 are operands + is an operator.
Ex: 3 + 4 * 5 / 7 – 2
It is necessary to have rules which define the order in which the operations
are carried
out such rules are called the Order of Precedence of operators.
06/26/10
Order Of Precedence
When writing complex expressions with several operands and
operators it is possible to have doubts about which operation is
evaluated first and which later.
For Example the expression
a=6+8/2
could be considered in two ways,
1. a = 6 + ( 8 / 2 ) → with a result of 10
2. a = ( 6 + 8 ) / 2 → with a result of 7
06/26/10
Order Of Precedence
1. a = 6 + ( 8 / 2 ) → result is 10
06/26/10
Syntax and Semantics
Syntax
The syntax of a programming language are the grammatical rules
which controls the ways in which words, expressions and statements
may be formed and combined.
E.g. : in C
int a
int a;
syntactically incorrect. (syntax error)
syntactically correct.
Every statement should end up with a semicolon.
This type of errors are also known as compile time errors.
06/26/10
Syntax and Semantics…
Semantics
The semantics of a language are the rules which govern the meaning.
E.g.: in C
int a=5;
int b=5;
int c=10;
int d=0;
d=c/(a-b); semantically incorrect.
here (a-b)=0 therefore d=c/0 this is semantically incorrect.
These errors are also known as “Run time errors”.
(Fatal errors, Logical errors).
06/26/10
Syntax and Semantics…
Semantics
The semantics of a language are the rules which govern the meaning.
E.g.: in C
int a=5;
int b=5;
int c=10;
int d=0;
d=c/(a-b); semantically incorrect.
here (a-b)=0 therefore d=c/0 this is semantically incorrect.
These errors are also known as “Run time errors”.
(Fatal errors, Logical errors).
06/26/10
Data input & Storage
The input data for a computer are need to be stored in the main
memory for the processing. Once this data is stored, it is necessary to
have an identifier to identify the place where the data is.
06/26/10
Data input & Storage…
Identifiers
An identifier is the name by which the data value can be referenced.
The names which
are associated with the stored data values in the memory.
2 types
constants
variables.
•Constants
An identifier is a constant if it is associated with the same
data value.
•Variable
An identifier is a variable if its associated data value is
allowed to change.
06/26/10
Data input & Storage…
Variable Declaration
Reserve a place in the memory before storing data.
At the declaration two things should be mentioned.
1. Variable name (identifier name):
− name assigned to the location where the value is stored.
2. Data type:
− to declare the amount of memory to be reserved.
E.g.: in C
Int a;
integer → 16 bits.
06/26/10
Data input & Storage…
Data types can be categories into two groups
• Simple data type:
also called predefined data type by the language.
In C: Int float, double, char
• Abstract data type:
a collection of simple data type.
E.g.: Array
structures
struct student
{
int regNo;
int age;
}
06/26/10
Data input & Storage…
Variable initialization
Assigning a value to a variable before using it for the operations is
called variable
initialization.
int a; → declaration
a=10; → initialization.
After storing data in the main memory Algorithms are used to
perform some functionality to produce a desired output.
06/26/10
Download