Menu

Saturday, 2 June 2012

JAVA Program to demonstrate use of Dynamic Method Dispatch


class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {
// override callme()
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
// override callme()
void callme() {
System.out.println("Inside C's callme method");
}
}
class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
THE JAVA LANGUAGE
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}

JAVA Program to implement an Abstract class


abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class AbstractAreas {
public static void main(String args[]) {
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area i is " + figref.area());
}
}

JAVA Program to implement multi level inheritance .


class A {
String getName(){
return "Gagan";
}
}
class B extends A {
int getAge() {
return 20;
}}
class C extends B {
String getAddress(){
return "A/1,India";
}}
class D extends C {
void print() {
System.out.println(getName());
System.out.println(getAge());
System.out.println(getAddress());
}}
class main{
public static void main(String args[]){
D obj1=new D();
obj1.print();
}}

Friday, 1 June 2012

JDBC (Java Database Connectivity)


What is JDBC Driver ?

JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server.
For example, using JDBC drivers enable you to open database connections and to interact with it by sending SQL or database commands then receiving results with Java. The Java.sql package that ships with JDK contains various classes with their behaviours defined and their actual implementaions are done in third-party drivers. Third party vendors implements the java.sql.Driver interface in their database driver.

Understanding the JDBC Architecture


JDBC is an API specification developed bySun Microsystems that defines a uniform interface for accessing various relational databases. JDBC is a core part of the Java platform and is included in the standard JDK distribution.
The primary function of the JDBC API is to provide a means for the developer to issue SQL statements and process the results in a consistent, database-independent manner. JDBC provides rich, object-oriented access to databases by defining classes and interfaces that represent objects such as:
1. Database connections
2. SQL statements
3. Result Set
4. Database metadata
5. Prepared statements
6. Binary Large Objects (BLOBs)
7. Character Large Objects (CLOBs)
8. Callable statements
9. Database drivers
10. Driver manager
The JDBC API uses a Driver Manager and database-specific drivers to provide transparent connectivity to heterogeneous databases. The JDBC driver manager ensures that the correct driver is used to access each data source. The Driver Manager is capable of supporting multiple concurrent drivers connected to multiple heterogeneous databases.

Layers of the JDBC Architecture:

A JDBC driver translates standard JDBC calls into a network or database protocol or into a database library API call that facilitates communication with the database. This translation layer provides JDBC applications with database independence. If the back-end database changes, only the JDBC driver need be replaced with few code modifications required.


JDBC Drivers Types:


JDBC driver implementations vary because of the wide variety of operating systems and hardware platforms in which Java operates. Sun has divided the implementation types into four categories, Types 1, 2, 3, and 4, which is explained below:

Type 1: JDBC-ODBC Bridge driver (Bridge)
Type 2: Native-API/partly Java driver (Native)
Type 3: AllJava/Net-protocol driver (Middleware)
Type 4: All Java/Native-protocol driver (Pure)


Type 1 JDBC Driver (JDBC-ODBC Bridge driver):


The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.


Type 1: JDBC-ODBC Bridge
Advantage:

The JDBC-ODBC Bridge allows access to almost any database, since the database's ODBC drivers are already available.
Disadvantages:
1. Since the Bridge driver is not written fully in Java, Type 1 drivers are not portable.
2. A performance issue is seen as a JDBC call goes through the bridge to the ODBC driver, then to the database, and this applies even in the reverse process. They are the slowest of all driver types.
3. The client system requires the ODBC Installation to use the driver.
4. Not good for the Web.

Type 2 JDBC Driver (Native-API/partly Java driver):


The distinctive characteristic of type 2 jdbc drivers are that Type 2 drivers convert JDBC calls into database-specific calls i.e. this driver is specific to a particular database. Some distinctive characteristic of type 2 jdbc drivers are shown below. Example: Oracle will have oracle native api.


Type 2: Native api/ Partly Java Driver
Advantage:

The distinctive characteristic of type 2 jdbc drivers are that they are typically offer better performance than the JDBC-ODBC Bridge as the layers of communication (tiers) are less than that of Type 1 and also it uses Native api which is Database specific.
Disadvantage:
1. Native API must be installed in the Client System and hence type 2 drivers cannot be used for the Internet.
2. Like Type 1 drivers, it’s not written in Java Language which forms a portability issue.
3. If we change the Database we have to change the native api as it is specific to a database
4. Mostly obsolete now
5. Usually not thread safe.

Type 3 JDBC Driver (All Java/Net-protocol driver):


Type 3 database requests are passed through the network to the middle-tier server. The middle-tier then translates the request to the database. If the middle-tier server can in turn use Type1, Type 2 or Type 4 drivers.

Type 3: All Java/ Net-Protocol Driver
Advantage:

1. This driver is server-based, so there is no need for any vendor database library to be present on client machines.
2. This driver is fully written in Java and hence Portable. It is suitable for the web.
3. There are many opportunities to optimize portability, performance, and scalability.
4. The net protocol can be designed to make the client JDBC driver very small and fast to load.
5. The type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advanced system administration such as logging and auditing.
6. This driver is very flexible allows access to multiple databases using one driver.
7. They are the most efficient amongst all driver types.
Disadvantage:
It requires another server application to install and maintain. Traversing the recordset may take longer, since the data comes through the backend server.

