Chapter 1
Chapter 1
Chapter 1
Chapter 1
Introduction
1
Objectives
After studying this chapter, students should be able to:
Learn about Programming Paradigms
Structured Vs Object-Oriented paradigm
2
Programming Paradigms
Programming Paradigm is a way of conceptualizing what it
means to perform computation and how tasks to be carried out
and organized on a computer.
Structured Programming
Problem solving would involve the analysis of processes in
terms of the procedural tasks carried out and the production of a
system whose representation is based on the procedural flow of
the processes.
data was separate from code.
programmer is responsible for organizing everything in to
logical units of code/data
A procedural program is divided into functions, and (ideally, at
least) each function has a clearly defined purpose & a clearly
defined interface to the other functions in the program.
3
Contd…
Disadvantages of Structured Programming
1. Unrestricted Access
Functions have unrestricted access to global data.
2. Real-World Modeling
Unrelated functions and data, the basics of the procedural paradigm,
provide a poor model of the real world.
3. Difficult of Creating New Data Types
Traditional languages are not extensible because they will not let you
create new data types.
4
Contd…
OOP Approach
A modern programming paradigm that allows the Programmer
to model a problem in a real-world fashion as an object.
Major objective is to eliminate some of the flaws encountered
in the procedural approach.
OOP allows us to decompose a problem into number of entities
called objects and then build data and methods (functions)
around these entities.
The data of an object can be accessed only by the methods
associated with the object
Follows bottom-up approach in program design
5
Contd…
Features of OOP
Emphasis is on data rather than procedure.
Programs are divided into objects.
Data Structures are designed such that they characterize the
objects.
Methods that operate on the data of an object are tied together
in the data structure.
Data is hidden and can not be accessed by external functions.
Objects may communicate with each other through methods.
6
Basic OOP Concepts
The following are the basic OOP concepts:
1. Objects
2. Classes
3. Data Abstraction
4. Data Encapsulation
5. Inheritance
6. Polymorphism
7. Dynamic Binding
8. Message Passing
7
Contd…
1. Object
An object is an any real world entity which may represent place, person,
data item related to program.
An object has state, behavior and identity.
Ex. A ‘Mouse’ object has,
State: moving or working
Behavior: picking or pointing an object on the screen
Identity: color, company, Identification No. etc.
An object is a variable/instance of class.
An object is a run-time entity.
2. Class
Is the template or blueprint that defines the states and the behaviors
common to all objects of a certain kind.
It is a collection of objects of similar type.
Classes are user-defined data types & behave like the built-in types of
programming language.
8
Contd…
3. Data Abstraction
Abstraction means representing essential features without
9
Contd…
5. Inheritance
Is the process by which objects of one class acquire the
properties of objects of another class.
It provides the idea of reusability( reusing the code)
6. Polymorphism
In polymorphism, ‘Poly’ means many and ‘morph’ means
forms, i.e. many forms.
Is the ability to take more than one form. It allows a function
responding in different ways.
7. Dynamic Binding
Dynamic binding means that the code associated with a given
procedure call is not known until the time of the call at
runtime.
Memory is allocated at runtime not at compile time. 10
Contd…
8. Message Passing
The process of invoking an operation of an object is called
Massage Passing.
In response to the given massage, the respective method or
operation is called.
OOPs includes objects which communicates by sending/
procedure.
11
Introduction to Java
Java is an Object Oriented Programming language developed
by Sun Microsystems in the year 1991.
Initially it was named as “Oak” by James Gosling
In 1995, “Oak” is renamed to “Java” because the name “Oak”
did not survive legal registration.
James Gosling and his team members were consuming a lot of
coffee while developing this language.
Good quality of coffee was supplied from a place called “Java
Island’. Hence they fixed the name of the language as Java. The
symbol for Java language is cup and saucer.
Sun formally announced Java at Sun World conference in 1995.
On January 23rd 1996, JDK1.0 version was released.
12
Features of Java (Java Buzzwords)
Simple: Learning and practicing Java is easy because of
resemblance with C and C++.
Object Oriented: Unlike C++, Java is purely OOP.
Distributed: Java is designed for use on network; it has an
extensive library which works in agreement with TCP/IP.
Secure: Java is designed for use on Internet. Java enables the
construction of virus-free, tamper free systems.
Robust (Strong/ Powerful): Java programs will not crash
because of its exception handling and its memory management
features.
Portable: Java does not have implementation dependent
aspects and it gives same result on any machine.
13
Contd…
Interpreted: Java programs are compiled to generate the byte
code(.class file). This byte code can be interpreted by the
interpreter contained in JVM.
Architectural Neutral Language: Java byte code is not
machine dependent, it can run on any machine with any
processor and with any OS.
High Performance: Along with interpreter there will be JIT
(Just In Time) compiler which enhances the speed of execution.
Multithreaded: Executing different parts of a program
simultaneously is called multithreading. This is an essential
feature to design server side programs.
Dynamic: We can develop programs in Java which
dynamically change on Internet (e.g.: Applets).
14
Java Environment
Java Environment includes a large number of development tools
and hundreds of classes and methods.
The development tools are part of the system known as Java
Development Kit (JDK) and the classes and methods are part of
the Java Standard Library (JSL), also known as Application
Programming Interface (API).
JDK comes with a collection of tools that are used for developing
and running java programs:
appleviewer (for viewing java applets )
javac (java compiler)
java (java interpreter)
javap (java disassembler)
javah (for C header files)
javadoc (for creating HTML documents)
jdb (Java debugger)
15
Java API
It includes hundreds of classes and methods grouped into
several functional packages.
Most commonly used packages are:
Language Support Package: a collection of classes and methods required
for implementing basic features of java.
Utilities Package: a collection of classes to provide utility functions such
as date and time functions.
Input/output Package: a collection of classes required for input/output
manipulation.
Networking Package: a collection of classes for communicating with
other computers via Internet.
AWT Package (Abstract Window Tool kit package): contains classes that
implements platform-independent graphical user interface.
Applet Package: includes set of classes that allows us to create java
applets.
16
Programming Structure
In Java programming language:
A program is made up of one or more classes
A class contains one or more methods
A method contains program statements
In Java, first we need to import the required packages. By
default, java.lang.* is imported. Java has several such packages
in its library.
A package is a kind of directory that contains a group of related
classes and interfaces. A class or interface contains methods.
Since Java is purely an Object Oriented Programming language,
we cannot write a Java program without having at least one
class or object. So, it is mandatory to write a class in Java
program. We should use class keyword for this purpose and
then write class name.
17
Contd…
// comments about the class
class header
public class Class-name
{
18
First Java Program
// MyProgram.java
public class Sample
{
public static void main( String args[])
{
System.out.println(“ Hello World! “);
}
}
Let us discuss the program line by line:
Class Declaration: the first line class Sample declares a class,
19
Contd…
Braces : Every class definition in java begins with an opening
brace “{“ and ends with a closing brace “}”.
The main line: the third line public static void main(String args[])
defines a method named as main, is the starting point for the
interpreter to begin the execution of the program.
public: is an access specifier that declares the main method as
“unprotected” and therefore making it accessible to all other
classes.
static: declares that this method as one that belongs to the entire
class and not a part of any objects of the class.
Main must always be declared as static since the interpreter
21
Java Program Structure
Optional
Package Statement
Optional
Import Statements
Optional
Interface Statements
22
Documentation Section
Comprises a set of comment lines giving the name of the
program, the author and other details, which the programmer
would like to refer at a later stage.
Comments are description about the aim and features of the
program.
Comments increase readability of a program.
Java supports three types of comments:
1. Single line comment //
2. Multiple line comment /*………………
………………*/
3. Documentation comment /**….*/
This form of comment is used for generating documentation
automatically.
23
Package Statement
Is the first statement in Java file and is optional.
It declares a package name and informs the compiler that the
classes defined here belong to this package.
Example: package student;
Import statements
Next to package statements (but before any class definitions) a
number of import statements may exist. This is similar to
#include statements in C or C++.
Using import statements we can have access to classes that are
part of other named packages.
Example: import java.lang.Math;
24
Interface Statements
An interface is like a class but includes a group of method
declarations.
is also an optional section.
is used only when we wish to implement the multiple inheritance
features in the program
Class Definitions
A Java program may contain multiple class definitions.
Classes are the primary and essential elements of a Java
program.
These classes are used to map objects of real-world problems.
The number of classes depends on the complexity of the
problem.
25
Main Method
Since every Java stand-alone program requires a main method as
its starting point, this class is the essential part of a Java
program.
A simple Java program may contain only this part.
The main method creates objects of various classes and
establishes communications between them.
On reaching the end of main, the program terminates and control
passes back to the operating system.
26
Creating a Source File
Type the program in a text editor (i.e. Notepad, WordPad,
Microsoft Word or Edit Plus).
We can launch the Notepad editor from the Start menu by selecting
Programs > Accessories > Notepad. In a new document, type the
above code (i.e. Sample Program).
Save the program with filename same as Class_name (i.e.
Sample.java) in which main method is written. To do this in
Notepad, first choose the File > Save menu item. Then, in the
Save dialog box:
Using the Save in combo box, specify the folder (directory)
where you'll save your file.
In this example, the directory is JQR on the D drive.
In the File name text field, type "Sample.java", including the
quotation marks. Then the dialog box should look like this:
27
Contd…
28
Executing the Source File
To Compile the Sample.java program go to DOS prompt. We
can do this from the Start menu by choosing Run... and then
entering cmd. The window should look similar to the following
figure.
31
1. Keywords
Are essential part of a language definition and can not be used as
names for variables, classes, methods and so on.
Java language has reserved 60 words as keywords.
32
2. Identifiers
Are programmer-designed tokens.
Are used for naming classes, methods, variables, objects,
labels, packages and interfaces in a program.
Java identifiers follow the following rules:
They can have alphabets, digits, and the underscore and
dollar sign characters.
They must not begin with a digit
Uppercase and lowercase letters are distinct.
They can be of any length.
Can not be a reserved words like main, class ,string etc. 33
POP QUIZ
34
3. Literals
Literals in Java are a sequence of characters(digits, letters and
other characters) that represent constant values to be stored in
variables.
Five major types of literals in Java:
36
Contd…
III.Brackets [ ] :- are used to declare array types and for
dereferencing array values.
IV. Semicolon ; :- used to separate statements.
V. Comma , :- used to separate consecutive identifiers in a
variable declaration, also used to chain statements together
inside a “for” statement.
VI.Period . :- Used to separate package names from sub-package
names and classes; also used to separate a variable or method
from a reference variable.
37
5. Operators
Are symbols that take one or more arguments (operands) and
operates on them to a produce a result.
Are used to in programs to manipulate data and variables.
They usually form a part of mathematical or logical
expressions.
Expressions can be combinations of variables, primitives and
operators that result in a value.
38
END
39