JavaBook ToC
JavaBook ToC
Preface……………………………………………………………………………………………… iii
Chapter 1 Software Development and Object Oriented Programming Paradigms .............. 1
1.1 Introduction ......................................................................................................................... 1
1.2 Problem Domain and Solution Domain............................................................................... 2
1.2.1 Problem States ............................................................................................................. 3
1.3 Types of Persons Associated to Solution............................................................................. 3
1.4 Program ............................................................................................................................... 4
1.5 Approaches in Problem Solving .......................................................................................... 4
1.5.1 Multiple attacks or Ask Questions............................................................................... 5
1.5.2 Look for things that are similar ................................................................................... 5
1.5.3 Working backward or bottom-up approach ................................................................. 5
1.5.4 Problem decomposition or top-down approach ........................................................... 5
1.6 Styles of Programming ........................................................................................................ 5
1.7 Complexity of Software ...................................................................................................... 8
1.8 Software Crisis .................................................................................................................... 9
1.9 Software Engineering Principles ....................................................................................... 10
1.10 Evolution of a New Paradigm ........................................................................................... 13
1.11 Natural Way of Solving a Problem.................................................................................... 14
1.12 Abstraction ........................................................................................................................ 15
1.13 Interface and Implementation............................................................................................ 16
1.14 Encapsulation .................................................................................................................... 17
1.15 Comparison of Natural and Conventional Programming Methods ................................... 17
1.16 Object-Oriented Programming Paradigms ........................................................................ 18
1.17 Classes and Objects ........................................................................................................... 19
1.18 Features of Object-Oriented Programming ....................................................................... 21
1.18.1 Encapsulation ............................................................................................................ 22
1.18.2 Data Abstraction........................................................................................................ 22
1.18.3 Inheritance ................................................................................................................. 25
1.18.4 Multiple Inheritance .................................................................................................. 25
1.18.5 Polymorphism ........................................................................................................... 25
1.18.6 Delegation ................................................................................................................. 25
1.18.7 Genericity .................................................................................................................. 25
1.18.8 Persistence ................................................................................................................. 26
1.18.9 Concurrency .............................................................................................................. 26
1.18.10 Events .................................................................................................................... 26
1.19 Modularity ......................................................................................................................... 26
1.20 How to Design a Class?..................................................................................................... 27
1.21 Design Strategies in OOP .................................................................................................. 27
1.21.1 Composition .............................................................................................................. 27
1.21.2 Generalization ........................................................................................................... 28
v
1.22 Comparison of Structured and Object-Oriented Programming ......................................... 29
1.23 Object-Oriented Programming Languages ........................................................................ 29
1.24 Requirements of Using OOP Approach ............................................................................ 31
1.25 Advantages of Object-Oriented Programming .................................................................. 31
1.26 Limitations of Object-Oriented Programming................................................................... 32
1.27 Applications of Object-Oriented Programming................................................................. 32
1.28 Summary ........................................................................................................................... 32
1.29 Excersices.......................................................................................................................... 33
Chapter 2 Java Platform and Program Structure................................................................... 35
2.1 Introduction ....................................................................................................................... 35
2.2 Historical Perspective of Java............................................................................................ 36
2.3 Java.................................................................................................................................... 37
2.4 Java Runtime Environment ............................................................................................... 40
2.5 Architecture of JVM.......................................................................................................... 42
2.6 Characteristics of Java ....................................................................................................... 44
2.7 Java Program Structure...................................................................................................... 44
2.8 Commands for Running a Java Program ........................................................................... 46
2.9 Simple I/O Operations in Java........................................................................................... 48
2.9.1 Reading Input Data from the Keyboard..................................................................... 49
2.9.2 Writing Output to the Screen..................................................................................... 49
2.10 Code Conventions ............................................................................................................. 51
2.10.1 Packages .................................................................................................................... 52
2.10.2 Classes ....................................................................................................................... 52
2.10.3 Interfaces ................................................................................................................... 52
2.10.4 Methods ..................................................................................................................... 52
2.10.5 Variables.................................................................................................................... 52
2.10.6 Constants ................................................................................................................... 52
2.11 Java Enterprise Edition (Java EE) 5.0 ............................................................................... 52
2.12 Java 2 Micro Edition (J2ME) ............................................................................................ 55
2.13 Summary ........................................................................................................................... 57
2.14 Exercises............................................................................................................................ 57
Chapter 3 Lexical Elements of Java ......................................................................................... 59
3.1 Introduction ....................................................................................................................... 59
3.2 Grammar............................................................................................................................ 59
3.3 Character Set Used in Java Programs................................................................................ 60
3.4 Character Encoding ........................................................................................................... 60
3.5 Escape Sequences.............................................................................................................. 61
3.6 Identifiers........................................................................................................................... 63
3.7 Keywords .......................................................................................................................... 64
3.8 Concept of Data................................................................................................................. 64
3.9 Data Types......................................................................................................................... 64
3.10 Declaration of Scalar Variables......................................................................................... 66
3.11 Lexical Elements ............................................................................................................... 67
3.12 Comments.......................................................................................................................... 68
3.12.1 Regular comments ..................................................................................................... 68
3.12.2 Single-line comments ................................................................................................ 68
3.12.3 Documentation comments ......................................................................................... 68
vi
3.13 White Spaces ..................................................................................................................... 69
3.14 Tokens ............................................................................................................................... 69
3.15 Literals............................................................................................................................... 70
3.15.1 Boolean Literals......................................................................................................... 70
3.15.2 Arithmetic Literals..................................................................................................... 71
3.15.3 Integer Literals........................................................................................................... 71
3.15.4 Octal and Hexadecimal Literals ................................................................................ 71
3.15.5 Character Literals ...................................................................................................... 72
3.15.6 Floating Point Literals ............................................................................................... 72
3.15.7 String Literals ............................................................................................................ 73
3.16 Separators or Punctuators .................................................................................................. 74
3.17 Operators ........................................................................................................................... 74
3.18 Summary ........................................................................................................................... 75
3.19 Exercises............................................................................................................................ 75
Chapter 4 Operators and Expressions........................................................................................ 77
4.1 Introduction ....................................................................................................................... 77
4.2 Categories of Operators..................................................................................................... 78
4.3 Expressions........................................................................................................................ 79
4.4 Binding and Binding Time ................................................................................................ 79
4.5 Side Effect ......................................................................................................................... 80
4.6 Features of Operators ........................................................................................................ 80
4.7 Evaluation of Expressions ................................................................................................. 81
4.8 Type Conversion ............................................................................................................... 82
4.9 Numeric Promotion ........................................................................................................... 83
4.10 Arithmetic Expressions ..................................................................................................... 84
4.11 Relational and Equality Operators..................................................................................... 85
4.12 Logical Operators .............................................................................................................. 86
4.12.1 Bitwise Logical Operators ......................................................................................... 86
4.13 Shift Operators .................................................................................................................. 91
4.14 One’s Complement Operator............................................................................................. 93
4.15 Logical Operators .............................................................................................................. 94
4.16 Assignment Operators ....................................................................................................... 95
4.17 Explicit Type Conversion.................................................................................................. 97
4.18 String Concatenation ......................................................................................................... 97
4.19 Operator Precedence and Associativity ............................................................................. 97
4.20 Summary ........................................................................................................................... 99
4.21 Exercises............................................................................................................................ 99
Chapter 5 Control Flow Statements ....................................................................................... 101
5.1 Introduction ..................................................................................................................... 101
5.2 Classification of Statements ............................................................................................ 102
5.2.1 Expression Statement .............................................................................................. 102
5.2.2 Control Flow Statements ......................................................................................... 103
5.3 if-else Control Constructs............................................................................................... 104
5.3.1 Nested if-else ........................................................................................................... 106
5.3.2 if-else-if Control Construct...................................................................................... 106
5.4 switch-case Control Construct......................................................................................... 108
5.5 enum Types and Conditional Statements ........................................................................ 110
vii
5.6 while Loop Construct ...................................................................................................... 111
5.7 do-while Loop Construct ................................................................................................. 114
5.8 for Loop Construct .......................................................................................................... 114
5.9 Unconditional Execution ................................................................................................. 123
5.9.1 break Statement ....................................................................................................... 123
5.9.2 Labeled break statement .......................................................................................... 124
5.9.3 continue Statement .................................................................................................. 124
5.9.4 The return Statement ............................................................................................... 125
5.10 Block Statements ............................................................................................................. 126
5.11 Declaration Statement ..................................................................................................... 126
5.12 Empty Statement ............................................................................................................. 127
5.13 Summary ......................................................................................................................... 128
5.14 Exercises.......................................................................................................................... 129
Chapter 6 Arrays...................................................................................................................... 133
6.1 Introduction ..................................................................................................................... 133
6.2 Arrays .............................................................................................................................. 134
6.3 Classification of Arrays ................................................................................................... 134
6.4 Creation of Arrays ........................................................................................................... 135
6.5 Creation of Regular Arrays.............................................................................................. 135
6.5.1 Creation of One-Dimensional Regular Arrays ........................................................ 136
6.5.2 Creation of Two Dimensional Regular Arrays ........................................................ 137
6.5.3 Creation of Three-Dimensional Regular Arrays...................................................... 139
6.6 Reading and Writing of Arrays ....................................................................................... 141
6.7 Initialization of Arrays .................................................................................................... 142
6.7.1 Initialization of One-Dimensional Regular Arrays.................................................. 143
6.7.2 Initialization of Two-Dimensional Regular Arrays ................................................. 145
6.7.3 Initialization of Three-Dimensional Regular Arrays ............................................... 153
6.8 Features of Arrays ........................................................................................................... 154
6.9 Passing Array as a Parameter .......................................................................................... 156
6.10 Applications of Arrays .................................................................................................... 157
6.11 Recursive Methods .......................................................................................................... 168
6.12 Summary ......................................................................................................................... 170
6.13 Exercises.......................................................................................................................... 170
Chapter 7 Classes and Objects................................................................................................ 173
7.1 Introduction ..................................................................................................................... 173
7.2 Class ................................................................................................................................ 174
7.2.1 Class Declaration..................................................................................................... 175
7.2.2 Field Declarations.................................................................................................... 176
7.2.3 Defining Methods.................................................................................................... 176
7.3 Objects............................................................................................................................. 178
7.3.1 Creation of Object References................................................................................. 178
7.3.2 Creation of Objects Using new Operator................................................................. 178
7.3.3 Accessing Object Members ..................................................................................... 179
7.3.4 Sample Programs..................................................................................................... 179
7.4 Constructors..................................................................................................................... 182
7.4.1 Default Constructors................................................................................................ 184
7.5 Access Modifiers ............................................................................................................. 185
viii
7.6 Getter and Setter Methods ............................................................................................... 189
7.7 Classification of Methods................................................................................................ 190
7.8 Instance Methods............................................................................................................. 191
7.9 Parameter Passing............................................................................................................ 191
7.10 Invoking Methods............................................................................................................ 192
7.10.1 Method call for a method returning void ................................................................. 193
7.10.2 Method Call for a Method Returning a Value ......................................................... 193
7.10.3 Actual Arguments.................................................................................................... 196
7.11 Methods Overloading ...................................................................................................... 196
7.12 The this Reference........................................................................................................... 199
7.12.1 Using this as an object reference ............................................................................. 201
7.13 Static Fields and Methods ............................................................................................... 202
7.13.1 Static Fields ............................................................................................................. 203
7.13.2 Static Methods ......................................................................................................... 204
7.14 Accessing a Static Member ............................................................................................. 205
7.15 Features of Static Members ............................................................................................. 205
7.16 Java Program Structure.................................................................................................... 206
7.16.1 Entry Point............................................................................................................... 210
7.16.2 Dummy Class .......................................................................................................... 211
7.17 Nested Classes ................................................................................................................. 211
7.18 Summary ......................................................................................................................... 212
7.19 Exercises.......................................................................................................................... 212
Chapter 8 Inheritance .......................................................................................................... 215
8.1 Introduction ..................................................................................................................... 215
8.2 Derived Class Declaration ............................................................................................... 217
8.3 Types of Inheritance ........................................................................................................ 219
8.4 How to Implement Inheritance ........................................................................................ 221
8.5 Inheritance and Member Accessibility ............................................................................ 222
8.6 Constructors in Derived Classes...................................................................................... 224
8.7 Overriding and Hiding Fields and Methods .................................................................... 225
8.8 Using the keyword super ................................................................................................. 228
8.9 Abstract Classes and Methods......................................................................................... 231
8.10 The final Classes and final Methods................................................................................ 234
8.11 Java Class Hierarchy ....................................................................................................... 236
8.12 Dynamic Binding ............................................................................................................ 237
8.13 Polymorphism ................................................................................................................. 239
8.14 When to Use Inheritance? ............................................................................................... 241
8.15 Advantages of Inheritance ............................................................................................... 241
8.16 Multi-Level Inheritance Program .................................................................................... 241
8.17 Hierarchical Inheritance Program.................................................................................... 244
8.18 Summary ......................................................................................................................... 246
8.19 Exercises.......................................................................................................................... 246
Chapter 9 Interfaces and Packages......................................................................................... 249
9.1 Interfaces ......................................................................................................................... 249
9.1.1 Declaration and Implementations of Interfaces ....................................................... 251
9.1.2 Polymorphism in Interfaces..................................................................................... 254
9.1.3 Multilevel Inheritance ............................................................................................. 256
ix
9.1.4 Multiple Inheritance ................................................................................................ 257
9.1.5 Explicit Interface Member Implementations ........................................................... 259
9.1.6 Validating Interfaces ............................................................................................... 261
9.1.7 Problems in Interfaces Because of Inheritance........................................................ 263
9.2 Packages: Putting classes Together ................................................................................. 265
9.2.1 Java Foundation Packages ....................................................................................... 265
9.2.2 Package Naming Conventions................................................................................. 266
9.2.3 Creating Packages ................................................................................................... 267
9.2.4 Accessing Classes from Packages ........................................................................... 268
9.2.5 Accessing a Package................................................................................................ 268
9.2.6 Using a Package: An Example ................................................................................ 269
9.2.7 Adding a Class to an Existing Package ................................................................... 270
9.2.8 Packages and Name Clashing.................................................................................. 271
9.2.9 Extending a Class from Package ............................................................................. 272
9.2.10 Creating Java Archives............................................................................................ 272
9.2.11 Set Java Classpath ................................................................................................... 272
9.2.12 Read Environment Variables................................................................................... 273
9.3 Summary ......................................................................................................................... 273
9.4 Exercises.......................................................................................................................... 274
Chapter 10 Exception Handling.............................................................................................. 277
10.1 Introduction ..................................................................................................................... 277
10.2 Exception Handling ......................................................................................................... 279
10.3 Exception Programming .................................................................................................. 280
10.3.1 The throw Statement................................................................................................ 281
10.3.2 The try Statement .................................................................................................... 281
10.4 User Defined Exception .................................................................................................. 287
10.5 Debugging Java Programs .............................................................................................. 293
10.6 Summary ......................................................................................................................... 294
10.7 Exercises.......................................................................................................................... 294
Chapter 11 Strings and Collections ........................................................................................ 297
11.1 Introduction ..................................................................................................................... 297
11.2 String Class...................................................................................................................... 298
11.3 String Manipulation......................................................................................................... 300
11.4 StringBuffer..................................................................................................................... 304
11.5 Command-Line Arguments ............................................................................................. 309
11.6 Java.util ........................................................................................................................... 309
11.7 StringTokenizer ............................................................................................................... 311
11.8 Collection Framework ..................................................................................................... 313
11.9 Components of Collection Framework............................................................................ 314
11.10 Accessing the Collection Class.................................................................................... 314
11.11 Legacy Collection Types ............................................................................................. 315
11.11.1 Vector .................................................................................................................. 316
11.11.2 Hash Table........................................................................................................... 318
11.11.3 Enumeration ........................................................................................................ 319
11.12 Wrapper Classes .......................................................................................................... 320
11.12.1 Methods in Wrapper Class .................................................................................. 321
11.13 Generic Data Types and Collections ........................................................................... 321
x
11.14 Frequently Used Collections ....................................................................................... 329
11.14.1 List....................................................................................................................... 329
11.14.2 Set........................................................................................................................ 330
11.14.3 Map...................................................................................................................... 332
11.15 Summary ..................................................................................................................... 335
11.16 Exercises...................................................................................................................... 335
Chapter 12 Streams and I/O Programming .......................................................................... 337
12.1 Introduction to Streams ................................................................................................... 337
12.2 Java Stream API .............................................................................................................. 338
12.2.1 Reading and Writing Bytes...................................................................................... 338
12.2.2 Reading and Writing Characters.............................................................................. 339
12.2.3 Layered Java Streams .............................................................................................. 341
12.2.4 Handling Exceptions ............................................................................................... 342
12.3 File Management ............................................................................................................. 343
12.4 File Processing ................................................................................................................ 346
12.4.1 Binary Streams ........................................................................................................ 346
12.4.2 Write Text Output.................................................................................................... 348
12.4.3 Read Text Input ....................................................................................................... 351
12.5 Primitive Data Processing ............................................................................................... 353
12.6 Object Processing ............................................................................................................ 355
12.6.1 Java Serialization..................................................................................................... 355
12.6.2 Write and Read Objects........................................................................................... 356
12.6.3 Versioning ............................................................................................................... 358
12.7 Retrieve Data from Console ............................................................................................ 359
12.8 Summary ......................................................................................................................... 364
12.9 Exercises.......................................................................................................................... 364
Chapter 13 Socket Programming............................................................................................ 367
13.1 Introduction ..................................................................................................................... 367
13.1.1 Client/Server Communication ................................................................................. 367
13.1.2 Hosts Identification and Service Ports..................................................................... 369
13.1.3 Sockets and Socket-based Communication ............................................................. 369
13.2 Socket Programming and java.net Class ......................................................................... 370
13.3 TCP/IP Socket Programming .......................................................................................... 371
13.4 UDP Socket Programming .............................................................................................. 374
13.5 Math Server ..................................................................................................................... 377
13.6 URL Encoding................................................................................................................. 381
13.6.1 Writing and Reading Data via URLConnection ...................................................... 382
13.7 Summary ......................................................................................................................... 384
13.8 Exercises.......................................................................................................................... 385
Chapter 14 Multithreaded Programming .............................................................................. 387
14.1 Introduction ..................................................................................................................... 387
14.2 Defining Threads ............................................................................................................. 388
14.3 Threads in Java................................................................................................................ 389
14.3.1 Extending the Thread Class ..................................................................................... 390
14.3.2 Implementing the Runnable Interface ..................................................................... 391
14.3.3 Thread class versus Runnable interface................................................................... 392
xi
14.4 Thread Life Cycle............................................................................................................ 393
14.5 A Java Program with Multiple Threads........................................................................... 393
14.6 Thread Priority ................................................................................................................ 396
14.7 Thread Methods............................................................................................................... 398
14.8 Multithreaded Math Server.............................................................................................. 400
14.9 Concurrent Issues with Thread Programming ................................................................. 402
14.9.1 Read/Write problem ................................................................................................ 402
14.9.2 Producer and Consumer Problem ............................................................................ 405
14.10 Summary ..................................................................................................................... 409
14.11 Exercises...................................................................................................................... 409
Chapter 15 Graphical Programming...................................................................................... 411
15.1 Introducing Swing ........................................................................................................... 411
15.2 Graphics Programming.................................................................................................... 414
15.2.1 Displaying String..................................................................................................... 414
15.2.2 Working with Shapes .............................................................................................. 416
15.3 Handling Events .............................................................................................................. 419
15.3.1 Overview of Delegation Event Model..................................................................... 419
15.3.2 Examples: Capturing Simple Action ....................................................................... 420
15.3.3 Yet Another Example : Window Events ................................................................. 423
15.3.4 Work with Keyboard ............................................................................................... 423
15.3.5 Work with Mouse .................................................................................................... 425
15.4 Swing Components.......................................................................................................... 430
15.4.1 Introduction to Layout Management ....................................................................... 430
15.4.2 Top-level Containers ............................................................................................... 435
15.4.3 JComponent Base Class .......................................................................................... 436
15.4.4 Text Components..................................................................................................... 437
15.4.5 Choice Components................................................................................................. 443
15.4.6 Menu........................................................................................................................ 452
15.5 Summary ......................................................................................................................... 462
15.6 Exercises.......................................................................................................................... 463
Chapter 16 Advanced GUI Programming and Applets ........................................................ 465
16.1 Advanced Swing Components......................................................................................... 465
16.1.1 Dialogs..................................................................................................................... 465
16.1.2 Advanced Containers............................................................................................... 470
16.2 Model-View-Controller ................................................................................................... 480
16.3 Java Applet ...................................................................................................................... 481
16.3.1 The Lifecycle of Applets ......................................................................................... 483
16.3.2 Passing Parameters to Applets................................................................................. 484
16.3.3 Interactive Applet .................................................................................................... 486
16.3.4 AudioClip Interface ................................................................................................. 491
16.3.5 AppletContext .......................................................................................................... 494
16.3.6 AppletStub............................................................................................................. 496
16.4 Building Non-Blocking GUI ........................................................................................... 499
16.4.1 Event Dispatcher Thread ..................................................................................... 500
16.4.2 Accessing Swing Components in Other Threads............................................. 500
16.4.3 Real Time Clock Example.................................................................................... 500
xii
16.5 Summary ......................................................................................................................... 503
16.6 Exercises.......................................................................................................................... 503
Chapter 17 RMI Programming.............................................................................................. 505
17.1 When to use RMI ............................................................................................................ 505
17.2 RMI Development Lifecycle ........................................................................................... 507
17.3 Implementing an RMI Server .......................................................................................... 509
17.4 Implementing an RMI Client........................................................................................... 515
17.5 How to Run an RMI-based Application .......................................................................... 516
17.6 Security Issues ................................................................................................................. 518
17.7 Summary ......................................................................................................................... 521
17.8 Exercises.......................................................................................................................... 522
Chapter 18 JDBC Programming ........................................................................................... 525
18.1 What is JDBC: A Brief Introduction ............................................................................... 525
18.2 Types of JDBC Drivers ................................................................................................... 527
18.3 Using HSQL Database .................................................................................................... 528
18.4 Configuration for JDBC Connection ............................................................................... 531
18.5 JDBC Update Operations ................................................................................................ 534
18.6 JDBC Query Operation ................................................................................................... 538
18.7 A Robust and Efficient Approach: Using Prepared Statement........................................ 542
18.8 Stored Procedure ............................................................................................................. 543
18.9 JDBC Transaction Support.............................................................................................. 546
18.10 Summary ..................................................................................................................... 548
18.11 Exercises...................................................................................................................... 548
Chapter 19 Java Servlet Programming ................................................................................. 551
19.1 Server-side Programming ................................................................................................ 551
19.1.1 The Old Way: CGI Programming............................................................................ 552
19.1.2 The Java Way: Model-View-Controller .................................................................. 552
19.2 Apache Tomcat Servlet Container................................................................................... 553
19.3 The Controller: Java Servlet ............................................................................................ 554
19.3.1 What is Servlet ........................................................................................................ 554
19.3.2 Servlet Lifecycle...................................................................................................... 555
19.3.3 Serlvets in Action .................................................................................................... 556
19.3.4 Deployment ............................................................................................................. 570
19.3.5 Cookies and Session ................................................................................................ 572
19.3.6 Filtering Request ..................................................................................................... 577
19.4 Summary ......................................................................................................................... 580
19.5 Exercises.......................................................................................................................... 580
Chapter 20 JavaServer Pages and Java Beans ..................................................................... 583
20.1 What is JavaServer Pages (JSP) ...................................................................................... 583
20.2 The Skeleton of JSP......................................................................................................... 586
20.2.1 Directives................................................................................................................. 586
20.2.2 Java Expressions...................................................................................................... 588
20.2.3 Implicit Objects ....................................................................................................... 589
20.3 Getting Started with JSP: A Blog Example ..................................................................... 589
20.3.1 Blog Controller........................................................................................................ 591
20.3.2 Viewing the Blog..................................................................................................... 596
xiii
20.3.3 Modifying a Blog Entry........................................................................................... 599
20.3.4 Posting Comments................................................................................................... 601
20.3.5 Processing Requests ................................................................................................ 602
20.4 Simplifying JSP with JavaBeans ..................................................................................... 608
20.4.1 How to Write JavaBeans ......................................................................................... 608
20.4.2 JSP Standard JavaBeans Tags ................................................................................. 616
20.5 JSP Expression Language (EL) ....................................................................................... 618
20.5.1 Reserved Words....................................................................................................... 618
20.5.2 Operators ................................................................................................................. 619
20.5.3 Literals..................................................................................................................... 619
20.5.4 Implicit Objects ....................................................................................................... 619
20.6 Introduction to JSP Standard Tag Library (JSTL)........................................................... 619
20.6.1 Getting Started with JSTL ....................................................................................... 620
20.6.2 Configuring JSTL.................................................................................................... 622
20.7 Summary ......................................................................................................................... 622
20.8 Exercises.......................................................................................................................... 622
Appendix A Project A - Publishing House Automation ................................................. 625
Appendix B Project B - Bank Automation System .............................................................. 635
Appendix C Eclipse IDE.................................................................................................... 647
Appendix D Answers to Objective Questions ....................................................................... 655
Appendix E Glossary ......................................................................................................... 669
Appendix F ASCII Table ....................................................................................................... 681
Appendix G Recommended References ........................................................................... 683
Index …………………………………………………………………………………………….. 685
xiv
Chapter 1
This chapter presents various methodologies for problem solving and development of applications
that have evolved over a period of time. This is primarily driven by the increasing complexity of
software and the cost of software maintenance growing rapidly. The chapter introduces object-
oriented design and programming as a silver bullet to solve software crisis. It then discusses various
features of objected oriented programming (OOP) from encapsulation and inheritance to templates.
Finally, the chapter presents various OOP programming languages with their unique properties.
Objectives
After learning the contents of this chapter, the reader must be able to:
• understand programming paradigms
• know the factors influencing the complexity of software development
• define software crisis
• know the important models used in software engineering
• explain the natural way of solving a problem
• understand the concepts of object-oriented programming
• define abstraction and encapsulation
• differentiate between interface and implementation
• understand classes and objects
• state the design strategies embedded in OOP
• compare structured programming with OOP
• list examples of OOP languages
• list the advantages and applications of OOP
1.1 Introduction
Computers are used for solving problems quickly and accurately irrespective of the magnitude of the
input. To solve a problem, a sequence of instructions is communicated to the computer. To
communicate these instructions, programming languages are developed. The instructions written in
a programming language comprise a program. A group of programs developed for certain specific
1
purposes are referred to as software whereas the electronic components of a computer are referred to
as hardware. Software activates the hardware of a computer to carry out the desired task. In a
computer, hardware without software is similar to a body without soul. Software can be system
software or application software. System software is a collection of system programs. A system
program is a program, which is designed to operate, control and utilize the processing capabilities of
the computer itself effectively. System programming is the activity of designing and implementing
system programs. Almost all the operating systems come with a set of ready to use system
programs: user management, file system management, and memory management. By composing
programs it is possible to develop new, more complex, system programs. Application software is a
collection of prewritten programs meant for specific applications.
Computer hardware can understand instructions only in the form of machine codes i.e. 0's and
1's. A programming language used to communicate with the hardware of a computer is known as
low-level language or machine language. It is very difficult for humans to understand machine
language programs because the instructions contain a sequence of 0’s and 1’s only. Also, it is
difficult to identify errors in machine language programs. Moreover, low-level languages are
machine dependent. To overcome the difficulties of machine languages, high-level languages such
as Basic, Fortran, Pascal, COBOL and C were developed.
High-level languages allow some English-like words and mathematical expressions that
facilitate better understanding of the logic involved in a program. While solving problems using
high-level languages, importance was given to develop an algorithm (step by step instructions to
solve a problem). While solving complex problems, a lot of difficulties were faced in the
algorithmic approach. Hence object-oriented programming languages such as C++ and Java were
evolved with a different approach to solve the problems. Object-oriented languages are also high-
level languages with concepts of classes and objects that are discussed later in this chapter.
2
Problem is transformed to Solution
F:P S
The states are to be clearly understood before trying to get a solution for the problem. The initial
conditions and assumptions are to be explicitly stated to derive a solution for a problem. The
solution to a problem must be viewed in terms of people associated with it.
3
Domain
Expert
Provides Logical
Solution
Produces uses
Analyst/ User
Designer/ Solution
Developer
1.4 Program
The solution to a problem is written in the form of a program, while a computer is used to solve the
problem. A program is a set of instructions written in a programming language. A programming
language provides the medium for conveying the instructions to the computer. There are many
programming languages such as BASIC, FORTRAN, Pascal, C, C++, etc., similar to the written
languages like English, Tamil and Hindi. Once the steps to be followed for solving a problem are
identified, it is easier to convert these steps to a program through a programming language. The idea
of providing solution is quite challenging. The domain experts play a major role in formulating the
solution. The formulation of solution is important before writing a program. It requires logical
thinking, careful planning and systematic approach. This can be achieved through the proper
combination of domain experts, system analysts/system designers and developers. The program
takes the input from the user and generates the desired output as shown in Figure 1.4.
Converted to
F:I O
4
1.5.1 Multiple attacks or Ask Questions
By asking questions like what, why and how, the solution may be outlined for some problems.
Questions can be asked to many people irrespective of the domain and the answers to multiple
attacks of questions may help in revealing the solution. Whenever the solution is not known, this
approach may be used.
1.5.2 Look for things that are similar
We should never reinvent the wheel again. The existing solution for a similar problem can be used
to solve a problem. For example, finding the maximum value in a set of numbers is the same as
finding the maximum mark in a class of students or finding the highest temperature in a day. All
these different problems require the same concept of finding the biggest value among the values.
The solution is based on the similar nature of a problem.
1.5.3 Working backward or bottom-up approach
The problem can also be solved by starting from the Goal state and reaching the Start state. For
example, sometimes we prefer to derive an equation in mathematics from right side to left side. The
solution is derived in the reverse direction. For complex problems, this approach will be an easier
approach. Consider the problem of reaching an unknown place from a known place. It is always
easier to trace a known place starting from an unknown place compared to tracing from known to
unknown place. There may be many known landmarks nearer to the known place helping in locating
the place. If any one such landmark is reached, it is equivalent to finding the solution. But, the
landmarks of the unknown place are new while searching. Hence, even by reaching to the nearest
place, sometimes the location may not be identified and the tracing becomes difficult.
1.5.4 Problem decomposition or top-down approach
The problem is decomposed into small units and they are further decomposed into smaller units over
and over again until each smaller unit is manageable. The complex problem is simplified by
decomposing it into many simple problems. It is applicable for simple and fairly complex problems.
The top-down approach is also known as stepwise refinement or modular decomposition or
structured approach or algorithmic approach.
5
Sequence of statements
Global Data
PROGRAM
Global Data
FORTRAN II, ALGOL 60 and COBOL are second generation languages. The second generation
languages provided the possibility of information hiding (i.e., hiding the implementation details of a
subprogram). However, sharing the same data by many subprograms breaks the data hiding
principle. Hence, data hiding is only partially succeeded. Here also the program is closer to solution
domain where concentration is on operations/operators using functions.
Third generation programming languages (1962-1970) such as PASCAL and C use sequential
code, global data, local data and subprograms as shown in Figure 1.7. They follow structured
programming, which supports modular programming. The program is divided into a number of
modules. Each module consists of a number of subprograms represented by rectangles.
Importance was given for developing an algorithm and hence this approach is also known as
algorithmic oriented programming. In structural programming approach, data and subprograms
exist separately (Algorithms + Data Structures = Programs). A main program calls the subprograms.
Structured programming approach supports the following features:
1. Each procedure has its own local data and algorithm.
2. Each procedure is independent of other procedures.
3. Parameter passing mechanisms are evolved.
4. It is possible to create user defined data types.
5. A rich set of control structures is introduced.
6
6. Scope and visibility of data are introduced.
7. Nesting of subprograms is supported.
8. Procedural abstractions or function abstractions are achieved yielding abstract operations.
9. Subprograms are the basic physical building blocks supporting modular programming.
Global Data
Nested
subprogram
develops
Programmer Program ( closer to computer )
It can be observed that in structured programming, the emphasis is on the subprograms and the
efficient way of developing algorithms in terms of computing time and computer memory to solve
the problem. The relationship between programmer and program is given prime importance as
shown in Figure 1.8. Hence structured programming paradigms depend on solution domain and not
on problem domain. The data is not given importance regarding access permission.
To solve a complex problem using top-down approach, first the complex problem is
decomposed into smaller problems. Further these smaller problems are decomposed and finally a
collection of small problems are left out. Each problem is solved one at a time. Structured
programming starts with high-level descriptions of the problem representing global functionality. It
successively refines the global functionality by decomposing it into subprograms using lower level
1
A scope identifies the portion of source program from which a variable can be accessed. It
normally consists in the portion of text that starts from the variable declaration and spans till the end
of the nearest enclosing block.
7
descriptions, always maintaining correctness at each level. At each step, either a control or a data
structure is refined. Thus top-down approach is followed in structured programming. This is a fairly
successful approach because it will cause problems only when there is a revision of design phase.
Such revisions may result in massive changes in the program. Also the possibility of reuse of
software modules is minimized.
There was a generation gap from 1970 to 1980. Many programming languages evolved, but only
a few of them were used in software development. Despite the invention of new programming
languages and software engineering concepts, software industries were unable to meet the demand
in reality.
8
The behavior of a continuous system can be predicted by using the existing laws and theorems.
For example, the landing of a satellite can be predicted exactly using some theory even though it is a
complex system. But, computers have systems with discrete states during execution of the software.
The behavior of the software may not be predicted exactly because of its discrete nature. Even
though the software is divided into smaller parts, the phase transition cannot be modeled to predict
the output. Sometimes an external event may corrupt the whole system. Such events make the
software extremely complex.
7. Software testing
The number of variables, control structures and functions used in the software are enormous.
The discrete nature of the software execution modifies a variable and it may be unnoticed. This may
result in unpredictable output. Hence, vigorous testing is essential. It is impossible to test each and
every aspect of the software in a complex software system. So only important aspects are subjected
to testing and the user must be satisfied with this. The reliability of the software depends on rigorous
testing. But testing processes make software development more and more complex.
Improper
understanding of
problem
Change of
rules during
Software development
Testing
Software
Behaviour Development Preservation
of discrete of existing
system system
Flexibility
due to lack of
standards Management of
development process
9
development – not conceived as a process – led to late completion, exceeding budget in the case of
large and complex project. The OO paradigm arose as a consequence of a software crisis, where the
relative cost of software has increased substantially at a rate where software maintenance and
software development cost has far outstripped that of hardware costs. This rate of increase is
depicted in Figure 1.10. Software crisis as a term arose from the understanding that costs in software
development and maintenance have increased significantly, and that software engineering concepts
and innovations have not resulted in significant improvements in the productivity of software
development and maintenance. The software crisis provided an impetus to develop principles and
tools in software to drive, maintain and provide solid paradigms to apply to the software
development life cycle, with the intent to create more reliable and reusable systems. The sharp
increase in software maintenance from 1995-2000 is attributed to Y2K (Year 2000) problem in
software applications. As a result Indian software engineers have gained world-wide popularity,
which has in turn led to rapid growth of IT industries in India.
100
Proportionate of cost
80
Hardware Software
Development
60
40
Software
Maintenance
20
10
activities in this model. Each activity is followed after completion of the previous activity. Because
of the rigid sequential nature there is a lack of iterations of activities. The analyst may use dataflow
diagrams (DFDs), the designer may focus on hierarchy charts, and the programmer may use
flowcharts and hence there are disjoint mappings among the SDLC activities. Generally, the analyst
uses top-down functional decomposition while solving a problem. The programmer implements the
solution easily by using the procedural languages/structural programming languages that support
functional decomposition. The difficulty of reuse of software components still persists.
Requirement
Specification
Analysis
Design
Implementation
Testing
Maintenance
Percentage of costs incurred during the different phases of SDLC is shown in Figure 1.12. Cost
factor of the first two phases can be combined. It can be observed that the maintenance of software
is 60% whereas all the other costs are only 40%. Hence, maintenance is an important factor to be
considered in software development process. Also, earlier programming languages did not support
reusability. An existing program cannot be reused because of the dependence of the program on its
environment. Thus, the following two major problems demanded a new programming approach:
1. Software maintenance.
2. Software reuse.
Logical improvement to the Waterfall model resulted in the Fountain model. The same six
activities in the software development are still followed in the same sequence. However, there is an
overlap of activities and iteration of activities as shown in Figure 1.13. The Fountain model is a
graphical representation to remind us that although some life cycle activities cannot start before
others, there is a considerable overlap and merging of activities across the full life cycle. In a
fountain, water rises up the middle and falls back, either to the pool below or is re-entrained at an
intermediate level.
11
Cost in Percentage
60
50
40
30
20
10
0
Analysis Design Development Maintenance
Maintenance Evolution
Software
in Use
Testing
coding
Design
Analysis
12
The Fountain model outlines the general characteristics of the systems level perception of an
object-oriented development. There is a high degree of merging in the analysis, design,
implementation and unit testing phases. Moving through a number of steps, falling back one or more
steps and performing repeatedly, is a far more flexible approach than the one proposed by Waterfall
model. It follows a bottom up approach, which starts from the solution. If there is an existing
solution, that solution is studied first and the necessary details are identified and organized in a
suitable manner. For a problem not having a solution, the domain experts (i.e., experts who are
capable of providing useful information and future requirements) are consulted with the
conventional solution to start with. Since the software is developed by analyzing the solution first,
this approach is known as bottom up approach. There is another approach similar to Fountain model
called as a Spiral model as shown in Fig. 1.14. Spiral model also follows iterative approach in each
phase.
The Spiral model involves a little bit of analysis, followed by a little bit of design, a little bit of
implementation and a little bit of testing. A loop of the spiral goes through some or all of the
Waterfall phases. The idea is that each loop produces an output and by repeatedly following all the
activities such as planning, analysis, implementation and review the final solution is reached.
Engineering phase shown in quadrant III of Figure 1.14 involves coding, testing and putting the
solution into use.
Quadrant I Quadrant II
Toward a
Correct and complete solution
Quadrant IV
Quadrant III
Reviewing
Engineering
Both the Fountain model and Spiral model provided better solution for complex problems
compared to top-down approach followed in the Waterfall model. The procedural and structured
programming languages were found unsuitable for the bottom-up approach because a change in
requirement, analysis, or design phase can cause the programming to start from the beginning once
again. They lack flexibility, modifiability and software component reuse.
13
5. quicken the completion time of software development
The Object-oriented model was evolved for solving complex problems. It resulted in object-
oriented programming paradigms. Object-oriented software development started in the 1980s.
Object-oriented programming (OOP) seems to be effective in solving the complex problems faced
by software industries. The end-users as well as the software professionals are benefited by OOP.
OOP provides a consistent means of communication among analysts, designers, programmers and
end users.
Object-oriented programming paradigm suggests new ways of thinking for finding a solution to
a problem. Hence the programmers should keep their mind tuned in such a manner that they are not
to be blocked by their preconceptions experienced in other programming languages such as
structured programming. Proficiency in object-oriented programming requires talent, creativity,
intelligence, logical thinking and the ability to build and use abstractions and experience.
If procedures or functions are considered as verbs and data items are considered as nouns, a
procedure oriented program is organized around verbs while an object-oriented program is
organized around nouns.
How does the manager solve this problem? The way by which the problem is addressed is shown in
Figure 1.15.
MANAGER
Pa
sse
e
ag
s
s
me
es
m
ssa
es
ge
ss
Pa
Stenographer Driver
The manager first calls the stenographer to prepare the letter and dictates the matter. The
stenographer takes shorthand notes of the dictation and prepares the letter using a computer and a
printer. Now the letter is ready for signing and the manager signs it. Then the manager calls the
driver to take him to the customer’s site. The driver along with the manager reaches the destination
with the help of a car.
The manager delegates the responsibility of typing and taking the printed output to the
stenographer. The driver is entrusted with the responsibility of taking him to the customer’s site.
14