Data Structures and Algorithms in Java
Data Structures and Algorithms in 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 2
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 4
Size
1 bit 16 bits 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits
Range
true, false Unicode characters [-128, 127] [-32768, 32767] [-2147483648, 2147483647] [-9223372036854775808, 9223372036854775807] [-3.4E38, 3.4E38] [-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; For a prefix operator, a variable is incremented (or decremented) first and then an operation is performed in which the increment takes place For a postfix operator, autoincrement (or autodecrement) is the last operation performed
Data Structures and Algorithms in Java 7
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
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[];
A declaration of a basic data type also creates an item of the specified type Wrapper classes provide object versions of basic data types
16
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
To print anything on the screen, use the statements: System.out.print(message); System.out.println(message); To read one line at a time, the method readLine() from BufferedReader is used
Data Structures and Algorithms in Java 23
25
28
29
The constructor opens a file with the specified name either for reading, or for reading and writing:
RandomAccessFile = raf new RandomAccessFile("myFile", "rw");
Data Structures and Algorithms in Java 30
31
32
33
Figure 1-1 Object reference variables p and q (a) logic of reference of q to an object (b) implementation of this reference
34
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
If the vectors capacity is greater than its size, then a new element can be inserted at the end of the vector immediately
Data Structures and Algorithms in Java 37
More Research
Tutorial: http://java.sun.com/docs/books/tutorial/java/index.html . Hierachy for packages and classes: http://java.sun.com/j2se/1.4.2/docs/api/overviewtree.html
40
42
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 43
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 44
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 45