Java Programming-Unit 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Programming in Java

IVth Semester
CIC-212

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


File Handling

1. The java.io package contains nearly every class you might ever
need to perform input and output (I/O) in Java
2. All these streams represent an input source and an output
destination
3. The stream in the java.io package supports many data such as
primitives, Object, localized characters, etc.
4. A stream can be defined as a sequence of data
5. The InputStream is used to read data from a source and the
OutputStream is used for writing data to a destination

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Stream

A stream is a sequence of data. In Java a stream is


composed of bytes. It's called a stream because it's
like a stream of water that continues to flow.
1. System.out: standard output stream
2. System.in: standard input stream
3. System.err: standard error stream

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Examples

1. Use of FileReader Class – Example(Reading Character


from File)
2. Use of FileWriter Class – Example(Writing Character to
file)
3. Use of FileInputStream Class – Example(Reading Bytes
from File)
4. Use of FileOutputStream Class – Example(Writing Bytes
to File)

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Reading/Writting Console Data

1. Java input console is accomplished by reading from System.in. To obtain a


character-based stream that is attached to the console, you wrap System.in in a
BufferedReader object, to create a character stream. Once BufferedReader is
obtained, we can use read( ) method to reach a character or readLine( ) method to
read a string from the console.
1. Reading characters – Example
2. Reading String – Example
3. Writing Console Output - Console output is most easily accomplished with print( )
and println( ). These methods are defined by the class PrintStream which is the
type of the object referenced by System.out. Even though System.out is a byte
stream, using it for simple program output is still acceptable. Because
PrintStream is an output stream derived from OutputStream, it also implements
the low-level method write( ). Thus, write( ) can be used to write to the console.
Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Collections

1. A framework that provides an architecture to store and


manipulate the group of objects.
2. Java Collection framework provides many interfaces (Set,
List, Queue, Deque etc.) and classes (ArrayList, Vector,
LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet
etc).
3. The java.util package contains all the classes and interfaces
for Collection framework.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Java ArrayList class

1.Dynamic Array For Storing The Elements


2.Can Contain Duplicate Elements
3.Maintains Insertion Order
4.Non Synchronized
5.Allows Random Access Because Array Works At The
Index Basis.
Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


LinkedList class

1. Uses doubly linked list to store the elements


2. Can contain duplicate elements
3. Maintains insertion order
4. Non synchronized
5. Can be used as list, stack or queue
Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Difference between ArrayList &LinkedList

ArrayList LinkedList
1) ArrayList internally uses dynamic array to LinkedList internally uses doubly linked list to
store the elements. store the elements.

2) Manipulation with ArrayList is slow because Manipulation with LinkedList is faster than
it internally uses array. If any element is ArrayList because it uses doubly linked list so
removed from the array, all the bits are shifted no bit shifting is required in memory.
in memory.

3) ArrayList class can act as a list only because LinkedList class can act as a list and
it implements List only. queue both because it implements List and
Deque interfaces.

4) ArrayList is better for storing and LinkedList is better for manipulating data.
accessing data.of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi
Department
List Interface

1. List Interface is the sub interface of Collection. It contains


methods to insert and delete elements in index basis.
2. Commonly used methods of ListIterator Interface:
1. public boolean hasNext();
2. public Object next();
3. public boolean hasPrevious();
4. public Object previous();
3. Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


HashMap class

1. Contains values based on the key


2. Contains only unique elements
3. May have one null key and multiple null
values
4. Maintains no order
5. Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Event Handling

1. Event is the change in the state of the object or source. Events are
generated as result of user interaction with the graphical user interface
components. For example, clicking on a button, moving the mouse,
entering a character through keyboard, selecting an item from list, scrolling
the page are the activities that causes an event to happen.
2. Event Handling is the mechanism that controls the event and decides what
should happen if an event occurs. This mechanism have the code which is
known as event handler that is executed when an event occurs. Java Uses
the Delegation Event Model to handle the events. This model defines the
standard mechanism to generate and handle the events

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Delegation Event Model

1. Source - The source is an object on which event occurs. Source is


responsible for providing information of the occurred event to
its handler. Java provide as with classes for source object.
2. Listener - It is also known as event handler. Listener is
responsible for generating response to an event. From java
implementation point of view the listener is also an object.
Listener waits until it receives an event. Once the event is
received, the listener process the event then returns.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Benefits of Delegation Event Model

1. User interface logic is completely separated from the logic that


generates the event.
2. The user interface element is able to delegate the processing of
an event to the separate piece of code.
3. In this model ,Listener needs to be registered with the source
object so that the listener can receive the event notification.
4. This is an efficient way of handling the event because the event
notifications are sent only to those listener that want to receive
them.

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Event Handling Examples

ActionListener Example - This interface is used to handle the events caused by sources like Buttons, Menu Items, Enter Keys and Double Click of Mouse. When you
implements ActionListener Interface following methods needs to be override. public void actionPerformed(ActionEvent ae) - Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Event Handling Examples

ItemListener Example - Used for Radio Button, List, Choice and Check
Box AWT Controls. The method that needs to be override is as follows: public
void itemStateChanged(ItemEvent e) – Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Event Handling Examples

AdjustmentListener Example - Used to handle events generated


from Scrollbar source. This interface has only method which is: public void
adjustmentValueChanged(AdjustmentEvent e) – Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Event Handling Examples

KeyListener Example - This interface is used to handle the


events generated from keys of the keyboard. There are three
methods present in the KeyListener Interface and all methods
needs to be override.
1. public void keyPressed(KeyEvent ke)
2. public void keyTyped(KeyEvent ke)
3. public void keyReleased(KeyEvent ke)

Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Event Handling Examples

MouseMotionListener - As the name suggests this interface is used to handle events


generated from Mouse Source.
Methods are as follows:
public void mouseMoved(MouseEvent me)
public void mouseDragged(MouseEvent me)
MouseListener - Methods are as follows:
public void mousePressed(MouseEvent me)
public void mouseClicked(MouseEvent me)
public void mouseReleased(MouseEvent me)
public void mouseEntered(MouseEvent me)
public void mouseExited(MouseEvent me)

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi


Adapter Classes

Adapter classes are used to simplify the process of event handling


in Java. As we know that when we implement any interface all the
methods defined in that interface needs to be override in the class,
which is not desirable in the case of Event Handling.
Adapter classes are useful as they provide empty implementation
of all methods in an event listener interface. In this you can define
a new class to act as event listener by extending one of the adapter
classes and implementing only those methods that you want to use
in your program - Example

Department of COMPUTER SCIENCE & ENGINEERING, BVCOE New Delhi

You might also like