Chapter 1 - OOP For CS
Chapter 1 - OOP For CS
2
Programming Paradigm’s
a way to classify programming languages based on their style
and approach to solving problems.
Different paradigms provide different ways of thinking about and
structuring code.
Paradigm's a style, or “way,” of programming.
Some languages make it easy to write in some paradigms but not
others.
different ways or styles in which a given program or
programming language can be organized.
Each paradigm consists of certain structures, features, and
opinions about how common programming problems should be
tackled.
3
Cont’d …
4
Cont’d …
Procedural Programming
• focuses on functions and procedures that manipulate data.
• follows a top-down approach during the designing of a
program.
• It gives importance to the concept of the function and divides
the large programs into smaller parts or called as functions.
• straightforward.
• It follows a step-by-step approach in order to break down a
task into a set of variables and routines via a sequence of
instructions.
• Examples:
• ALGOL, COBOL, BASIC, PASCAL, FORTRAN, C.
5
Cont’d …
Example:
public class ProceduralEx {
public static void main(String[] args) {
int a = 5;
int b = 3;
int sum = add(a, b);
System.out.println("Sum: " + sum);
}
Structured Programming
• Also known as modular programming.
• characterized by the use of procedures or functions, control flow
constructs, and well-defined, modular structures.
• It improve code readability, maintainability, and reliability by
avoiding unstructured practices like goto statements.
• intends to optimize the code by using the program control flow
constructs , decision making (If , If Then , Else ) constructs and
the iteration constructs (For , while loops ), blocks and the
functions.
• code will execute the instruction by instruction one after the other.
7
Cont’d …
Example:
public class StructuralEx {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
else {
System.out.println("The number is not positive.");
}
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}
}
}
8
Cont’d …
Declarative Programming
• focuses on describing what the program should do rather than
how it should be done.
• Programming by specifying the result you want, not how to get it.
• SQL is an example in the context of databases
Functional Programming
• Functional programming emphasizes immutability and the use of
pure functions.
Imperative Programming
• Programming with an explicit sequence of commands that update state.
9
Cont’d …
Object-oriented Programming
• OOP is based on objects and classes, promoting the
organization of data and methods into objects that interact with
each other.
• It is based on the concept of object.
• An object contains data in the form of fields that are known as
attributes and the procedures are known as methods.
• programs are divided into what are known as objects.
• It follows the bottom-up flow of execution.
• It introduces concepts like data abstraction, inheritance, and
overloading of functions and operators overloading.
• Example: Java, C++, Python, C#, Perl, Kotlin, Ruby
10
Cont’d …
Example:
class Dog {
String name;
void bark() {
System.out.println(name + " barks!");
}
}
public class OOPEx {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.name = "Buddy";
myDog.bark();
}
}
11
Cont’d …
Some Advantages
of OOP Paradigms
12
Java
What is Java?
13
Cont’d …
Java:
• Programming language and a platform.
• Developed by sun microsystems (James Gasoling)
• a high level, robust, secured and object-oriented programming language.
• based C/C++
• widespread acceptance
Platform:
• Any hardware or software environment in which a program runs, is known
as a platform.
• Since Java has its own runtime environment (JRE) and API, it is called
platform.
14
Where it is used?
According to Sun, 3 billion devices run java.
There are many devices where java is currently used. Some of them are as
follows:
• Desktop Applications such as acrobat reader, media player, antivirus etc.
• Web Applications
• Enterprise Applications such as banking applications.
• Mobile
• Embedded System
• Smart Card
• Robotics
• Games etc.
15
Types of Java Applications
There are mainly 4 type of applications that can be created using java
programming:
1. Standalone Application
• It is also known as desktop application or window-based application.
• An application that we need to install on every machine such as media
player, antivirus etc.
• AWT and Swing are used in java for creating standalone applications.
2. Web Application
• runs on the server side and used to create dynamic pages
• Currently, servlet, JSP, struts, JSF etc. technologies are used for creating
web applications in java.
16
Cont’d …
3. Enterprise Application
• An application that is distributed in nature, such as banking
applications etc.
• It has the advantage of high level security, load balancing and
clustering.
• In java, EJB is used for creating enterprise applications.
4. Mobile Application
• An application that is created for mobile devices.
• Currently Android and Java ME are used for creating mobile
applications.
17
History of Programming
19
Cont’d …
BCPL:
• In 1967, Created by Martin Richards B:
in 1967 • Created by Ken Thompson In
• Basic Combined Programming 1970
Language) • created for UNIX OS at Bell
• Primarily BCPL is developed for Laboratories.
system software • Both BCPL and B were “type
• Operating systems and less” languages
compilers • It was derived from BCPL
• Designed for primarily non-
• high portability.
numeric applications.
• It is the successor to
the CPL programming language.
20
History of C
Dennis Ritchie
• He is an American computer scientist.
• He created the C programming language and, with
long-time-colleague ken Thompson, the UNIX OS.
21
Cont’d …
C was evolved from ALGOL, BCPL and B.
22
History of C++
• C++ Development started in 1979.
• It is an extension of C language.
23
Cont’d …
Simula was first language to support object-oriented programming
language (OOP).
24
Cont’d …
25
History of Java
• Java started out as a research project.
26
Cont’d …
28
Cont’d …
• Java is name of
an island of Indonesia, south of Borneo,
from which it
is separated by the Java Sea.
29
Java Version History
There are many java versions that has been released. Current stable
release of Java is Java SE 8.
• JDK Alpha and Beta (1995) • J2SE 1.4 (6th Feb, 2002)
• JDK 1.0 (23rd Jan, 1996) • J2SE 5.0 (30th Sep, 2004)
30
Java Platforms
31
Java Terminology
Java Development Kit
It contains one (or more) JRE's along with the various development
tools like
• Java source compilers,
• Bundling and deployment tools,
• Debuggers, development libraries, etc.
32
Cont’d …
• It interprets the byte code into the machine code depending upon
the underlying OS and hardware combination.
• JVM is platform dependent. (It uses the class libraries, and other
supporting files provided in JRE)
33
Cont’d …
Java Runtime Environment:
• implements Java Virtual Machine, and provides all class libraries and
other facilities necessary to execute Java programs.
• This is the software on your computer that actually runs Java
programs.
• JRE = JVM + Java Packages Classes (like util, math, lang, awt, swing
etc ) + runtime libraries.
34
Java execution procedure
Java
Byte codes Java OS W 32
Win Solaris
MAC Others
Hardware
35
Java Virtual Machine
36
Cont’d …
37
Cont’d …
38
Characteristics of Java
39
Simple Java program
Output:
Hello Ethiopia!
40
Valid java main method signature
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
public static void main(String... args)
static public void main(String[] args)
public static final void main(String[] args)
final public static void main(String[] args)
final strictfp public static void main(String[] args)
41
Question
Can you save a java source file by other name than the
class name?
43
Cont’d …
44
Compiling Java Source Code
Java was designed to run object programs on any platform.
With Java, you write the program once, and compile the source
program into a special type of object code, known as bytecode.
The byte-code can then run on any computer with a Java Virtual
Machine
Java Virtual Machine is a software that interprets Java byte-code.
Java Byte code
JVM
any
Computer
45
Exercise
Write a java program to print:
46
Question
47