bioprogramming in java

11 downloads 1892 Views 3MB Size Report
The applet code is embedded into HTML file using the tag. Attributes of ..... current value of the scroll bar one unit in the direction of the arrow.
BIOPROGRAMMING IN

JAVA

Contents About the Book Acknowledgement 1. Introduction

1

1.1 Concept

2

1.2 Why Java is more Important

3

1.3 How

3

1.4 Java Overview

4

1.5 Program Structure

5

1.6 Atomic Elements of Java

6

1.7 Type conversion and casting

9

1.8 Scope of a Variable

12

Problem & Solution

13

2. Inheritance Problem & Solution

25 27

3. Abstract Class

32

4. String Handling

34

4.1 String Constructors

35

4.2 String Handling Methods

36

Problem & Solution

37

5. Package

50

5.1 Concept

51

5.2 Creation & Accessing of Package

52

5.3 Access Specifiers

52

Problem & Solution

53

6. Interfaces Solved Programs

61 63

7. Utility Package

69

7.1 Concept

70

7.2 Java Garbage Collection

72

Problem & Solution

75

8. Exception Handling

91

8.1 Concept

92

8.2 User Defined Exception

96

Problem & Solution (Built-in Exception)

97

Problem & Solution (User-Defined Exception)

103

9. Streams

111

9.1 Concept

112

9.2 Byte Stream Classes

113

9.3 Character Stream Classes

113

9.4 Predefined Streams

113

9.5 Byte Array Input Stream

114

9.6 Byte Array Output Stream

114

9.7 File Input Stream

114

9.8 File Output Stream

115

9.9 Data Output Stream

115

9.10 Serialization & Deserialization

116

Problem & Solution

118

10. Java Database Connectivity

136

10.1 Concept

137

102 Steps to Connect JDBC

138

Problem & Solution

141

11. Multithreading

151

11.1 Concept

152

11.2 Life cycle of a Thread

152

11.3 Creation of Thread

153

11.4 Thread priorities

155

11.5 Synchronization

156

11.6 Inter Thread Communication

156

Problem & Solution

157

12. Applet

172

12.1 Concept

181

12.2 Steps to build an Applet

173

12.3 Applet Life Cycle

177

12.4 Graphics class Methods

179

13. Abstract Window Toolkit

180

13.1 Concept

181

13.2 Component Hierarchy

181

13.3 Common methods for Components

181

13.4 AWT Components

182

13.4.1 Label

183

13.4.2 Button

183

13.4.3 Text Component

183

13.4.4 Checkbox

185

13.4.5 Checkbox Group

186

13.4.6 Choice

187

13.4.7 List

188

13.4.8 Scrollbar

189

14. Event Delegation Model

190

14.1 Concept

191

14.2 Low Level Events

191

14.2.1 Component Event

192

14.2.2 Focus Event

192

14.2.3 Key Event

193

14.2.4 Mouse Event

193

14.2.5 Window Event

194

14.3 Semantic / High Level Event

194

14.3.1 Action Event

195

14.3.2 Adjustment Event

195

14.3.3 Item Event

195

14.3.4 Text Event

195

14.4 Event Adapters

195

14.5 Frame

196

14.6 Panel

196

14.7 Layout

197

14.7.1 Flow Layout

197

14.7.2 Grid Layout

197

14.7.3 Border Layout

198

14.7.4 Gridbag Layout

198

14.7.5 Card Layout

199

Problem & Solution

200

15. Bio-Java

285

Reference

305

About the Book The main objective of this book is to provide guidelines for the students those who are interested in solving biological related problems using Java. It helps to improve their understanding and programming knowledge.

This book starts with a brief introduction to Java, its features and advantages. The book also includes the information about Class and Array, Inheritance, String Handling, Packages, Interfaces, Exception Handling, JDBC, Multithreading, Event Handling and AWT. Each section has been thoroughly analyzed and more appropriate examples are given. This book invokes the curiosity among students to solve the problem in Java. The book also provides sample programs for Genome Analysis, Sequence Alignment, Proteome Analysis, Handling Sequence and Structure files, Threading, Database Connectivity, Creation of Interface, Exception Handling, and so on.

Acknowledgement My sincere thanks to Dr.Basant K. Tiwary, Centre Head, Centre for Bioinformatics, Pondicherry University for his continuous support and encouragement.

I wish to thank Prof. P.P Mathur, Vice Chancellor, KIIT University, Bhubaneswar, who inspired me to write this book.

I am very grateful to Mr. M. Sundara Mohan, Information Officer, Centre for Bioinformatics, Pondicherry University for his valuable suggestions in composing this work and I extend my thanks to all my colleagues for their support.

My heartfelt thanks to my students (KushbooBafna, Gayathri Suresh, Ramya Chandar Charles M., Sentamilarasu.S, Surekha Anandhi.M., Bharathiraj.R, Ishwar Patidar) and all other students who motivated me to write this book.

I am very much grateful to my husband, son and my family members for their continuous support in writing this book.

G.Jeyakodi

1. INTRODUCTION

1

1.1 Concept Java:  

Java is a general purpose object oriented programming language Since it is OOP’s many of the conceptswere derived from C and C++.

Birth of C:   

Invented by Dennis Ritchi and standardized in 1989 Prior to C language are designed for an academic or technical purpose but C is different it is developed for real time application. Most of the software,operating system and games are developed by C language.

C++:        

