Object Oriented Programming With Java 1st Edition by Hanumanth Ladwa ISBN 8450193403 9788450193403 - Get The Ebook Instantly With Just One Click
Object Oriented Programming With Java 1st Edition by Hanumanth Ladwa ISBN 8450193403 9788450193403 - Get The Ebook Instantly With Just One Click
com
https://ebookball.com/product/object-oriented-programming-
with-java-1st-edition-by-hanumanth-ladwa-
isbn-8450193403-9788450193403-25034/
OR CLICK BUTTON
DOWLOAD EBOOK
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-wu-
isbn-9780073523309-0073523305-15552/
ebookball.com
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-wu-
isbn-0073523305-9780073523309-16398/
ebookball.com
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-
wu-0073523305-9780073523309-17244/
ebookball.com
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-
wu-0073523305-9780073523309-15256/
ebookball.com
An Introduction to Object Oriented Programming With Java
5th Edition by Thomas Wu 0073523305 9780073523309
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-
wu-0073523305-9780073523309-15330/
ebookball.com
https://ebookball.com/product/object-oriented-programming-and-
java-1st-edition-by-danny-poo-derek-
kiong-9813083964-9789813083967-15306/
ebookball.com
https://ebookball.com/product/object-oriented-programming-and-
java-1st-edition-by-danny-poo-derek-
kiong-9813083964-9789813083967-15278/
ebookball.com
https://ebookball.com/product/beginning-object-oriented-programming-
with-c-1st-edition-by-jack-purdum-1118336925-978-1118336922-15290/
ebookball.com
https://ebookball.com/product/java-java-java-object-oriented-problem-
solving-3rd-edition-by-ralph-morelli-ralph-walde-
isbn-0131474340-9780131474345-12410/
ebookball.com
Object Oriented Programming
with
JAVA
Hanumanth Ladwa
Dedicated to my Parents and
Sisters
Contents
Chapter
Chapter Title Page no.
no.
3 Overview of java 15 – 27
4 Data types 28 – 35
5 Operators 36 – 48
6 Selection and iterative statements 49 – 68
7 Class 69 – 106
107 –
8 Arrays strings and vectors
130
131 –
9 Packages
138
139 –
10 Programming threads
158
159 –
11 Java exceptions
177
178 –
12 Applet programming
188
189 –
13 Graphics programming
199
200 –
14 Event handling
250
251 –
15 Swing
259
260 –
16 File handling
267
LAB ASSIGNMENTS 268 - 325
CHAPTER 1
FUNDAMENTALS OF OBJECT ORIENTED
PROGRAMMING
Introduction: As we move ahead with the new programming
languages, it indicates that we are improving the previous version of
the same programming language or creating a new programming
language to tackle the overheads of existing programming language
and making it more flexible and user friendly. The programmer must
be able to write the programs in such a way that the program should
take optimum memory space and increase its execution speed.
The drawbacks of procedural programming and structured
programming language are eliminated as it has certain loopholes in
the programming techniques and it makes a program too complex.
Hence, in order to eliminate the limitations of the structured and
procedural programming techniques programmers have developed a
new programming approach called Object Oriented
Programming (OOP) that emphasizes data rather than procedure.
Each problem is treated as an object rather than procedure. This
new programming technique uses the bottom-up approach. Some of
the programming languages that uses OOP technique is, C++, Small
Talk, VC++, Simula 67, C# and Java.
Object Oriented Programming Paradigm: In object oriented
programming paradigm every problem is viewed as an object rather
than the procedure. Hence, the object is a combination of data and
methods that operate on data. Some of the features of object
oriented programming language is as follows:
- Focus is one data rather than the procedure.
- Programs are decomposed into objects.
- Data can be protected by the external non-member
functions.
- Objects may communicate with each other through methods.
- OOP follows a bottom-up approach.
The naming conventions used in Procedural Oriented Programming
(POP) and Object Oriented Programming (OOP) are listed as below.
Procedural oriented Object oriented
programming (POP) programming (OOP)
Variable Objects
User-defined data types Classes
Structure members Instance variables
Functions Methods
Function call Message passing
Difference between procedural oriented programming and
object-oriented programming.
Procedural oriented Object oriented
programming programming
1. Emphasizes on the 1. Emphasizes on the
procedure rather than data rather than the
data. procedure.
2. Data is not secured. 2. Data is secured.
3. Uses a top-down 3. Uses bottom-up
approach. approach.
4. It does not model real 4. It models real world
world entities. entities.
5. Programs are 5. Programs are
decomposed into decomposed into
functions or objects.
procedures.
OOP emphasizes on data rather than procedure. Every given
problem is viewed as an object rather than procedure. The ideology
here is to unite both data and functions that operate on data into a
single unit called object.
Concepts of OOP:
- Objects
- Class
- Data abstraction
- Data encapsulation
- inheritance
- polymorphism
- dynamic binding
- message passing
Object: Object is an identifiable entity with some characteristics and
behaviour. Object may be a person, place or table of data. Let us
take an example, a mobile cell phone consisting of its features such
as color, weight, price etc. are data whereas a mobile cell phone is
used to make an audio or video call, take photographs, make videos
etc are the operations performed are called functions. Each object is
having both of these two features i.e. data and functions. An object
is any real-world entity that has its existence has both features
called data members and operations called function members.
- Example, apple, orange, banana etc are the objects of the
group called fruit.
- Similarly, roses, jasmine, lilies etc are the objects of the
group called flowers.
Object: student
Data members
Rollno.
Name
percentage
Methods
getdata()
putdata()
Class: class is a fundamental building block of object-oriented
program which serves as a blueprint. Class is a user defined data
type. Class acts as a template using which we will be able to create
any number of objects of that type. A class is a collection of similar
objects that share common features.
Example: in the above example of objects, the group of fruit is
called class. A group of flowers is called class.
Similarly, the solar system is a class whereas, an individual planet in
a solar system is called an object.
class
Data members
Data1
Data2
…
Data-n
Methods
Method 1
Method 2
…
Method-n
Data abstraction: the act of including only essential details of an
entity without including background details about it is called data
abstraction.
Example, let us consider the mobile cell phone with attributes such
as, color, Bluetooth, mobile cell phone camera etc. we are not
considering how Bluetooth works, or how the camera works or even
how the circuit inside the mobile is designed and what is happening
inside. We are interested only in using the mobile cell phone and its
features. Therefore, it is required to know how to operate a mobile
cell phone rather than its internal working, the internal working is
hidden from the user is called abstracted data.
Data encapsulation: the process of wrapping up data and
functions into a single unit called class is called data encapsulation.
Data encapsulation makes data hidden; data hiding is an act of
insulating data from outside programs. The data is not accessible
directly to the outside world, except for the functions defined inside
the class definition. These function members provide an interface
between the data and outside world to access the data out of class.
Inheritance: it is a process of acquiring properties from an object
of one class to properties of an object of another class. The class
that inherits the properties to the other class is called base class or
parent class or super class.
The class that inherits the properties from the other class is called
derived class or child class or subclass.
The programmers can add new features to the already existing class
without modifying that class. This is achieved by deriving a new
class from the already existing class. The derived class has its base
class features, in addition to that it has its own features.
The process of two or more functions having the same name with
different return types or different number of arguments is called
function overloading.
Here paint() items are single interface and paint() furniture, paint()
vehicle, paint() building and paint() fabric are many methods.
Example:
int add (int a, int b);
float add (float x, float y) ;
Here, add is a function name which is common in both the functions
but return types of these two functions are different, one is int and
another is float. And the number of arguments is the same in both
the functions i.e. 2. Function overloading is used when we want to
perform the same operations on different data. Function overloading
has the same interface but many methods. You can observe in the
above example add is the function name i.e. interface and many
methods means we have addition operations to be performed on
two different data types.
Dynamic binding: binding is the process of providing reference
between more than one module. Dynamic binding means the code
associated with a given procedure call is not known until the time of
the call at run-time or at the time of program execution.
Message passing: message passing is a request for execution of a
procedure for an object. Therefore, a message invokes a procedure
in the receiving object to generate the desired output.
Applications of OOP:
- Computer graphic applications.
- CAD/CAM software.
- Object oriented databases.
- User interface design such as windows.
- Real-time systems.
- Simulation and modelling.
- Artificial intelligence and expert systems.
- Hypermedia, expert text and hypertext.
- Decision support systems and office automation systems.
- Parallel programming and neural networks.
EXERCISES
1. What are the features of OOP?
2. List the naming conventions used in POP and OOP.
3. Differentiate between POP and OOP.
4. Explain the basic concepts of OOP.
5. Describe the advantages of OOP.
6. List the applications of OOP.
CHAPTER 2
EVOLUTION OF JAVA
Java history:
Java is a general purpose object oriented programming language
developed by Sun Microsystems of USA in 1991. Earlier java was
called oak (because it is a symbol of strength and chosen as a
national tree of many countries) by James Gosling (One of the
inventors of the language) who is known as Father of Java. In 1995
Oak was renamed as Java. Java is an island in Indonesia. Developed
this language to make it platform independent meaning that the
programs written in java can be executed on any of the systems
irrespective of hardware and operating system. Java programs are
written and executed on any of the computer systems, because java
produces the intermediate code called bytecode that can be
interpreted by any system as it is not tied with hardware and
software configuration of the computer system. The team members
of java also known as Green Team, initiated the project to develop a
programming language for digital devices like, set-top box,
interactive television etc. It was best suited for internet
programming. Presently, Java is used in internet programming, like
mobile devices. Electronic games, electronic business solutions etc.
Features of Java:
The primary objective of developing Java programming language
was to make it portable, simple and secure programming language.
In addition to this, there are some other features of Java as
described below.
- Simple: Java is simple, easy to learn and easy to understand
because Java syntax is similar to C++ programming language.
Java is made simple by removing some complicated features of
C++ such as explicit pointers, friend functions, inline functions,
goto statements, operator overloading etc. Java allocates and
deallocates memory for the object at runtime hence, the
objects are allocated dynamically and deleted by automatic
garbage collection once the program execution terminates.
- Object Oriented: Everything in java is considered as an
object. The software is organized as a combination of different
types of objects that incorporate both data and behavior.
Object oriented methodology makes the software development
process easy and provides certain rules for maintenance.
- Platform independent: Java is not dependent on machine
hardware as well as software. It is completely independent of
any platforms. A platform is the hardware or software
environment in which a program runs. Java can be executed on
multiple platforms, for example, windows, Linux, sun Solaris,
Mac/OS, etc. Java is compiled by the compiler and converted
into byte code. This byte code is also called as intermediate
code that can be interpreted by the Java Virtual Machine (JVM).
This byte code can be executed on any of the platforms
irrespective of their configurations. Hence, java is a portable
programming language. Java programs can be easily moved
from one computer system to another. Java is architecture
neutral, in C programming, int data type takes 2 bytes of
memory for 32 bit architecture and 4 bytes for 64 bit
architecture. However, it takes 4 bytes of memory for both 32
and 64 bit architecture in Java.
- Secured: Java is known for security. As java is used to
develop internet applications called applets, java ensures that
no viruses are communicated with an applet. As java does not
support pointers, it ensures that the programs cannot gain
access to memory locations without proper authorization.
- High performance: Java is both a compiled and interpreted
programming language. Java compiler produces an
intermediate code called byte code. This byte code can be
executed on any of the computer systems. This intermediate
code of java makes the java program execute the program
faster. Hence, provides high performance.
- Robust: java has the ability to use strong memory
management. As there is no use of pointers in java avoids
security issues. It provides garbage collection which runs on
the java virtual machine to delete the unwanted objects that
are no longer required. Java strongly checks for type and has
an exception handling mechanism. Hence, makes Java a robust
programming language.
- Distributed: Java is designed to distribute the applications
on the networks. Java applications allows programmers to
access remote objects on the internet, enables multiple
programmers at remote locations to collaborate with one
another and work together on a single project.
- Multi-threaded: A thread is a sequence of finite sets of
instructions written as a separate program, executing
concurrently. If multiple threads are to be executed, then the
advantage of using multi thread is that, instead of allocating
memory for each thread, it shares the common memory
location. Threads are used for multimedia, web applications
etc.
- Dynamic: Java is capable of dynamically linking in new class
libraries, methods and objects. Java supports functions written
in C and C++ programming languages. These functions are
known as native methods. These methods are linked
dynamically at runtime. Even the objects are allocated memory
space at the time of program execution and frees up the
memory locations by the built-in system called garbage
collection, as soon as the program terminates its execution.
How does Java differ from C?
Some of the features of the C programming language that causes
problems are eliminated from java. Those features are list as below:
- The user defined data types structures and unions are not
included.
- In java, header files or preprocessor directives are not
required.
- No sizeof and typedef statements are used.
- Java does not support the type modifiers such as auto ,
extern , register , signed and unsigned .
- The goto statement is omitted.
- Java does not support explicit pointers.
- Java adds new operators such as instanceof and >>>.
- Java adds labeled break and continue statements.
- Functions in java that accept no parameters must be declared
with empty brackets and not using void .
How does Java differ from C++?
Some of the features present in C++ programming language are
eliminated in java that are listed as below.
- Java does not support operator overloading.
- Java does not use default arguments.
- Java doesn’t support multiple inheritance (instead java uses
interface).
- Java does not support global variables. Every variable and
method must be declared in a class.
- Java does not support destructor functions. Instead, java
uses a method called finalize().
- Java is both a compiled and interpreted programming
language. Java source code is converted into byte code at
compile time and the interpreter executes this bytecode at
runtime and produces output.
Java and internet: Java is associated with the internet. Web
browsers to run applets on the internet. Java programming language
is used to create applet programs and run them locally using a java
enabled browser such as hot java, used to download an applet
located on the internet and it on the local computer system. Internet
users can set their websites containing java applets that could be
used by other remote users of the internet. Because of this fact, java
is also known as an internet language.
Java and World Wide Web: java communicates with a web page
through a special tag called <applet>. Java users send a request for
an HTML document to the remote computers and browser. The web
browser is a program that accepts a request, processes the request
and sends the required documents to the local computer. The
document contains the applet tag which identifies the applet. The
corresponding applet is transferred to the local computer. The java
enabled browser on the local computer interprets the byte code and
provides output.
www is a collection of information stored on the internet. It is an
information retrieval system. www contains web pages that provide
both information and controls. Web pages contain HTML tags that
enable us to retrieve, manipulate and display documents across the
globe. Java enables web pages to display animation, graphics, and
special effects. The following figure depicts exactly how the local
computer system user communicates to the java applet on the
remote computer system.
Web browsers: web browser is an application software used to
locate, fetch and show the content on the world wide web. Web
browser acts as a client whereas the remote computer holding
information acts as a web server. Web browser sends a request
object for information to the remote web server. The web server
responds back by sending a response object to the web browser that
contains information. The information fetched from the server
contains HTML tags that are interpreted by the browser. Some of the
examples for web browsers are, hot java, Netscape navigator,
internet explorer, google chrome, firefox mozilla etc.
Java environment: java environment includes a large number of
development tools and number of classes and methods. The
development tools are present in a unit called Java Development
Kit (JDK) and the classes and methods are present in a unit called
Java Standard Library (JSL) or Application Programming
Interface (API)
Java Development Kit:
Tools Description
Applet Enables to run java applets.
viewer
Java Java interpreter which runs both
applet and application by interpreting
byte code.
Javac The java compiler which translates
java source code to byte code.
Javadoc Create HTML format documentation.
Javah Produces header files for use with
native methods.
Javap Java disassembler enables to convert
byte code into program description.
Jdb Java debugger helps to find errors in
the program.
FirstProgram.java FirstProgram.class
“‘Fate . . .
Bade through the deep recesses of our breast
The unregarded river of our life
Pursue with indiscernible flow its way;
“The room where we sat before a grate fire seemed filled with the
thought of the noble man who penned the poem, and the evening
was a most enjoyable one.”
Harper’s was a nature quick to respond to the beautiful and true
wherever found, whether in prose or verse, in music or painting, or in
the actions of daily life. He was, moreover, intensely sympathetic, and
what he read or saw always impressed, and sometimes affected, him
deeply. He would often rise from the reading of a beautiful poem, or
the story of some heroic human effort, with eyes filled and voice
completely overcome, and then, as a means of gaining relief, and at
the same time of giving expression to his feelings, would pen in a
single sentence or two the thought that was most in his mind at the
time.
Such little entries as the following are a characteristic feature of
his diary, and reveal his sympathetic appreciation of what he read,
and of the subject treated:
“To-night I read the sad story of Keats’ life. How sad it is to see so
promising a man pass so soon! How admirably he declared a great
truth when he said,
was unfolded. Even that beautifully conceived court, with its noble
King, its high ideals and its battle-tried knights, went to utter ruin
through the example of one sin. Another thing which struck me was
that Tennyson, like others, shows that the deadliest enemy is the
Judas. The most cherished knight and beloved Queen poisoned the
court by betraying friend and husband. But Tennyson holds out the
beautiful hope of the thief upon the cross. Lancelot was allowed to die
a holy man; and Guinevere, by true repentance and goodly works,
was able to purge her soul so as to be prepared for the reunion
hereafter. The gentle teaching of the poem is that we must be swayed
by high resolves and noble motives.
“My admiration for the poem increased towards the close. The
delicate portrayal of character, and of utter pain and remorse in
Guinevere, and the beautiful imagery of The Passing of Arthur are
sublime—
“If this be so, can we not increase and make more lasting our
knowledge of these things by mastering ourselves and giving scope to
the spiritual side of us?”
THE LOVE OF OTHERS
“The poor downtrodden have more to hope for from men who,
having a specialized training in the operation of social forces, apply
themselves to the proper remedy, than from all the windy, ultra-
radical demagogues.”
“It is the alienation—partly, no doubt, due to indolence—of the
men of thought from those from whom the mass of the people
habitually receive their inspiration, which accounts for much of the
crass ignorance and purposeless passion of the people and their
demagogues.”
ebookball.com