Sie sind auf Seite 1von 20

T.E.

Comp Sem I SDL

BRACT’s
Vishwakarma Institute of Information Technology, Pune
Department Of Computer Engineering

A.Y. 2017-18 Semester –I

Class: - TE (Computer) Div:- C

Skills Development Lab

SD Module-III: Advanced JAVA and GROOVY on GRAILS

List of Assignments

S.N. Assignment Name Start Date End Date


1. Design a system with the help of advance data structures 19/06/2017 30/06/2017
in Java and enhance the system using collections and
generics.
2. Enhance the above system with the help of socket 03/07/2017 14/07/2017
programming use client server architecture.
3. Enhance above system by using JDBC, Multithreading, 17/07/2017 28/07/2017
concurrency, synchronous and asynchronous callbacks,
Thread Pools using Executor Service.
4. Transform the above system from command line system 31/07/2017 18/08/2017
to GUI based application.
5. Download Install and Configure IDE with Grails Plug-in 21/08/2017 25/08/2017
on Windows/Linux platform.
6. Design a simple web application using Scaffolding data 28/08/2017 08/09/2017
source for CRUD operations.
7. Design a simple web application using MySQL for 11/09/2017 22/09/2017
CRUD operations.
8. Design & Implement dynamic web application system. 25/09/2017 06/10/2017

Mrs Fatima M Inamdar

Approved By:

Practical In-Charges HOD


Prepared By:

VIIT, Pune 1
T.E. Comp Sem I SDL

ASSIGNMENT NO.: 1

TITLE: Design a system with the help of advance data


structures in Java and enhance the system using collections
and generics.

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:

To understand different concepts in advanced java.

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.

Theory about Generics in Java


  
Introduced to deal with type-safe objects
  
Before generics -> able to store any type of objects in collection
  
After generics -> forces to store specific type of objects in collection
  
Advantages –
◦ Type-safety

VIIT, Pune 3
T.E. Comp Sem I SDL

◦ Type casting is not required


◦ Compile-Time checking
  
Syntax –
◦ ClassORInterface<Type>
◦ Example –
◦ ArrayList<String>

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

Platform: Ubentu, NetBeans / Ecilipse and JDK8.0.

VIIT, Pune 4
T.E. Comp Sem I SDL

ASSIGNMENT NO.: 2

TITLE: Enhance the above system with the help of socket


programming use client server architecture.

DATE:

REMARK:

VIIT, Pune 5
T.E. Comp Sem I SDL

Assignment –2

Title: Use of socket programming in Java .

Objective:
To understand concepts of client- server architecture in java.

Problem statement: Enhance the above System (StudentDB) with the help of socket
programming.

Theory: Theory about Serialization in Java


  
Is a mechanism of writing the state of an object into a byte stream
  
Mainly used in Hibernate, RMI, JPA, EJB and JMS technologies

 as
Reverse operation is know
Deserialization Example
  
Serializable Student Example
  
Student class implements Serializable interface
  
Hence marked as Serializable
 
Its objects can be converted into Stream

How to convert objects into stream?


  
Converting object into stream is Serialization
  
ObjectOutputStream class is used to –
◦ write primitive data types to an OutputStream
◦ Java objects to an OutputStream
  
Only objects of classes which are marked Serializable are supported

Example of writing Student object to 
File How to convert stream into object?
  
Converting stream back to object is Deserialization
  
Deserialization is process of reconstructing the object from the serialized state
  
ObjectInputStream class is used to –
◦ Deserialize java objects which are serialized by ObjectOutputStream
◦ Deserialize prmitive data type which written using ObjectOutputStream

 Example of Deserializing 
Student object from File
Theory about Networking
  
Is a concept of connecting two or more computing devices to share resources
  
Advantages –
◦ Share resources
Centralize software management

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

◦ public Socket accept()


 
Returns the socket and establish a connection between server and client

◦ public synchronized void close()
 
Close the server socket

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.

Platform: Ubentu, NetBeans / Ecilipse and JDK8.0.