Type 4 JDBC Driver (Native-protocol/all-Java driver):


The Type 4 uses java networking libraries to communicate directly with the database server.


Type 4: Native-protocol/all-Java driver
Advantage:

1. The major benefit of using a type 4 jdbc drivers are that they are completely written in Java to achieve platform independence and eliminate deployment administration issues. It is most suitable for the web.
2. Number of translation layers is very less i.e. type 4 JDBC drivers don't have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good.
3. You don’t need to install special software on the client or server. Further, these drivers can be downloaded dynamically.
Disadvantage:
With type 4 drivers, the user needs a different driver for each database.

Monday, 7 May 2012

JAVA program to design Fibonacci series


class fibo
{
public static void main(String args[])
{
int prev, next, sum, n,a=0;
System.out.println(+a);
prev=next=1;
for(n=1;n<=10;n++)
{
System.out.println(prev);
sum=prev+next;
prev=next;
next=sum;
}
}
}

Saturday, 21 April 2012

What Is an Operating System?

Definition :

The operating system is the most important program that runs on a computer. Every general-purpose computer must have an operating system to run other programs. Operating systems perform basic tasks, such as recognizing input from the keyboard, sending output to the display screen, keeping track of files and directories on the disk, and controlling peripheral devices such as disk drives and printers. For large systems, the operating system has even greater responsibilities and powers. It is like a traffic cop -- it makes sure that different programs and users running at the same time do not interfere with each other. The operating system is also responsible for security, ensuring that unauthorized users do not access the system.



Operating systems can be classified as follows:


multi-user : Allows two or more users to run programs at the same time. Some operating systems permit hundreds or even thousands of concurrent users.
multiprocessing : Supports running a program on more than one CPU.
multitasking : Allows more than one program to run concurrently.
multithreading : Allows different parts of a single program to run concurrently.
real time: Responds to input instantly. General-purpose operating systems, such as DOS and UNIX, are not real-time.
Operating systems provide a software platform on top of which other programs, called application programs, can run. The application programs must be written to run on top of a particular operating system. Your choice of operating system, therefore, determines to a great extent the applications you can run. For PCs, the most popular operating systems are DOS, OS/2, and Windows, but others are available, such as Linux.
As a user, you normally interact with the operating system through a set of commands. For example, the DOS operating system contains commands such as COPY and RENAME for copying files and changing the names of files, respectively. The commands are accepted and executed by a part of the operating system called the command processor or command line interpreter. Graphical user interfaces allow you to enter commands by pointing and clicking at objects that appear on the screen.

Friday, 13 April 2012

What is inode?

When a file system is created, data structures that contain

information about files are created. Each file has an inode
and is identified by an inode number (often "i-number" or
even shorter, "ino") in the file system where it resides.
Inodes store information on files such as user and group
ownership, access mode (read, write, execute permissions)
and type of file. There is a fixed number of inodes, which
indicates the maximum number of files each filesystem can hold.
 
A file's inode number can be found using the ls -i command,
while the ls -l command will retrieve inode information.
This is description of inode information  which it contain:
 
    * The length of the file in bytes.
    * Device ID (this identifies the device containing the file).
    * The User ID of the file's owner.
    * The Group ID of the file.
    * The file mode, which determines what users can read, write and execute the file.
    * Timestamps telling when the inode itself was last modified (ctime, change time), the file content 
last modified (mtime, modification time), and last accessed (atime, access time).
    * A reference count telling how many hard links point to the inode.
    * Pointers to the disk blocks that store the file's content

What is Unix file system ?

The Unix file system has a hierarchical (or tree-like) structure with its highest level directory called root (denoted by /, pronounced slash). Immediately below the root level directory are several subdirectories, most of which contain system files. Below this can exist system files, application files, and/or user data files. Similar to the concept of the process parent-child relationship, all files on a Unix system are related to one another. That is, files also have a parent-child existence. Thus, all files (except one) share a common parental link, the top-most file (i.e. /) being the exception. Below is a diagram (slice) of a "typical" Unix file system. As you can see, the top-most directory is / (slash), with the directories directly beneath being system directories. Note that as Unix implementaions and vendors vary, so will this file system hierarchy. However, the organization of most file systems is similar.


While this diagram is not all inclusive, the following system files (i.e. directories) are present in most Unix filesystems:

 • bin - short for binaries, this is the directory where many commonly used executable commands reside 

• dev - contains device specific files 

• etc - contains system configuration files

 • home - contains user directories and files

 • lib - contains all library files

 • mnt - contains device files related to mounted devices 

• proc - contains files related to system processes 

• root - the root users' home directory (note this is different than /) 

• sbin - system binary files reside here. If there is no sbin directory on your system, these files most likely reside in etc 

• tmp - storage for temporary files which are periodically removed from the filesystem 

• usr - also contains executable commands