0% found this document useful (0 votes)
16 views

OOP Chapter I

Uploaded by

abrhamashenafi3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

OOP Chapter I

Uploaded by

abrhamashenafi3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Wolaita Sodo University

School of Informatics
Department of Computer Science

Object Oriented Programming (CoSc2051)

Compiled by Dawit Uta. (M. Tech.)


Computer Science Department, WSU
website address: www.davidtechnotips.com
2 Course outline

Chapter 1: Introduction to Object-Oriented Programming

1.1. Types of programming paradigms

1.2. Overview of OO principles

1.3. Editing, Compiling and Interpreting


3 Overview of computer programming

For a computer to be able to do anything (multiply, play a song, run a word


processor), it must be given the instructions to do so.

A program is a set of instructions written by humans for computers to


perform tasks.

The instructions are written in programming languages such as C, C++,


Java, etc.
4
1.1. Types of programming paradigms
 The term paradigm is can be pattern, model, or approach to programming that a language
supports.

 It is a pattern that serves as a school of thoughts for programming of computers.


 We can characterize a main programming paradigm in terms of an idea and a basic discipline.
 Main programming paradigms

 Imperative paradigm: oldest approach, closest to the actual mechanical behavior of a


computer⇒ original imperative languages were abstractions of assembly language,
Useful for quite simple programs.
 It is also called Structured or Procedural Programming
 Languages like FORTRAN, BASIC, COBOL, Pascal, C are good examples
5
 Functional paradigm: Nearly as old as imperative programming. Created by John
McCarthy with LISP (list processing) in the late 1950s.
 program as a collection of (math) functions
 Many important innovations that have been deeply influential.
 Main languages: Mathematica, Common Lisp/Scheme/Clojure, Standard,
ML/Calm/OCalm/F#, Haskell, Erlang/Elixir, Scala.
 Logical paradigm: fits extremely well when applied in problem domains that deal with the
extraction of knowledge from basic facts and relations.
 The logical paradigm seems less natural in the more general areas of computation.
 Based on axioms, inference rules, and queries.
 Program execution becomes a systematic search in a set of facts, making use of a set of
inference rules
6 Introduction to Object-Oriented Programming
 Object-oriented programming (OOP) is a fundamental and most popular
programming paradigm used by nearly every developer at some point in their career.

 OOP is a programming paradigm that relies on the concept of classes and objects.

 It is used to structure a software program into simple, reusable pieces of code blueprints
(usually called classes), which are used to create individual instances of objects.

 Object receive messages, processes data, and sends messages to other objects.

 There are many object-oriented programming languages including C++, Java, JavaScript
Python etc. •
7 Introduction to Object-Oriented Programming
 The simplest way to explain object-orientated programming is to use something like a
car as an example.

 Incorporates both encapsulation and inheritance through the class concept

 Focus is on writing good classes and on code reuse

 Examples: Shape, Circle, and Rectangle in a drawing program, Employee, Faculty, Staff
in a university personnel system

 A car has a model name, a colour, a year in which it was manufactured, an engine size and
so on.
8 Introduction to Object-Oriented Programming
 Benefits of OOP
OOP models complex things as reproducible, simple structures

Reusable, OOP objects can be used across programs

Allows for class-specific behavior through polymorphism

Easier to debug, classes often contain all applicable information to them

Secure, protects information through encapsulation


 The fundamental building blocks of an OOP program are:
 Classes, Objects, Methods, Attributes
1.2. Overview of OO principles
9

Principles of OOP: The four pillars of object oriented programming are:

 Inheritance: child classes inherit data and behaviors from parent class

 Encapsulation: containing information in an object, exposing only selected information

 Abstraction: only exposing high level public methods for accessing an object

 Polymorphism: many methods can do the same task


10 1.3. Editing, Compiling and Interpreting

 Computers do not understand the languages (C++, Java, etc) that programs are written in.

 Programs must first be compiled (converted) into machine code that the computer can run.

 A compiler is a program that translates a programming language into machine code.

 Running Programs All programs follow a simple format:

Input Execution Output


 Inputs can be from users, files, or other computer programs
 Outputs can take on many forms: numbers, text, graphics, sound, or commands to other
programs
11 1.3. Editing, Compiling and Interpreting

Multiple Compilers

 Because different operating systems (Windows, Macs, Unix) require different machine
code, you must compile most programming languages separately for each platform
12 1.3. Editing, Compiling and Interpreting
 Java interpreter
 Java is a little different.
 Java compiler produces bytecode not machine code.
 Bytecode can be run on any computer with the Java interpreter installed.
13 1.3. Editing, Compiling and Interpreting

 A Java programming environment typically consists of several programs that perform


different tasks required to edit, compile, and run a Java program.

 The following description will be based on the software development environment


provided by Oracle, the company that owns and maintains Java.

 It is currently known as the Java Platform, Standard Edition 8.0 (Java SE 8).

 Versions of Java SE are available for various platforms, including Linux, Windows, and
macOS computers. Free downloads are available at Sun’s Web site at
http://www.oracle.com/technetwork/java/.
14 1.3. Editing, Compiling and Interpreting

 In some cases, the individual programs that make up the Java SE are available in a single
program development environment, known as an integrated development environment (IDE).

 Some examples include Eclipse, jGrasp, and Oracle’s own NetBeans IDE.

 Each of these provides a complete development package for editing, compiling, and running
Java applications on a variety of platforms, including Linux, macOS, and Windows.
15 1.3. Editing, Compiling and Interpreting

 Overview of Java Programming: Java is a general-purpose, class-based,


object-oriented programming language designed for having lesser
implementation dependencies.

It is a computing platform for application development.

Java is fast, secure, and reliable, therefore, It is widely used for developing
Java applications in laptops, data centers, game consoles, scientific
supercomputers, cell phones, etc.
History of Java Programming Language
16

 Here are important landmarks from the history of the Java language:

 The Java language was initially called OAK.

 Originally, it was developed for handling portable devices and set-top boxes. Oak was a
massive failure.

 In 1995, Sun changed the name to “Java” and modified the language to take advantage
of the burgeoning www (World Wide Web) development business.
 Later, in 2009, Oracle Corporation acquired Sun Microsystems and took ownership of
three key Sun software assets: Java, MySQL, and Solaris.
 James Gosling developed the Java platform at Sun Microsystems, and the Oracle
Corporation later acquired it.
Java Programming
17

 The Java Programming Language is a high-level language. Its Syntax is similar to C and
C++, but it removes many of the complex , confusing features of C and C++.

 The Java Programming Language includes the feature of automatic storage management by
using a garbage collector.

 The Java programming language source code is compiled into the bytecode instruction set
which can be run inside the Java Virtual Machine (JVM) process.
Java Application

 In the Java Language, all of the source code is written in plain text files with the .java
extension
Java Programming …
18

 The java source code files are then compiled into .class extension files by the command
javac

 A .class file contains bytecode which is a platform independent instruction set

 The java command then runs the application

 Java application translation from source code to byte code procedure.


19 Java Programming …
 Oracle has two products that implement Java Platform Standard Edition, Java SE
development Kit and Java SE Runtime Environment
Java Programming …
20 Java Application Example
 Step 1: create a sample source code file Test.java

Step 2. Compile the Source code to generate the Class file


javac HelloWorld.java
Step 3. Run the Application
java HelloWorld
Step 4. Print out the result
Hello World!
21
Java Programming …
 The Java Development Kit (JDK) is a software development environment used for
developing Java applications and applets.

 It includes the Java Runtime Environment (JRE), an interpreter/loader (java), a compiler


(javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in
Java development.

 The Java Virtual Machine gives runtime support to the application and can make the
application independent from different hardware systems.
22 Data types and variables
 Java has primitive and non-primitive data types. Primitive data types are a special group of
data types that do not use the keyword new when initialized.
 There are eight primitive data types that are used to store data during a program's operation.
23 Data types and variables…
 Java creates them as automatic variables that are not references, which are stored in
memory with the name of the variable.
 To display Variables
 The println() method is often used to display variables. To combine both text and a
variable, use the + character:

Data Type Size Example Data Data Description


boolean 1 bit true, false true, false
1 byte
byte 12, 128 Stores integers from -128 to 127
(8 bits)
24 Data types and variables…
 Stores a 16-bit Unicode
char 2 bytes 'A', '5', '#'
character
short 2 bytes 6, -14, 2345 Stores integers from -32,768 to 32,767.
Stores integers from:
int 4bytes 6, -14, 2345
-2,147,483,648 to 2,147,483,647
Stores integers from:-9,223,372,036,854,775,808
long 8bytes 3459111, 2
to 9,223,372,036,854,775,807
Stores a positive or negative decimal number
float 4bytes 3.145, .077
from: 1.4023x10-45 to 3.4028x10+38
Stores a positive or negative decimal number
double 8bytes .0000456, 3.7
from: 4.9406x10-324 to 1.7977x10+308
25 Data types and variables…
 Variables are containers for storing data values. Variable is a name of memory location.
 In Java, there are different types of variables, for example: String- stores text, such as
"Hello". String values are surrounded by double quotes
 int- stores integers (whole numbers), without decimals, such as 123 or -123 3. float-
stores floating point numbers, with decimals, such as 19.99

 Declaring Variables and Using Literals


• The keyword new is not used when initializing a variable primitive type.
• Instead, a literal value should be assigned to each variable upon initialization.
• A literal can be any number, text, or other information that represents a value.
• Examples of declaring a variable and assigning it a literal value:
26 Data types and variables…

 There are three types of variables in Java:


i. local variable
ii. instance variable
iii. static variable
27 Data types and variables…
i. local variable: is a variable declared inside the body of the method. You can use this
variable only within that method and the other methods in the class aren't even aware
that the variable exists.
 A local variable cannot be defined with "static" keyword.
ii. instance variable: A variable declared inside the class but outside the body of the
method, is called an instance variable. It is not declared as static.
 It is called an instance variable because its value is instance-specific and is not shared
among instances.
iii. Static variable: A variable that is declared as static is called a static variable. It cannot
be local. You can create a single copy of the static variable and share it among all the
instances of the class.
 Memory allocation for static variables happens only once when the class is loaded in the
memory.
28 Data types and variables…
29 Arrays
 Non-primitive data types: The non-primitive data types include Arrays, Classes, and
Interfaces.
 An array is a data type that stores a collection of items of the same data type stored in a
container object.

 These items are sometimes referred to as the elements of the array.

 All elements must be of the same type BUT there is no restriction on which type this is.

 Length of the array is set when the array is declared, so the size is fixed.

 arrays can be used to hold a collection of int values; or a collection of char values;

 BUT they cannot be used to hold a mixture of int and char values.
Declaring arrays
30

Need to state
 the size of the array ;
 the type of each individual array element .
 The array type and size are then put together with a special new operator.
 For example
Declaring arrays
31
Example 2

The effect on computer memory of declaring an array


Naming the array elements
32

 The first element in the temperature array is temperature[0]


 The second element is temperature[1] and so on:
Initializing an array
33

 This is the only instance in which all the elements of an array can be assigned
explicitly by listing out the elements in a single assignment statement.
Accessing array elements
 Array can be used like any other variable of the given type in Java.
 The assignment operator can be used to enter a value.
 You must specify which element to place the value in.
For example: Allowing the user of the program to enter the value of the first temperature:

 Assume that i is some integer variable:


Operators in Java
34

 Java provides a rich set of operators to manipulate variables. We can divide all the Java
operators into the following groups:

 Arithmetic Operators  Assignment Operators

 Relational Operators  Bitwise Operators


 Other Operators
 Logical Operators

 Arithmetic operators: are used in mathematical expressions in the same way that they are
used in algebra. These are addition, Subtraction, multiplication, division, modulus,
increment and decrement operators.
Operators in Java
35 The following program is a simple example which demonstrates the arithmetic operators.
Operators in Java
36
 output
Operators in Java
37 Relational operators
 Java has six relational operators used to test primitive or literal numerical values.

 Relational operators are used to evaluate if-else and loop conditions

Logic Operators
Java has three logic operators used to combine
Boolean expressions into complex tests.
Operators in Java
38

 Relational operator example: output


Decision and Repetition statement
39
 Control structures alter the flow of the program, the sequence of statements that are executed
in a program.
 They act as "direction signals" to control the path a program takes.
 Two types of control structures in Java:
decision statements
Repeating / iterating statements
 Decision statements have one or more conditions to be evaluated or tested by the program,
along with a statement or statements that are to be executed if the condition is determined to
be true, and optionally, other statements to be executed if the condition is determined to be
false.
 Types of decisions statements in Java:
if statements, if else statement, if else if, else statement
switch statements
Decision and Repetition statement
40
If Statement
if (expression) {
statement;
}
rest_of_program;

expression must evaluate to a boolean value, either true or false

If expression is true, statement is executed and then rest_of_program

If expression is false, statement is not executed and the program continues
at rest_of_program
Decision and Repetition statement
41 If-Else Statement If-Else Statement Example
if (expression) {
statement1;
}
else{
statement2;
}
next_statement;

Again, expression must produce a boolean value


If expression is true, statement1 is executed and then next_statement is
executed.
If expression is false, statement2 is executed and then next_statement is
executed.
Decision and Repetition statement
42 Chained If-Else Statement syntax Chained If-Else Statement Example

if-else chains can be sometimes be rewritten as a “switch” statement. switches are usually
simpler and faster
Decision and Repetition statement
43 switch Statement switch Statement Example
The switch statement enables you to test
several cases generated by a given expression.
switch (expression) {
case value1:
statement1;
case value2:
statement2;
default:
default_statement;
}
Every statement after the true case is executed
The expression must evaluate to a char, byte,
short or int, Character, Byte, Short, Integer,
String, enum but not float, or double or long
The break statement tells the computer to exit the switch statement
44 Decision and Repetition/Itration statement
 Iteration is the form of program control that allows us to repeat a section of code.
 This form of control is often also referred to as repetition.
 The programming structure that is used to control this repetition is often called a loop.
 There are three types of loops in Java:
for loop;
while loop;
do…while loop.
The ‘for’ loop
 If we wish to repeat a section of code a fixed number of times we would use Java's for
loop.
 The for loop is usually used in conjunction with a counter to keep track of how many
times we have been through the loop so far:
The ‘for’ loop
45 for (init_expr; loop_condition; increment_expr) {
statement;
}
The control of the for loop appear in parentheses and is made up of three parts:
1. The first part, the init_expression, sets the initial conditions for the loop and is executed
before the loop starts.
2. Loop executes so long as the loop_condition is true and exits otherwise.
3. The third part of the control information, the increment_expr, is usually used to
increment the loop counter. This is executed at the end of each loop iteration.
Example

output
Decision and Repetition statement
46

 while loop: while loop statement in Java programming language repeatedly executes a
target statement as long as a given condition is true.
while Statement syntax  Here, statement(s) may be a single statement or a block of
statements.
 The condition may be any expression, and true is any non
zero value. When executing, if the boolean_expression result
is true, then the actions inside the loop will be executed. This
will continue as long as the expression result is true.
 When the condition becomes false, program control passes
to the line immediately following the loop
Decision and Repetition statement
47 while Statement Example
output

Decision and Repetition statement
48

 do...while: A do...while loop is similar to a while loop, except that a do...while loop is
guaranteed to execute at least one time.
do…while Statement syntax  Notice that the Boolean expression appears at the end of
the loop, so the statements in the loop execute once before
the Boolean is tested.
 If the Boolean expression is true, the control jumps back up
to do statement, and the statements in the loop execute
again.
 This process repeats until the Boolean expression is false.
49 Decision and Repetition statement
 do…while Statement Example
output
50

You might also like