Beruflich Dokumente
Kultur Dokumente
BRACT’s
Vishwakarma Institute of Information Technology, Pune
Department Of Computer Engineering
List of Assignments
Approved By:
VIIT, Pune 1
T.E. Comp Sem I SDL
ASSIGNMENT NO.: 1
DATE:
REMARK:
VIIT, Pune 2
T.E. Comp Sem I SDL
Assignment –1
Title: Use of advance data structures in Java and enhance the system using collections and generics.
Objective:
Problem statement: Design a system(Example StuddentDB) with the help of advance data structures in
Java and enhance the system using collections and generics.
Theory: Theory about data strecturs used in advanced java . Syntax & Example of
each data structure.
◦ Enumeration
◦ BitSet
◦ Vector
◦ Stack
◦ Dictionary
◦ Hashtable
◦ Properties
But all these classes are legacy now
All are replaced by new framework Collections
Theory about Collections Framework in Java.
1. Definaitin
2. Interfaces in Collection framework–
List, Set, Queue, Dequeue
3. Classes implementing above interfaces –
ArrayList, LinkedList, Vector, PriorityQueue, HashSet, LinkedHashSet, TreeSet,
etc.
VIIT, Pune 3
T.E. Comp Sem I SDL
Output: Display all the operations for System (Studdent DB) such as addition , deletion ,
search, display, finding students whose getting marks less than 40% using appropriate data
structures & collection framework
VIIT, Pune 4
T.E. Comp Sem I SDL
ASSIGNMENT NO.: 2
DATE:
REMARK:
VIIT, Pune 5
T.E. Comp Sem I SDL
Assignment –2
Objective:
To understand concepts of client- server architecture in java.
Problem statement: Enhance the above System (StudentDB) with the help of socket
programming.
Terminologies of Networking
VIIT, Pune 6
T.E. Comp Sem I SDL
IP Address
◦ Logical address assigned to a node of network
Protocol
◦ Set of rules followed for communication
◦ Example – TCP, HTTP, UDP, FTP, SMTP, etc.
Port Number
◦ Used to uniquely identify the different applications
◦ Acts as communication end-point between two applications
MAC Address
◦ MAC (Media Access Control) is a unique identifier of NIC
(Network Interface Controller)
◦ A network node can have multiple NIC but each with unique MAC
Connection-Oriented protocol
◦ Acknowledgement is sent by receiver to sender
◦ Reliable but slow
◦ Example: TCP
Connection-less protocol
◦ Acknowledgement not sent to sender by receiver
◦ Not Reliable but fast
◦ Example: UDP
Socket
◦ End-point between two-way communication
Classes used for connection-oriented:
◦ Socket class
◦ ServerSocket class
◦ Classes used for connection-less:
◦ DatagramSocket class
◦ DatagramPacket class
◦ The client in Socket Programming must know –
◦ IP Address or Hostname of Server
◦ Port Number
Socket Class
◦ Used to create a client socket
Methods –
◦ public InputStream getInputStream()
Returns the InputStream attached with this Socket
◦ public OutputStream getOutputStream()
Returns the OutputStream attached with this Socket
◦ public synchronized void close()
Closes this socket
ServerSocket Class
◦ Used to create Server socket
◦ Object of class is used to establish connection between server and client
Methods –
VIIT, Pune 7
T.E. Comp Sem I SDL
Output: Display all the operations for System (Studdent DB) such as addition , deletion , search,
display, finding students whose getting marks less than 40% using socket programming.
VIIT, Pune 8
T.E. Comp Sem I SDL
ASSIGNMENT NO.: 3
DATE:
REMARK:
VIIT, Pune 9
T.E. Comp Sem I SDL
Assignment –3
Objective:
To understand JDBC connectivity i. e. a database can be accessed from program.
To understand the concept of multithreading, concurrency, synchronous and
asynchronous callbacks and thread pools using execution service.
Problem statement: Enhance System by using JDBC, Multithreading, Concurrency, Synchronous and
Asynchronous callbacks, Thread Pools using Execution Service
Theory:
Java Database Connectivity (JDBC) Application Programming Interface (API) is a part of the Java
APIs provided by Sun Microsystems that defines a set of API objects and methods to interact with
databases.
The JDBC classes are present in the java.sql package and all Java programs that need to interact with
databases use methods from this package.
Class.forName()
The driver is loaded into the Java interpreter using Class.forName() method.
VIIT, Pune 10
T.E. Comp Sem I SDL
executeQuery() or executeUpdate()
Finally, SQL statements are constructed and executed through the statement instance by using either
executeQuery() or executeUpdate() methods.
PreparedStatement
The PreparedStatement interface allows us to create pre-compiled SQL statements and gives the
option of specifying the parameters for the statement at a later stage.
ResultSet can be scrollable or non scrollable and first(), last(), afterLast(), beforeFirst() are some of the
methods for moving the cursor in result set.
Multithreading
Steps: Following are the steps in order to implement concept of multi-threading in Java
1. Create a thread class by implementing the Runnable interface.
2. Start the thread in the constructor.
3. Implement the run () method.
4. Create multiple threads in the main() method
5. Display the threads
When Java programs execute, there is always one thread running and that is the main thread.
It is this thread from which child threads are created. Program is terminated when main thread stops
execution. Main thread can be controlled through Thread objects.
java.lang package
Declare a class that is a sub-class of the class Thread defined in java.lang
package. For example: class mythread extends Thread
currentThread()
Reference of the main thread can be obtained by calling the currentThread() method of the Thread
class.
Runnable interface
Declare a class that implements the Runnable interface.
For example: class mythread implements Runnable
VIIT, Pune 11
T.E. Comp Sem I SDL
Start()
After a new thread has been initiated, we use the start() method to start the thread, otherwise it is an
empty Thread object with no system resources allocated
sleep()
This method puts particular thread to sleep
wait()
Thread is waiting because wait() method was called. wait() method tells the calling thread to exit and
enter the sleep state till some other thread enters the monitor and calls the notify() method.
notify()
notify()method wakes up the first thread that called wait().
notifyAll()
notifyAll() wakes up or notifies all the threads that called wait().
Concurrency
Working with the Thread class can be very tedious and error-prone. Due to that reason the
Concurrency API has been introduced back in 2004 with the release of Java 5. The API is located in
package java.util.concurrent and contains many useful classes for handling concurrent programming,
such as ConcurrentHashMap, ConcurrentSkipListMap, ConcurrentSkipListSet,
CopyOnWriteArrayList, and CopyOnWriteArraySet.
When many threads are expected to access a given collection, a ConcurrentHashMap is normally
preferable to a synchronized HashMap, and a ConcurrentSkipListMap is normally preferable to a
synchronized TreeMap. A CopyOnWriteArrayList is preferable to a synchronized ArrayList when the
expected number of reads and traversals greatly outnumber the number of updates to a list.
VIIT, Pune 12
T.E. Comp Sem I SDL
A synchronous callback is invoked before a function returns, that is, while the API receiving the
callback remains on the stack. An example might be: list.foreach(callback); when foreach() returns,
you would expect that the callback had been invoked on each element.
Executors class
This simple class allows you to create thread pools and thread factories.
Java thread pool manages the pool of worker threads, it contains a queue that keeps tasks waiting to
get executed. We can use ThreadPoolExecutor to create thread pool in java. Java thread pool manages
the collection of Runnable threads and worker threads execute Runnable from the queue.
java.util.concurrent.Executors
java.util.concurrent.Executors provide implementation of java.util.concurrent.Executor interface to
create the thread pool in java.
VIIT, Pune 13
T.E. Comp Sem I SDL
ThreadPoolExecutor
Executors class provide simple implementation of ExecutorService using ThreadPoolExecutor but
ThreadPoolExecutor provides much more feature than that. We can specify the number of threads that
will be alive when we create ThreadPoolExecutor instance and we can limit the size of thread pool and
create our own RejectedExecutionHandler implementation to handle the jobs that can't fit in the
worker queue.
VIIT, Pune 14
T.E. Comp Sem I SDL
ASSIGNMENT NO.: 4
DATE:
REMARK:
VIIT, Pune 15
T.E. Comp Sem I SDL
Assignment –4
Objective:
To study and use various components of java GUI development
To use various AWT components like Button, Text, and TextBox etc.
To study and use event handling in java GUI.
Problem statement: Develop and implement GUI based application using AWT/Swing.
Steps:
AWT :
Step 1: Create java awt GUI application by Creating a frame in AWT. For this extend Frame class
which is available in java.awt.*
Step 2: Add different GUI components available in awt on frame like button, text, and radio
button etc.
SWING :
Step 1: Create java Swing GUI application by Creating a Jframe in Swing. For this extend JFrame
class which is available in javax.swing.*
Step 2: Add different GUI components available in swing on jframe like jbotton, jtext, and jlist etc.
VIIT, Pune 16
T.E. Comp Sem I SDL
Theory:
GUI in Java:
There are two sets of Java APIs for graphics programming: AWT (Abstract Windowing Toolkit)
and Swing.
1. AWT API was introduced in JDK 1.0. Most of the AWT components have become
obsolete and should be replaced by newer Swing components.
2. Swing API, a much more comprehensive set of graphics libraries that enhances the AWT,
was introduced as part of Java Foundation Classes (JFC).
AWT is huge! It consists of 12 packages of 370 classes (Swing is even bigger, with 18 packages of
737 classes as of JDK 1.8). Only 2 packages - java.awt and java.awt.event - are commonly-used.
Swing :
Swing is part of the so-called "Java Foundation Classes (JFC)" JFC was subsequently included as an
integral part of JDK since JDK 1.2. JFC consists of:
VIIT, Pune 17
T.E. Comp Sem I SDL
The goal of Java GUI programming is to allow the programmer to build GUI that looks good on ALL
platforms. JDK 1.0's AWT was awkward and non-object-oriented (using many event.getSource()).
JDK 1.1's AWT introduced event-delegation (event-driven) model, much clearer and object-oriented.
JDK 1.1 also introduced inner class and JavaBeans – a component programming model for visual
programming environment (similar to Visual Basic and Dephi).
Swing appeared after JDK 1.1. It was introduced into JDK 1.1 as part of an add-on JFC (Java
Foundation Classes). Swing is a rich set of easy-to-use, easy-to-understand JavaBean GUI components
that can be dragged and dropped as "GUI builders" in visual programming environment. Swing is now
an integral part of Java since JDK 1.2. Swing is huge (consists of 18 packages of 737 classes as in
JDK 1.8) and has great depth. Compared with AWT, Swing provides a huge and comprehensive
collection of reusable GUI components.
1. Swing is written in pure Java (except a few classes) and therefore is 100% portable.
2. Swing components are lightweight. The AWT components are heavyweight (in terms of
system resource utilization). Each AWT component has its own opaque native display, and
always displays on top of the lightweight components. AWT components rely heavily on the
underlying windowing subsystem of the native operating system. For example, an AWT button
ties to an actual button in the underlying native windowing subsystem, and relies on the native
windowing subsystem for their rendering and processing. Swing components (JComponents)
are written in Java. They are generally not "weight-down" by complex GUI considerations
imposed by the underlying windowing subsystem.
3. Swing components support pluggable look-and-feel. You can choose between Java look-and-
feel and the look-and-feel of the underlying OS (e.g., Windows, UNIX or Mac). If the later is
chosen, a Swing button runs on the Windows looks like a Windows' button and feels like a
Window's button. Similarly, a Swing button runs on the UNIX looks like a UNIX's button and
feels like a UNIX's button.
4. Swing supports mouse-less operation, i.e., it can operate entirely using keyboard.
5. Swing components support "tool-tips".
6. Swing components are JavaBeans – a Component-based Model used in Visual Programming
(like Visual Basic). You can drag-and-drop a Swing component into a "design form" using a
"GUI builder" and double-click to attach an event handler.
7. Swing application uses AWT event-handling classes (in package java.awt.event). Swing
added some new classes in package javax.swing.event, but they are not frequently used.
8. Swing application uses AWT's layout manager (such as FlowLayout and BorderLayout in
package java.awt). It added new layout managers, such as Springs, Struts, and BoxLayout
(in package javax.swing).
9. Swing implements double-buffering and automatic repaint batching for smoother screen
repaint.
VIIT, Pune 18
T.E. Comp Sem I SDL
10. Swing introduces JLayeredPane and JInternalFrame for creating Multiple Document
Interface (MDI) applications.
11. Swing supports floating toolbars (in JToolBar), splitter control, "undo".
12. Others - check the Swing website.
Example of swing component:-
Swing’s components:
Java adopts the so-called "Event-Driven" (or "Event-Delegation") programming model for event-handling,
similar to most of the visual programming languages (such as Visual Basic and Delphi). In event-driven
programming, a piece of event-handling codes is executed (or called back by the graphics subsystem)
when an event was fired in response to an user input (such as clicking a mouse button or hitting the
ENTER key). This is unlike the procedural model, where codes are executed in a sequential manner. The
AWT's event-handling classes are kept in package java.awt.event. Three objects are
VIIT, Pune 19
T.E. Comp Sem I SDL
involved in the event-handling: a source, listener(s) and an event object. The source object (such as
Button and Textfield) interacts with the user. Upon triggered, it creates an event object. This event
object will be messaged to all the registered listener object(s), and an appropriate event-handler
method of the listener(s) is called-back to provide the response. In other words, triggering a source
fires an event to all its listener(s), and invoke an appropriate handler of the listener(s). To express
interest for a certain source's event, the listener(s) must be registered with the source. In other words,
the listener(s) "subscribes" to a source's event, and the source "publishes" the event to all its
subscribers upon activation. This is known as subscribe-publish or observable-observer design pattern.
VIIT, Pune 20