Sie sind auf Seite 1von 11

P302 Introduction to Programming

1.0

PROGRAMMING CONCEPT

1.1

CONCEPT OF PROGRAMMING
1.1.1
a.

Terms
Program
Computer programs (also software programs) are instructions for a
computer
A computer requires programs to function, typically executing the program's
instructions in a central processor.
Computer programs may be categorized along functional lines: system
software and application software.
many computer programs may run simultaneously on a single computer, a
process known as multitasking
Computer programs are found either in executable form or as the source
code from which executable programs are derived
Example : Source code of a program
(C programming language)
#include <stdio.h>
int main(void)
{
puts("Hello world!");
return 0;
}

b.

Programmer
someone who writes computer software.
write, test, debug, and maintain programs, that computers must follow to
perform their functions.
conceive, design, and test logical structures for solving problems by
computer.
write programs according to the specifications determined primarily by more
senior programmers and by systems analysts.
Computer source code is often written by professional computer
programmers
referred to by the language they know, e.g. as Java programmers, or by the
type of function they perform or environment in which they work: for
example, database programmers, mainframe programmers, or Web
developers.
Ada Lovelace is popularly credited as history's first programmer.
International Programmers' Day is celebrated annually on January 7

c.

Programming language
a machine-readable artificial language designed to express computations
that can be performed by a machine, particularly a computer.
can be used to create programs that specify the behavior of a machine, to
express algorithms precisely, or as a mode of human communication.
Many programming languages have some form of written specification of
their syntax and semantics, since computers require precisely defined
instructions

1/11

P302 Introduction to Programming

1.1.2

Source code is written in a programming language that usually follows one


of two main paradigms: imperative(describes computation in terms of
statements that change a program state) or declarative (expresses the logic
of a computation without describing its control flow) programming.

Types of programming languages

Programmers write instructions in various programming languages, some


directly understandable by computers and others requiring intermediate
translation steps. Hundreds of computer languages are in use today.
4 general types: Machine language, Assembly language, High Level
language and Object-Oriented language.

a. Machine language
Machine code is a system of instructions and data executed directly by a
computer's central processing unit.
Machine dependent
lowest-level representation
Too slow and tedious to programmers
Consist of strings of numbers - NATURAL LANGUAGE 0/1
b. Assembly language
a family of low-level languages for programming computers
Using English-like abbreviations to represent elementary operations
a symbolic representation of the numeric machine codes
A utility program called an Assembler is used to translate programs to
machine lang.
Slow and many instructions for simplest task
c. High Level language
it may use natural language elements, be easier to use, or be more portable
across platforms
hide the details of CPU operations such as memory access models and
management of scope.
Compilers used to convert to machine lang.
Interpreters used to execute h-l lang prog. directly
Single statement for substantial task
d. Object-Oriented language
allows object-oriented programming techniques
Many modern programming languages now support OOP.
Languages called "pure" OO languages, because everything in them is
treated consistently as an object, from primitives such as characters and
punctuation, all the way up to whole classes, prototypes, blocks, modules,
etc. They were designed specifically to facilitate, even enforce, OO
methods - .
Languages designed mainly for OO programming, but with some
procedural elements.
Languages that are historically procedural languages, but have been
extended with some OO features.
Languages with most of the features of objects (classes, methods,
inheritance, reusability), but in a distinctly original form.
2/11

P302 Introduction to Programming

1.1.3

Languages with abstract data type support, but not all features of objectorientation, sometimes called object-based languages.

Types of programming

a.
Structured programming
Structured programming(70s-80s)
program design was based on the following advice:
To solve a large problem, break the problem into several pieces and work on each
piece separately; to solve each piece, treat it as a new problem which can itself be
broken down into smaller problems

This approach is called top-down programming.


it makes it difficult to reuse work done for other projects
Program 1

Program 2

main program data

main program data

b.
Modular programming
Also called Procedural Programming dividing a large program into a set of
subprocedures or subprograms that perform specific tasks. The modules can be
user-defined or can be taken from libraries.
A module can consist of single or multiple procedures that can consist of multiple
levels or scopes.
Modules that could support this kind of information-hiding became common in
programming languages in the early 1980s.
Modular programming

main program
data

module 1

module 2

module 3

c.
Object-Oriented programming.
OOP is a programming paradigm that uses "objects" data structures consisting
of datafields and methods and their interactions to design applications and
computer programs.
Programming techniques may include features such as information hiding, data
abstraction, encapsulation, modularity, polymorphism, and inheritance
The central concept of object-oriented programming is the object, which is a kind of
module containing data and subroutines.
an object is a kind of self-sufficient entity that has an internal state (the data it
contains) and that can respond to messages (calls to its subroutines).
3/11

