Java Programming D. Kavitha M. Neelamegan
Java Programming
D. Kavitha, M.Sc., M.Phil., Set. (Ph.D) HOD of BCA
M. Neelamegan, ME., MBA., Assistant Professor Department of BCA SVM ATRS AND SCIENCE COLLEGE Katteri, Uthangarai
For online purchase
www.charulathapublications.com New No.24, Thambiah Road, West Mambalam, Chennai - 33.
Mobile : 98404 28577
June 2019
© Charulatha Publications
ISBN No. : 978-93-89051-14-8
Published by
CHARULATHA
PUBLICATIONS
New No.24, Thambiah Road, West Mambalam, Chennai - 600 033. Phone : 24745589, 24746546 Email :
[email protected] [email protected] www.charulathapublications.com
PREFACE Java Programming is an introduction to computer science and applications with programming intended for readers with little or no experience. In this second edition, we added managing Input/ Output operations on files along with the first edition. We start with the most basic concepts and are careful to define all terms when they are first used. The book presents each new idea in a logical progression. Larger topics, like recursion and objectoriented programming, are divided into smaller examples and introduced over the course of several chapters. This book is intentionally concise. It is not meant to be a comprehensive presentation of Java, but rather, an initial exposure to programming constructs and techniques.
ACKNOWLEDGEMENT Many persons have contributed to make this book a reality. We would especially like to express our appreciation to Dr.K.Arul, Principal, Sri Vidya Mandir Arts & Science College, Katteri, Uthangarai for his unstinted support and encouragement. We would also like to thank Thiru.R.P.Rajee, Secretary, Sri Vidya Mandir Arts & Science College, Katteri, Uthangarai for his continuous guidance. Finally, we wish to express our sincere thanks to all our family members for their moral support.
D. KAVITHA., M. Sc., M.Phil., SET., M. NEELAMEGAN, M.E., MBA.,
SYLLABUS PERIYAR UNIVERSITY BACHELOR OF COMPUTER APPLICATION (B.C.A.) SEMESTER V CORE XI - JAVA PROGRAMMING UNIT— I Java Evolution — Simple Java Program — Java program structure — Java Tokens — Java Statements — JVM— Command Line Arguments — Constants, Variables, and Data Types — Operators and Expressions. UNIT- II Decision Making and Branching: Introduction — Decision Making with if Statement—Simple ifStatement — The if... else Statement — Nesting of if...else Statement — The else if Ladder — The SwitchStatement — The ?: Operator. Decision Making and Looping: Introduction — The While Statement — TheDo Statement — The For Statement — Jumps in Loops — Labelled Loops. Classes, Objects and Methods:Introduction — Defining a Class — Fields Declaration — Methods Declaration — Creating Objects —Accessing Class Members — Constructors — Methods Overloading — Static Members —Nesting ofMethods — Inheritance —Overriding Methods — Final Variables and Methods — Final Classes — FinalizerMethods — Abstract Methods and Classes — Methods with Varargs — Visibility Control. UNIT- III Arrays, Strings and Vectors: Introduction —One-dimensional Arrays — Creating an Array — TwodimensionalArrays — Strings — Vectors — Wrapper Classes — Enumerated Types —
Annotations. Interfaces: Introduction — Defining Interfaces —Extending Interfaces — Implementing Interfaces —Accessing Interface Variables — Packages: Introduction —Java API Packages — Using System Packages—Naming Conventions — Creating Packages — Accessing a Package—Using a Package — Adding a Class toa Package — Hiding Classes — Static Import. UNIT- IV Multithreaded Programming: Introduction —Creating Threads — Extending the Thread class — Stoppingand Blocking a Thread - Life cycle of a Thread— Using Thread methods — Thread Exceptions — ThreadPriority — Synchronization — Implementing the Runnable interface — Inter-thread Communication.Managing Errors and Exceptions: Introduction —Types of Errors — Exceptions — Syntax of ExceptionHandling Code — Multiple Catch Statements — Using Finally Statement — Throwing Our OwnExceptions. Applet Programming: Introduction — Difference Between Applets and Applications— WriteApplets - Building Applet code - Applet life cycle - Creating an Executable Applet —Designing a webpage — Applet Tag — Adding Applet to HTML File— Running the applet — Applet Tags—PassingParameters to Applets —Aligning the Display — Displaying Numerical values — Getting input from theuser — Event handling. UNIT— V Graphics Programming: Introduction — The Graphics Class - Lines and Rectangles — Circles andEllipses — Drawing Arcs — Drawing polygons — Line Graphs — Using Control Loops in Applets — DrawingBar Charts. Managing)/0 Files in Java:lntroduction — Concept of stream — Stream classes — Byte streamclasses — Character stream classes — Using stream — Using the file class — Creation of Files-Reading/Writing characters— Reading/Writing Bytes— Handling Primitive Data types — Concatenatingand buffering Bytes - Random access files.
CONTENTS CHAPTER – I : FUNDAMENTALS OF OBJECT ORIENTED PROGRAMMING 1.1
Introduction of Java
1.1
1.2
Benefits of OOPS
1.4
1.3
Application of OOPS
1.4
CHAPTER – II : JAVA EVALUATION 2.1
JAVA History
2.1
2.2
JAVA Features
2.2
2.3
How JAVA Differs from C and C++
2.4
2.4
Hardware and Software Requirements
2.6
2.5
JAVA Support Systems
2.7
2.6
JAVA Development Kit (JDK)
2.7
2.7
Application Programming Interface
2.9
CHAPTER – III : OVERVIEW OF JAVA LANGUAGE 3.1
Standalone Application
3.1
3.2
JAVA Program Structure
3.4
3.3
JAVA Tokens
3.7
3.4
JAVA Statements
3.10
3.5
Implementing the JAVA Program
3.12
3.6
Command Line Arguments
3.14
CHAPTER – IV : CONSTANTS,VARIABLES AND DATA TYPES 4.1
Constants
4.1
4.2
Variables
4.4
4.3
Data Types
4.4
4.4
Type Casting
4.10
4.5
Getting Values of Variables
4.11
4.6
Standard Default Values
4.12
CHAPTER – V : OPERATORS AND EXPRESSIONS 5.1 Operators 5.1 5.2
Operator Precedency and Associativity
5.8
5.3
Mathematical Function
5.9
CHAPTER – VI : DECISION MAKING AND BRANCHING 6.1
Branching
6.1
6.2
Types of Condition Branching (or) Decision Making6.1
CHAPTER – VII : DECISION MAKING AND LOOPING 7.1
Looping
7.1
7.2
Jumps IN Loop
7.8
7.3
Labelled Loop
7.10
CHAPTER – VIII : CLASSES AND OBJECT 8.1
Classes
8.1
8.2
Data Members
8.2
8.3
Methods
8.2
8.4
Accessing Data Member
8.3
8.5
Constructor
8.4
8.6
Method Overloading
8.5
8.7
Method Overriding
8.6
8.8
Nesting of Methods
8.9
8.9
Static Members
8.10
8.10 Inheritance : Extending a Class
8.11
8.11 Final Variables and Methods
8.16
8.12 Visibility Control
8.20
CHAPTER – IX : CLASSES AND OBJECT 9.1
Arrays
9.1
9.2
Strings
9.5
9.3
String Buffer
9.8
9.4
Vector
9.9
CHAPTER – X : INTERFACE : MULTIPLE INHERITANCE 10.1 Interfaces
10.1
10.2 Extending Interfaces
10.2
10.3 Implementing Interfaces
10.3
CHAPTER – XI : PACKAGES 11.1 API Package
11.1
11.2 Utility Package
11.2
11.3 Networking Package
11.3
11.4 System Package
11.2
11.5 User Defined Package
11.3
11.6 Hiding Classes
11.6
CHAPTER – XII : THREADED PROGRAMMING 12.1 Multi Threading
12.1
12.2 Creating a Thread
12.2
12.3 Thread Life Cycle
12.6
12.4 Thread Priority
12.11
CHAPTER – XIII : MANAGING ERRORS AND EXCEPTION 13.1 Error
13.1
13.2 Exception Handling
13.2
13.3 Multiple Catch Statements
13.6
13.4 Using Finally Statement 13.5 Throwing our own Exception
13.8 13.10
CHAPTER – XIV : APPLET PROGRAMMING 14.1 Applet
14.1
14.2 Applet Life Cycle
14.3
14.3 Creating Web Page
14.5
14.4 Applet Tag
14.7
14.5 Passing Parameters to Applet
14.8
14.6 Event Handling
14.11
14.7 Event Sources
14.12
14.8 Event Listeners
14.12
CHAPTER – XV : GRAPHICS PROGRAMMING 15.1 Graphics Classes
15.1
15.2 Introduction to AWT Package
15.3
15.3 Introduction to Swing
15.4
CHAPTER – XVI : MANAGING INPUT / OUTPUT FILE 16.1 File
16.1
16.2 File Processing
16.1
16.3 Stream
16.2
16.4 Managing Input / Output File
16.3
Byte Stream Class
16.6
Input Stream Classes
16.6
Output Stream Classes
16.8
Reader Stream Classes
16.9
Using Streams
16.11
Other Useful I/O Classes
16.12
Using the File Class
16.13
Input / Output Exceptions
16.14
Creation of Files
16.15
Reading / Writing Characters
16.17
Reading and Writing Bytes
16.19
Handling Primitive Data Types
16.22
Concatening and Buffering Files
16.25
APPENDIX : Previous Year Question Papers
CHAPTER - I FUNDAMENTALS OF OBJECT ORIENTED PROGRAMMING 1.1 Introduction of Java Basic Concepts of OOPs Object Classes Data abstraction3 Encapsulation Inheritance Polymorphism Dynamic binding Message passing Objects Objects are the basic runtime entity in an object oriented system. Syntax: Classname objectname = new objectname(); Example: Student s1 = new Student(); Classes A class is a collection of objects in similar type. Class can contain a data members and methods. Syntax Class classname { Data members;
1.2
Java Programming
Methods; } Example: Class Student { ——————— ——————— } Data Abstraction It refers to the act representing essential features without including the background details or explanation. Encapsulation The wrapping up of data members and methods into a single unit class is known as Encapsulation.
Inheritance The mechanism of deriving a new class from old class is known as Inheritance. Old class is also known as Super class or Base class. New class is also known as Sub class or Derived class. It supports the concept of hierarchical classification. Eg:
Fundamentals of Object Oriented Programming
1.3
Polymorphism Poly means many. Morph means Forms. It takes more than one forms.
There are 2 types. They are, Run time Polymorphism Compile time Polymorphism Dynamic Binding Binding refers to the linking of a procedure call to the code executed in response to the call. It is also known as Late Binding. Message Passing An object oriented program consists of a set of object that communication with each other.
It involves the following basic steps 1. Creating classes that define objects and their behavior
Java Programming
1.4
2. Creating object from class definition 3. Establishing communication among objects. 1.2 Benefits of OOPs The principal advantages are: 1. Through inheritance, we can eliminate redundant code extend the use of existing classes. 2. We can build programs from the standard working modules that communicate with one another that lead to saving of development time and higher productivity. 3. The principle of data hiding helps the programmer to build secure program. 4. It is possible to have multiple instances of an object to co-exist without any interference. 5. It is possible to map object in the problem domain to those in the program. 6. It is easy to partition the work in a project based on objects. 7. The data-centered design approach enables us to capture more detail of a model can implemental form. 8. Object-oriented system can be easily upgraded from small to large system. 9. Message passing techniques for communication between objects makes to interface descriptions with external systems much simpler. 10. Software complexity can be easily managed. 1.3 Application of OOPs Real time system Simulation and modeling Object oriented database AI and Expert system CIM/CAM/CAD system Neural Networks and parallel programming
THE FOLLOWING BOOKS FOR ARTS & SCIENCE B.Sc., & BCA. Computer Graphics Computer Organisation & Architecture Computer Application in Business Computer Networks Digital Electronics & Microprocesor Data Mining and Data Warehousing Database Management System Digital Computer Fundamentals & MP Data Communication Network Design Analysis of Algorithm Datastructures and Algorithm Fundamentals of Digital Computer HTML and Java Script Information Technology Internet & its Application Linux and Shell Programming Multimedia Microprocessor & its Application Object Oriented Analysis & Design Operating Systems Programming in C Programming in C++ Programming in C++ & Datastructures Programming in Java Programming in C (Bsc-Maths Allied) Programming in C & DS Python Programming Problem Solving Technique RDBMS & ORACLE RDBMS Resource Management Technique Software Testing Software Engineering System Administration & Maintainance Visual Programming Web Technology Soft Skill Services Marketing SBAMM
B.Com., & BBA. Auditing Principles & Practices Advertisement & Sales Promotion Business Economics Business Communication Business Law Business Environment Business Regulatory Framework Banking & Insurance Principle Banking Theory Law & Practice Business Mathematics Business Statistics Business Organisation Corporate Accounting (I & II) Cost Accounting (I & II) Company Law and Secretarial Practice Consumer Behaviour E- Commerce Entrepreurial Development Environmental Studies Financial Accounting (I & II) Financial Management Financial Management (MBA) Financial Services Human Resource Management International Trade/Business Indirect Taxation Insurance Indian Economy Lifeskill Education Management Accounting Marketing Management Management Concepts Operations Research Organisational Behaviour Principles of Management Production Management Personality Development Research Methodology Retail Management ISBN-13: 978-93-89051-14-8
9 789389 051148
Rs.180/-