Invented by Bjane straustrup in 1979. Since the people want to represent their data in real time entity C++ introduced the OOP’s concept. OOP’s helps to organize the problems through the OOP’s feature like abstraction, polymorphism etc. Limitation of C language is its complexity. If the program code exits between 25000 to1 lakh it becomes complex. But C++ allow to break and helps the programmer to manage the large problems. Initially the C++ is called as C with classes. In 1983 it is renamed as C++. Peak period of C++ is 1980’s to 1990’s. After few years the world wide web and internet become popular so the user need a language for posting their program in internet.

Creation of Java:     

Java was created by James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike Sheridan at sun Microsystems in 1990. Initially they took 18 months to release the first version of java. Initially it is called as Oak and renamed as Java in 1995. In 1995 many people constributed for the development of java,among them Bill Joy is a remarkable person. Basically the primary motivation of java is not for the internet , first is for the architectural netural and the second is for internet.

2

1.2 Why Java is more important? For 3 concepts: i) ii) iii)

Applet programming Security Portability

Security: Java provides a default firewall between the network application and computer network. With the help of firewall any file can be download without the fear of virus. Portability: 

   

Since many computers are connected to the internet for the dynamic execution of programs there is a strong requirement of protable and efficient code.This can be efficiently manged by Java. This can be done with the help of Java Byte Code. When the java program is compiled it as binary code. But it generates byte code instead of binary code. The byte code instruction can be accessed by different kinds of machine. Java byte code instruction is executed by Java Virtual Machine (JVM) which is an interpreter.

1.3 How Java is different from other languages? Most of the language will be either complier or interpreter but java has both complier and interpreter Java Source code

↓compilation (Java Compiler) Byte code

↓interpretation (Java Virtual Machine) Execution Features of java: i) ii) iii) iv) v) vi) vii)

It is an simple language It is more secure Portability Object oriented code Multi-threading Architecture neutral Distributed 3

viii) ix)

Dynamic Robustness -Program errors may be of memory management mistakes and runtime errors  In C and C++ dynamic memory allocation and deallocation is tedious. But in Java memory allocation and deallocation is done automatically with the help of garbage collection (which is the collection of unused objects).  Run time errors can be handled very efficiently with the help of Exception Handling.

1.4 Java –Overview Since Java is an Oops language it supports all the Oops property Main purpose of Oops is to manage complexity.Oops organizes a program around its data and a set of interfaces to that data. Abstraction / Data Hiding: Concept of hiding non-essential features and provides only the essential feature. Ex. TV remote button. Encapsulation: Wrapping of data and methods in a single unit. Ex. Medical capsules. Inheritance: It is a property of creating sub class form the existing classes. Polymorphism: It is a concept of single form behaving differently in different situation.Ex, person (Home act as a father, office act as a employee and public act as a good citizen). There are two types of java program: i) ii)

Stand alone Applet program

Stand alone

Applet

Hard java & it is like c++.

Web based application of java

Compiled and executed in a command window

Executed in the browser

Since java is a OOPs all the code must be embedded in a class .

4

1.5 Program-structure I)

II) III)

Comment line statement //-for single line /*….*/-for multiple lines Header file inclusion class Class_name { public static void main (String args[]) { System.out.println(“Welcome to Java programming”); } }

Explanation: class- is a Keyword Classname- Identifier public- Global declaration void-tells the compiler that main() method does not return any value static- specifierindicates the java interpreter the main method is a starting point of execution. Since the main method is declared as static it will be invoke before the creation of any other class objects. Java can have n number of classes but only one class can have main method. The main method have only one parameter String args[] String– is a class args[] is a parameter name which is a array of instances of a class String System-Predefined class Out-Output Stream that connected to the console print or println- methods to display the message on the standard output device All java statements terminated by a semicolor. lang- is a package which have class library for System class. Java.lang.Object is the root class for all java program. Since all the java program will havethe output statement, the lang package is imported automatically.

5

Simple java program: //My first java program class javademo { public static void main (String args[]) { System.out.println (“I am happy to learn java”); } } Note: Java program have the extension .java, can compile with javac filename.java .After the compilation java compiler generates a class file which have the byte code instruction. It will be created under the name of the class which have the main method. Execution of the file will be done with the commandjava classname (class name which contain the main method). 1.6 Atomic Elements of Java 1) 2) 3) 4) 5) 6)

White space Identifiers Comments Special symbols Literals Keywords& Variables

White spaces: Blank space, tab key space and new line. Identifier: Names given to classes, arrays, interfaces, variables and soon Variables: Identifier that denote the location to store value.

6

Literals: Literals are nothing but they are constants Classified as  Numeric Constants o Integer  Integer  Octal  Hexadecimal o Real  Float  Character Constant o Single Character o String Constant Comments:  //-for single line  /*…*/- for multiple lines Data types: I. Primitive (Built in Data types) a) Numeric 1) Integer Size(bytes) I) Byte 1 II) short 2 III) int 4 IV) long 8 2) Float I) Float 4 bytes II) Double 8 bytes b) Non-Numeric 1) char - 2 bytes 2) boolean – one bit of storage II. Non-Primitive ( Reference Data Types) a) Classes b) Arrays c) Interfaces

7

Range -128 to 127 -32,768 to 32767 232/2 264/2 3.4e-38 to 3.4e38 1.7e-308 to 1.7 e308

Array: Collection of homogenous data. Array can be single, two and multi-dimensional. Array Declaration: Data type arrayname[]; Arrayname=new datatype[size]; Array Initialization: Compile time- giving values during declaration Run Time- after the declaration assigning values using operators Example: //spliting the array into two small arrays by odd and even numbers class splitarray { public static void main(String args[]) { int x[]={1,10,455,867,54,98,35,64,9,7,5,4,2,6,8}; int oc,ec; oc=ec=0; int l=x.length; int[] oindex= new int[10]; int[] eindex=new int[10]; for(int i=0;i