P302 Introduction to Programming

Object-Oriented programming

object1
data

object2
data
object3
data
object5
data

object4
data

1.2

Concept of OOP
Everything in the language is treated consistently as an object, from primitives such as
characters and punctuation, all the way up to whole classes, prototypes, blocks,
modules
1.2.1

Advantages of OOP
1. Real-world programming

Create a basic structure of a program and keep extending the


functionality of the program as per the requirements.
2. Reusability of code

Classes can be used by several applications.

Can reduce the effort of recreating the components

Can reduce the chances of introduction of errors in various


modules

Saving in time and effort also cost effectiveness


3. Modularity of code

An object can be maintained independently of other objects

Can make modifications in the required object without affecting


the functionality of other objects.
4. Resilience to change

Enables easier maintenance of the program.

Parts of the system under development can be refined without


any major change in other parts.
5. Information hiding

Ensures data security in a program.

Limited access to information

1.2.2

Features of OOP
a. Encapsulation -The process of hiding all of the details of an object that do
not contribute to its essential characteristics. information hiding
b. Abstraction - Denotes the essential characteristics of an object that
distinguishes it from all other kinds of objects and thus provides crisply
defined conceptual boundaries, relative to the perspective of the viewer.
4/11

P302 Introduction to Programming

c.
d.
1.2.2

Inheritance - Enables to extend the functionality of an existing


class.Create class that inherits the attributes and behavior of another
class. 2 types : Single (1-1) and multiple (m-1)
Polymorphism - Enables to assign a different meaning or usage to an
entity in different contexts. Enables an entity to have more than one form

Class and object

Defines the structure and behavior of an object or a set of object.

2 types :
1. Superclass - A parent class is the one from which another class
inherits attributes and behavior.
2. Subclass - A child class is a class that inherits attributes and
behavior from a superclass.

Class:
Air ticket
Attributes:
Flight No.
Date
Time
Destination

Class:
Confirmed ticket
Inherited Attributes:
Flight No.
Date
Time
Destination
Own Attribute:
Seat No.

Class:
Requested ticket
Inherited Attributes:
Flight No.
Date
Time
Destination
Own Attribute:
Status
Example of Class

Relationships between Classes

Kind-of - Is used at the class level to describe the relationship between a


superclass and a subclass. A subclass can inherit the attributes of a
superclass. A subclass is a type of superclass.
Is-a - Relationship between objects of superclass and subclass
Part-of - When a class is an element of another class.
Has-a - Reverse of the part-of relationship

5/11

P302 Introduction to Programming

Object
Object is the basic building blocks of OOP. It is independent entities that can be
reused across various programs. It is Instance of a class and displays 3
characteristics:
State indicated by a set of attributes and the values of these attributes.
Behavior refers to a change of its state over a period of time.
Identity each object has a unique identity.

1.3

Five programming steps


a. Analyze the problem by identifying problem analysis elements:
i. What is the problem?
a. What is the given data and how to get
b. What data to be produced and how to display
c. How input data to be manipulated in order to produce output
ii.
What should you do?
a. write an algorithm
a finite sequence of instructions, an explicit, step-by-step procedure for
solving a problem, often used for calculation and data processing.
It is formally a type of effective method in which a list of well-defined
instructions for completing a task, will when given an initial state, proceed
through a well-defined series of successive states, eventually terminating
in an end-state.
b. Find the Input, Process, and Output
Example: Change meter to kilometer.
1) Start
2) Input
2.1) Value in meter
3) Process
3.1) Kilometer=meter/1000
4) Output
4.1) Value in kilometer
5) End
b. Design the program using program design tools:
i. Flowchart
a common type of chart, that represents an algorithm or process, showing the
steps as boxes of various kinds, and their order by connecting these with
arrows.
are used in analyzing, designing, documenting or managing a process or
program in various fields.

6/11

P302 Introduction to Programming

Start/end

Process

Input/Output
Procedure

Direction flow

Selection

Connector
Symbols of Flowchart
star
t
get
value in
meter
Kilometer=meter/1000