VIIT, Pune 8
T.E. Comp Sem I SDL

ASSIGNMENT NO.: 3

TITLE: Enhance System by using JDBC, Multithreading,


Concurrency, Synchronous and Asynchronous callbacks,
Thread Pools using Execution Service

DATE:

REMARK:

VIIT, Pune 9
T.E. Comp Sem I SDL

Assignment –3

Title: Enhance System by using JDBC, Multithreading, Concurrency, Synchronous and


Asynchronous callbacks, Thread Pools using Execution Service

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

JDBC (Java Database Connectivity)

Steps: Following are the steps to connect to database using JDBC


1. Start the program
2. Create the class
3. Load the driver class
4. Create a connection object
5. Create a statement object
6. Get the connection and you can execute commands
7. Close the connection and statement objects

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.

getConnection() and createStatement()


Establish a connection using the getConnection() method of DriverManager class and use the
createStatement() method of the Connection object.

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.

Output: Connection with database established.


Platform: Windows 7/8, MySql database management system, NetBeans 6.9.X and JDK6.

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().

The default priority is NORM_PRIORITY


Two methods used to change priority: final void setPriority(int newp): changes the thread’s current
priority. final int getPriority(): returns the thread’s priority.

Output: Multiple threads are created and run successfully.


Platform: Windows 7/8, NetBeans 6.9.X and JDK6.

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

Output: Successfully implemented concept of concurrency.


Platform: Windows 7/8, NetBeans 6.9.X and JDK6.

Synchronous and Asynchronous callbacks

In computer programming, a callback is a piece of executable code that is passed as an argument


to other code, which is expected to call back (execute) the argument at some convenient time.

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.

An asynchronous or deferred callback is invoked after a function returns, or at least on another


thread’s stack. Mechanisms for deferral include threads and main loops (other names include event
loops, dispatchers, executors). Asynchronous callbacks are popular with IO-related APIs, such as
socket.connect (callback); you would expect that when connect() returns, the callback may not have
been called, since it’s waiting for the connection to complete.

Output: Successfully implemented concept of Syncronous and asynchronous callbacks.


Platform: Windows 7/8, NetBeans 6.9.X and JDK6.

Thread pools using execution service

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

TITLE: Develop and implement GUI based application

DATE:

REMARK:

VIIT, Pune 15
T.E. Comp Sem I SDL

Assignment –4

Title: Develop and implement GUI based application

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.

Step 3: Set border alignment for added components on frame.

Step 4: Execute application

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.

Step 3: Set border alignment for added components on jframe.

Step 4: Execute application

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).

Programming GUI with AWT:-

2.1 AWT Packages

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.

1. The java.awt package contains the core AWT graphics classes:


o GUI Component classes, such as Button, TextField, and Label,
o GUI Container classes, such as Frame and Panel,
o Layout managers, such as FlowLayout, BorderLayout and GridLayout,
o Custom graphics classes, such as Graphics, Color and Font.
2. The java.awt.event package supports event handling:
o Event classes, such as ActionEvent, MouseEvent, KeyEvent and WindowEvent,
o Event Listener Interfaces, such as ActionListener, MouseListener, KeyListener
and WindowListener,
o Event Listener Adapter classes, such as MouseAdapter, KeyAdapter, and
WindowAdapter.

AWT provides a platform-independent and device-independent interface to develop graphic


programs that runs on all platforms, including Windows, Mac OS, and Unixes.

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:

Swing API: for advanced graphical programming.


Accessibility API: provides assistive technology for the disabled.
Java 2D API: for high quality 2D graphics and images.
Pluggable look and feel supports.
Drag-and-drop support between Java and native applications.

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.

The main features of Swing are :

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:

AWT and Swing Event-Handling:-

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.

Output: 1. Visible frames display.


2. Frames must have minimize/miximize/close.
3. Application must handle key and mouse event.

Platform: Windows 7, NetBeans 6.9.X and JDK1.7 or Latest.

VIIT, Pune 20

Das könnte Ihnen auch gefallen