Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
Object-Oriented Programming
Using Java
Objectives
Discuss the following topics:
Rudimentary Java
Object-Oriented Programming (OOP) in Java
Input and Output
Java and Pointers
Vectors in java.util
Data Structures and Object-Oriented
Programming
Case Study: Random Access File
Data Structures and Algorithms in Java
Rudimentary Java
A Java program is a sequence of statements
that have to be formed in accordance with the
predefined syntax
A statement is the smallest executable unit in
Java
Each statement ends with a semicolon
Compound statements, or blocks, are marked
by delimiting them with braces, { and }
Variable Declarations
Each variable must be declared before it can be
used in a program
It is declared by specifying its type and its name
Variable names are strings of any length of
letters, digits, underscores, and dollar signs that
begin with a letter, underscore, or dollar sign
A letter is any Unicode letter
Java is case sensitive
Data Structures and Algorithms in Java
Size
Range
boolean
1 bit
true, false
char
16 bits
Unicode characters
byte
8 bits
[-128, 127]
short
16 bits
[-32768, 32767]
int
32 bits
[-2147483648, 2147483647]
long
64 bits
[-9223372036854775808, 9223372036854775807]
float
32 bits
[-3.4E38, 3.4E38]
double
64 bits
[-1.7E308, 1.7E308]
Operators
Value assignments are executed with the
assignment operator =
Use one at a time or string together with other
assignment operators
x = y = z = 1;
Decision Statements
One decision statement is an if-else
statement
if (condition)
do something;
[else do something else;]
Loops
The first loop available in Java is the while
loop:
while (condition)
do something;
Exception Handling
Catching an error is possible by using the
try-catch statement
try {
do something;
} catch (exception-type exception-name) {
do something;
}
10
Object-Oriented Programming
(OOP) in Java
A class is a template in accordance to which
objects are created
Functions defined in a class are called methods
Variables used in a class are called class scope
variables, data fields, or fields
The combination of data and related operations
is called data encapsulation
An object is an instance of a class, an entity
created using a class definition
Data Structures and Algorithms in Java
11
Encapsulation
Objects make the connection between data and
methods much tighter and more meaningful
The first OOL was Simula; it was developed in
the 1960s in Norway
The information-hiding principle refers to
objects that conceal certain details of their
operations from other objects so that these
operations may not be adversely affected by
other objects
Data Structures and Algorithms in Java
12
13
Generic Classes
class IntClass {
int[] storage = new int[50];
..................
}
class DoubleClass {
double[] storage = new double[50];
..................
}
class GenClass {
Object[] storage = new Object[50];
Object find(int n) {
return storage[n];
}
..................
}
Data Structures and Algorithms in Java
14
Arrays
Arrays are Java objects
There is no keyword with which all arrays are
declared
Without keywords, subclasses cannot be
created
Arrays are declared with empty brackets after
the name of the type or the name of the array
itself
15
Arrays (continued)
These two declarations are equivalent:
int[] a;
and
int a[];
16
17
18
Inheritance
OOLs allow for creating a hierarchy of classes
so that objects do not have to be instantiations
of a single class
Subclasses or derived classes inherit the
fields and methods from their base class so that
they do not have to repeat the same definitions
A derived class can override the definition of a
non-final method by introducing its own
definition
Data Structures and Algorithms in Java
19
Polymorphism
Polymorphism is the ability of acquiring many
forms
Dynamic binding is when the type of method to
be executed can be delayed until run time
Static binding is when the type of response is
determined at compilation time
Dynamic binding is when the system checks
dynamically the type of object to which a
variable is currently referring and chooses the
method appropriate for this type
Data Structures and Algorithms in Java
20
Polymorphism (continued)
class A {
public void process() {
System.out.println("Inside A");
}
}
class ExtA extends A {
public void process() {
System.out.println("Inside ExtA");
}
}
21
Polymorphism (continued)
then the code
A object = new A();
object.process();
object = new ExtA();
object.process();
results in the output
Inside A
Inside ExtA
Data Structures and Algorithms in Java
22
23
24
25
26
readBoolean()
readByte()
readShort()
readChar()
readInt()
readLong()
readUTF() (to read strings in Unicode Text Format)
27
28
29
30
31
32
33
34
35
Vectors in java.util
A vector is a data structure with a contiguous
block of memory, just like an array
Class Vector is a flexible array whose size can
be dynamically changed
The class hierarchy in the package java.util
is:
Object AbstractCollection AbstractList Vector
36
37
38
39
40
41
Summary
A Java program is a sequence of statements
that have to be formed in accordance with the
predefined syntax.
A statement is the smallest executable unit in
Java.
Compound statements, or blocks, are marked by
delimiting them with braces, { and }.
A class is a template in accordance to which
objects are created.
Data Structures and Algorithms in Java
42
Summary (continued)
Functions defined in a class are called methods.
Variables used in a class are called class scope
variables, data fields, or fields.
The combination of data and related operations
is called data encapsulation.
An object is an instance of a class, an entity
created using a class definition.
An item specified in terms of operations is called
an abstract data type.
Data Structures and Algorithms in Java
43
Summary (continued)
Subclasses, or derived classes, inherit the fields
and methods from their base class so that they
do not have to repeat the same definitions.
Polymorphism is the ability of acquiring many
forms.
In many languages, pointer is a technical term
for a type of variable; in Java, the term reference
is used instead.
A vector is a data structure with a contiguous
block of memory, just like an array.
Data Structures and Algorithms in Java
44