display
Kilomete
r
end
Example The use of flowchart
ii. Pseudo code
English code that is similar to real programming code.
describes statements representing the actions that occur.
helps programmers develop algorithms without having to worry about the
strict details of Java language syntax.
purpose:
1. easier for humans to understand than conventional programming
language code
2. a compact and environment-independent description of the key
principles of an algorithm.
Example 1 : Sequential structure
start
get x and y
count total x + y
display total
7/11

P302 Introduction to Programming

iii.

end
Example 2 : Selection structure
start
get x and y
count total x + y
if total > 5 display total
end
or
start
get x and y
count total x + y
if total > 5
display total
else
display y
end
Example 3 : Looping structure
start
set count to 1 and sum to 0
loop while (count <=5)
start loop
read mark
sum
sum + mark
count
count + 1
end loop
average
sum / count
display sum, average
end

IPO chart (Input-Process-Output chart)

a popular 1970s systems analysis design aid and documentation


technique for representing the modules of a system as a hierarchy and for
documenting each module.

used to develop requirements, construct the design, and support


implementation of an expert system to demonstrate automated
rendezvous. Verification was then conducted systematically because of the
method of design and implementation.

also known as the IPO+S Model is a functional model and conceptual


schema of a general system.

identifies a programs inputs, its outputs, and the processing steps required
to transform the inputs into the outputs.

I : Input - The information, ideas, and resources used


P: Processing - Actions taken upon/using input or stored material
O: Output - Results of the processing that then exit the system
S: Storage - Location(s) where material inside the system is/are placed for
possible use at a later time (optional)
8/11

P302 Introduction to Programming

iv.

Structured chart
is used to show the hierachical arragement of the modules in a structured
program. Each rectangular box represents a module. The names of the
modules are written inside the box. An arrow joins two modules that have
an invocation relationship.
is a top-down modular design tool, constructed of squares representing the
different modules in the system, and lines that connect them. The lines
represent the connection and or ownership between activities and
subactivities as they are used in organization charts.
similar in appearance to an organizational chart, but has been modified to
show additional detail.
can be used to display several types of information, but are used most
commonly to diagram either data structures or code structures

Example of Structured Chart


c. Code the program
Write a program using any programming language
Write code in paper
Type a program in computer using a right editor called source code
d. Test and debug the program
Compile a program using compiler
Execute and test a program
makes the appropriate corrections, then rechecks the program until an
acceptably low level and severity of bugs remain.
Debug a program
important parts of every programmer's job.
Programmers may continue to fix these problems throughout the life of a
program.
e. Write program documentation
o A programming tool that generates documentation intended for programmers
(API documentation) or end users (End-user Guide), or both, from a set of
specially commented source code files, and in some cases, binary files.
9/11

P302 Introduction to Programming

1.4

Type of documents:
Internal (meaning of codes -comment in program)
External (manual in using the program)

Programming errors
a. Syntax Error
refers to an error in the syntax of a sequence of characters that is intended to be
written in a particular programming language.
For compiled languages syntax errors occur strictly at compile-time. A program will
not compile until all syntax errors are corrected.
For interpreted languages syntax errors cannot be detected until runtime and it is
not necessarily simple to differentiate a syntax error from a logic error.
b.

Run-Time Error
execution time error refers to the period while a computer program is actually
executed ("run") in a computer, from beginning to termination. It may also mean
the program's running time, the duration of that period.

c. Logical Error
a semantic error is a bug in a program that causes it to operate incorrectly, but not
to terminate abnormally (or crash). A logic error produces unintended or undesired
output or other behavior, although it may not immediately be recognized as such.
occur in both compiled and interpreted languages. Unlike a program with a syntax
error, a program with a logic error is a valid program in the language, though it
does not behave as intended.

10/11

P302 Introduction to Programming

Example
Problem : Count total, average and result of multiplication of two numbers.
Solution:
a. Algorithm:
1. get 2 numbers
2. do the calculation:
i. total up 2 numbers
ii. count the average of 2 numbers
iii. multiply 2 numbers
3. display the 3 results.
Input : value of 2 numbers : x, y
Process: total = x + y ; average=(x+y)/2; multiply = x * y
Output: value of total, average and multiply
b. Pseudocode :
example 1
1. get x and y
2. count
2.1 total
x+y
2.2 average
(x+y)/2
2.3 multiply
x*y
3. display total, average, multiplication
example 2
start
get x and y
count
total
x+y
average
(x+y)/2
multiply
x*y
display total, average, multiplication.
end
c. flowchart
star
t
get x and
y
total = x + y
average = (x+y)/2
multiply = x * y
display total,
average,
multiplicatio
n
end

11/11