Object-oriented programming Chapter One
Chapter One
Introduction to Object-Oriented Programming
What is programming paradigm?
Is a fundamental style computer programming used for solving computational
problems.
Paradigms may differ in concepts and representation of elements.
Capabilities and styles of various programming languages are defined by their
supported programming paradigms.
Some programming languages are designed to follow only one paradigm, while others
support multiple paradigms.
Major Paradigms
o Non-Structured programming paradigm.
o Structural programming paradigm.
o Object oriented programming paradigm. [Our concern].
Non-structured programming paradigm
o Code is written in a single continuous program.
o Lines may be numbered or labeled.
Allow control flow to jump to any line.
o Program works on data directly.
Example: BASIC programming Language.
1 By Gizachew B.
Object-oriented programming Chapter One
Limitation
o Difficult to maintain code as size of program increases.
Difficult to add new line of code
Difficult to modify and removal lines of code.
o The code cannot be reused.
o Difficult to test the code.
Structured programming paradigm
o Program is divided into smaller subtasks/modules
This helps in modularity
Complexity made manageable.
Maintenance made easier
Control flow between modules can be defined in structure manner.
Each modules has input(s) and a defined output(s)
o It’s a good model for small-medium size projects.
Examples: C and PASCAL programming languages
Limitation
o Emphasizes more on functionality, less on data.
o Data and functionality are treated separately.
o Doesn’t represent real world.
Its representation is specific to the computer domain, hence may not be
understood by the customer and
o Data may not be secure.
2 By Gizachew B.
Object-oriented programming Chapter One
Object oriented programming paradigm
Object oriented programming paradigm is one the most popular programming paradigms
o Program represents real world objects
Objects have properties
Objects exhibit behavior and interacts with other objects
o Useful for development of enterprise applications
o It has features for data security, code reuse and maintainability
o Examples: C++ and JAVA programming language
Real life modeling is one of the most important features of object oriented programming.
Fundamental programming in Java
Section 1: Introduction
Java is one of the most popular the programming language by which we can develop
computer program.
It is intended to let application developers "write once, run anywhere" (WORA),
meaning that compiled Java code can run on all platforms that support Java without the
need for recompilation.
It was designed to have the "look and feel" of the C++ language, but it is simpler to use
than C++ and enforces an object-oriented programming model.
Java can be used to create complete applications that may run on a single computer or
be distributed among servers and clients in a network.
It can also be used to build a small application module or applet for use as part of a
Web page.
Applets make it possible for a Web page user to interact with the page.
3 By Gizachew B.
Object-oriented programming Chapter One
Section 2: Basic elements of programming in java
The following elements are basic terms in every programming language.
o Syntax
o Data types
o Keywords
o Identifiers
o Variables
o Comment
o Operators
Syntax
Syntax is a rule that we should follow to write a computer program, it depends on the
programming language.
Primitive Data type
Data type is a category of data that we use while coding. The most common data types in java
are:
o int [ small size integers]
o long [ large size integers]
o float [ small size numbers in decimal point]
o double [ large size numbers in decimal point]
o char [ single characters].
o Boolean [Return true or false]
4 By Gizachew B.
Object-oriented programming Chapter One
Keywords
Keywords are words those are reserved for the programming language. We cannot use
keywords as an identifier. Java has many keywords, among them
o All data types
o Words for conditional statement (if , else)
o Words for looping statement (while, do, For) etc.
Identifiers
Identifiers are a name will be given for items (variables, functions, arrays etc) while coding.
Rules for naming identifiers
1) Starts with small letters or capital letters or underscore.
2) It cannot start with digits or special characters.
3) Keywords cannot be an identifier.
4) Space between an identifier is not allowed.
Valid identifiers Invalid identifiers
Num 7num (start with number)
xy ?x (start with special character).
_z rectangle area (space b/n words)
xx4 int (keyword)
rectangleArea
Variables
Variables are name of memory space that we represent while coding. In order to use variables
we should declare before.
o Variable declaration means allocating a memory space by the name of the variable.
5 By Gizachew B.
Object-oriented programming Chapter One
Syntax of variable declaration
o Data type variableName;
Syntax of variable initialization
Variable initialization means giving an initial value for the variable.
o Data type variableName=value;
Comment
Comments are non-executable statements that used to increase code readability and
understandability. When the compiler gets the comment symbol it understands that the code
after the comment symbol has no effect on the program. There are two types of comment in
java
o Single line comment (symbol “//”)- used to comment a single line
o Multiple line comment (symbol ”/*…*/”)-used to comment multiple lines
Example:
o If you need to include group members inside of your program, it’s better to use multiple
line comment.
6 By Gizachew B.
Object-oriented programming Chapter One
Operators
Most common java operators:
o Arithmetic operators [+,-, *, /]
o Comparison operators [<, >, ! =, ==, <= ,>=]… used to compare values
o Logical operators [&&, ||, !]
o Assignment operators [=, +=, -=, *=, /=]… used to assign values
Assignment Meaning
x+=1 x=x+1
x*=1 x=x*1
x-=1 x=x-1
x/=1 x=x/1
Section3: Control statements
Java Control statements control the order of execution in a java program, based on data values
and conditional logic. We use control statements when we want to change the default
sequential order of execution.
Control statements; break up the flow of execution by employing decision making, looping,
and branching, enabling your program to conditionally execute particular blocks of code.
There are three main categories of control statements:
1. Selection statement (branching statement) used to do operations based on some
condition.
o If
o If….else
o Switch
2. Looping statement (iterative statement) used to do operations repeatedly.
o While
o Do…while
o For
7 By Gizachew B.
Object-oriented programming Chapter One
3. Jumping statement (transfer statement) used to jump operations based on some
condition.
o Break
o Continue
Statements and their syntax
Statements Syntax
Selection statements
If if(condition)
{
//expression
}
If…else if(condition)
{
//expression
}
Else
{
//expression
}
Switch…case Switch(variable)
Case1 value:
break;
Case2 value:
break;
.
.
.
Case n value:
break;
default:
8 By Gizachew B.
Object-oriented programming Chapter One
Loop(Iterative) statements
While while(condition)
{
//expression
}
Do…while do
{
//expression
}
while(condition)
9 By Gizachew B.