Documente Academic
Documente Profesional
Documente Cultură
LAB MANUAL
Session 2013-2014
Branch : - CSE/IT
Year : - 4th
Semester : - 7th
In Java, most basic programming tasks are performed by the API’s classes and packages, which are
helpful in minimizing the number of lines written within pieces of code.
Java Development Kit (JDK) is comprised of three basic components, as follows:
Java compiler
Java Virtual Machine (JVM)
Java Application Programming Interface (API)
The Java API, included with the JDK, describes the function of each of its components. In Java
programming, many of these components are pre-created and commonly used. Thus, the
programmer is able to apply prewritten code via the Java API. After referring to the available API
classes and packages, the programmer easily invokes the necessary code classes and packages for
implementation.
The API is a library of available Java classes, packages and interfaces. The three API types are as
follows:
Official Java core API, which is bundled with the JDK download.
Optional official Java APIs, which may be downloaded if needed.
Unofficial APIs, which are third-party APIs that may be downloaded from source websites.
The API help programmers determine class or package functions, parameters and other necessary
information. The official API includes packages, e.g., applet packages, graphics and GUI swing
There are 3 types of Java Programming Language Application Programming Interfaces (APIs)
the official core Java API, contained in the JDK or JRE, of one of the editions of the Java Platform.
The three editions of the Java Platform are Java ME (Micro edition), Java SE (Standard edition),
and Java EE (Enterprise edition).
optional official APIs that can be downloaded separately. The specification of these APIs are
defined according to a Java Specification Request (JSR), and sometimes some of these APIs are
later included in the core APIs of the platform (the most notable example of this kind is Swing).
unofficial APIs, developed by third parties, but not related to any JSRs.
Third parties can freely implement any JSR specifications for an official API (even for the core API
of the language), providing that they conform to the Technology Compatibility Kit (TCK) for this
JSR (the TCK is a suite of tests that checks conformance of implementations for a JSR). The result
of this freedom is that many official APIs have more implementations than the Sun's Reference
implementation (RI).
1 Objective
To Learn Basic programming style in Java Programming
Development Environment.
To gain ability to write, compile, debug and execute Java Program.
Learning how to interface you with inbuilt Java Packages.
Ready to work with different Java SE APIs.
4 Solution
Step 1 Open Any Text Editor and write code as given below:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class EventApplet extends Applet implements ActionListener{
Button b;
TextField tf;
public void init(){
tf=new TextField();
tf.setBounds(30,40,150,20);
b=new Button("Click");
b.setBounds(80,150,60,50);
add(b);add(tf);
b.addActionListener(this);
Q3. How to do solve Tower of Hanoi using graphical Mode for only 3 or 4 disks.
2 Core java vol. 1,2 By Cay Horstmann; Gary Cornell (Available in Departmental and Central Library
at University campus)
While creating packages, the order of statements is very important. The order must be like this, else,
compilation error.
Package statement
/* FRAGEMENT CODE */
package MyPack;
public class MyArraySort
{
int arr[],lim;
public void sortMe()
{
// Code to sort the single dimension array if int type
}
}
package MyPack;
public class MyStringPallindrome
{
private String str;
5 Exercises:-
Q1 Write a program to print all the prime entries in an array in Same MyPack package
Q2 Write a program to print all non zero entries all positive and negative entries in same array of Q1.
2 Core java vol. 1,2 By Cay Horstmann; Gary Cornell (Available in Departmental and Central Library
at University campus)
A Java applet is a special kind of Java program that a browser enabled with Java
technology can download from the internet and run. An applet is typically embedded inside a
web page and runs in the context of a browser. An applet must be a subclass of the
java.applet.Applet class. The Applet class provides the standard interface between the applet
and the browser environment.Swing provides a special subclass of the Applet class called
javax.swing.JApplet. The JApplet class should be used for all applets that use Swing
components to construct their graphical user interfaces (GUIs). The browser's Java Plug-in
software manages the lifecycle of an applet. Use a web server to test the examples in this
lesson. The use of local applets is not recommended, and local applets are blocked when the
security level setting in the Java Control Panel is set to High or Very High.
Event Handling Mechanism:- Under the JDK1.02 event model, components are notified of events
by the invocation of a method that matches a specific signature. Applets can override these
methods as well - providing customized event handlers to respond to the actions of a user. An
example of this is shown below, where an overridden mouseMove method responds to user events.
In this method, we simply change the value of a member variable (message), and assign to it data
specific to the event. We call the repaint method, and our text is displayed to the user. Our applet
example responds to three different events (mouseMove, MouseExit, mouseDown), but there are many
events a custom component can respond to. Table One shows the major ones for subclasses of AWT
components, and Listing One the code for our example applet.
1 Objective
Learning the working of Java Applet Technologies in Java Programming
Model.
How to recognize different stages of Applet Life Cycle.
Learning how to create an Applet by extending Applet class
How to create customized events by implementing various interface
methods.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
TextField tf;
tf=new TextField();
tf.setBounds(30,40,150,20);
b=new Button("Click");
b.setBounds(80,150,60,50);
add(b);add(tf);
b.addActionListener(this);
setLayout(null);}
tf.setText("Welcome"); } }
5 Exercises:-
Q1 Write a Program to demonstrate the events handling mechanism in Applet
Programming Development Environment.
Q2 Write a Java Applet to demonstrate the use of various drawing methods and embed it
on web page.
2 Core java vol. 1,2 By Cay Horstmann; Gary Cornell (Available in Departmental and Central
Library at University campus)
Ready to use JDBC API to check and perform DML & DDL queries
Ready to create Java Database connectivity Program to talk to your
relational database of various domains.
Ready to analyze different stages involved in JDBC.
Ready to manage your Application by persistence of hidden data of the
Applicaion
import java.net.*;
import java.io.*;
Goodbye!
4 Core java vol. 1,2 By Cay Horstmann; Gary Cornell (Available in Departmental and Central Library
at University campus)
The distributed Hello World example uses a simple client to make a remote method invocation to a
server which may be running on a remote host. The client receives the "Hello, world!" message
from the server.
Note: For the remainder of this tutorial, the terms "remote object implementation" and "implementation
class" are used interchangeably to refer to the class example.hello.Server, which implements
a remote interface.
Here is the interface definition for the remote interface used in this example,
example.hello.Hello. It declares just one method, sayHello, which
returns a string to the caller
AdderRemote()throws RemoteException{
super();
}
public int add(int x,int y){return x+y;}
}
import java.rmi.*;
import java.rmi.server.*;
public class AdderRemote extends UnicastRemoteObject implements Adder{
AdderRemote()throws RemoteException{
super();
}
public int add(int x,int y){return x+y;}
}
rmic AdderRemote
rmiregistry 5000
For running this rmi example,
1) compile all the java files
javac *.java
2)create stub and skeleton object by rmic tool
rmic AdderRemote
3)start rmi registry in one command prompt
rmiregistry 5000
4)start the server in another command prompt
5 Exercises:-
Q1 Write a Program to implement Remote Method Invocations to check whether a file is
present in the client machine or not..
Core java vol. 1,2 By Cay Horstmann; Gary Cornell (Available in Departmental and Central Library at
University campus)
Multicasting in Java
1. Introduction
This article deals primarily with the subject of multicast communication in Java.
2. Sending multicast datagrams
In order to send any kind of datagram in Java, one needs a java.net.DatagramSocket:
DatagramSocket socket = new DatagramSocket();
This sample code creates the socket and a datagram to send and then simply sends the
same datagram every second:
DatagramSocket socket = new DatagramSocket();
byte[] b = new byte[DGRAM_LENGTH];
DatagramPacket dgram;
dgram = new DatagramPacket(b, b.length,
InetAddress.getByName(MCAST_ADDR), DEST_PORT);
System.err.println("Sending " + b.length + " bytes to " +
dgram.getAddress() + ':' + dgram.getPort());
while(true) {
System.err.print(".");
socket.send(dgram);
Thread.sleep(1000);
}
Valid values for the constants are:
•DGRAM_LENGTH: anything from 0 to 65507 (see section 5),
•MCAST_ADDR: any class D address (see appendix D), from 224.0.0.0 to 235.1.1.1,
•DEST_PORT: an unsigned 16-bit integer, eg. 6789 or 7777.
3. Receiving multicast datagrams
One can use a normal DatagramSocket to send and receive multicast datagrams as seen
in the section 2. In order to receive multicast datagrams, however, one needs a
MulticastSocket. The reason for this is simple, additional work needs to be done to
control and receive multicast traffic by all the protocol layers below UDP.
The example given below, opens a multicast socket, binds it to a specific port and joins
a specific multicast group:
byte[] b = new byte[BUFFER_LENGTH];
DatagramPacket dgram = new DatagramPacket(b, b.length);
MulticastSocket socket =
new MulticastSocket(DEST_PORT); // must bind receive side
socket.joinGroup(InetAddress.getByName(MCAST_ADDR));
while(true) {
socket.receive(dgram); // blocks until a datagram is received
System.err.println("Received " + dgram.getLength() +
" bytes from " + dgram.getAddress());
Exercise
Core java vol. 1,2 By Cay Horstmann; Gary Cornell (Available in Departmental and Central Library at
University campus)
The original call, INT 27h, is called 'terminate but stay resident', hence the name 'TSR'.
Using this call, a program can make up to 64 KB of its memory resident. MS-DOS version
2.0 introduced an improved call, INT 21h/function 31h ('Keep Process'), which removed
this limitation and let the program return an exit code. Before making this call, the program
can install one or several interrupt handlers pointing into itself, so that it can be called again.
Installing a hardware interrupt vector allows such a program to react to hardware events.
Installing a software interrupt vector allows it to be called by the currently running program.
Installing a timer interrupt handler allows a TSR to run periodically (see ISA and
programmable interval timer, especially the section "IBM PC compatible").
The typical method of utilizing an interrupt vector involves reading its present value (the
address), storing it within the memory space of the TSR, and installing a pointer to its own
code. The stored address is called before or after the TSR has received the interrupt and has
finished its processing, in effect forming a singly linked list of interrupt handlers, also called
By chaining the interrupt vectors TSR programs could take complete control of the
computer. A TSR could have one of two behaviors:
Take complete control of an interrupt by not calling other TSRs that had previously altered
the same interrupt vector.
Cascade with other TSRs by calling the old interrupt vector. This could be done before or
after they executed their actual code. This way TSRs could form a chain of programs where
each one calls the next one.
The 'terminate and stay resident' method was used by most DOS viruses which could either
take control of the PC or stay in the background. Viruses would react to disk I/O or
execution events by infecting executable (.EXE or .COM) files when they were run and data
files when they were opened.
Parts of DOS itself, especially in DOS versions 5.0 and later, used this same technique to
perform useful functions, such as the DOSKEY command-line editor and various other
installable utilities which were installed by running them at the command line (manually,
from AUTOEXEC.BAT or through INSTALL from within CONFIG.SYS) rather than
loading them as device drivers through DEVICE statements in CONFIG.SYS.
A TSR program can be loaded at any time; sometimes, they are loaded immediately after
the operating system's boot, by being explicitly loaded in the AUTOEXEC.BAT batch
program, or alternatively at the user's request (for example, Borland's Sidekick and Turbo
Debugger, Quicken's QuickPay, or FunStuff Software's Personal Calendar). These programs
will, as 'TSR' implies, stay resident in memory while other programs are executing. Some of
them do not have an option for unloading themselves from memory, so calling TSR means
the program will remain in memory until a reboot. However unloading is possible
externally, using utilities like the MARK.EXE/RELEASE.EXE combo by TurboPower
Software or soft reboot TSRs which will catch a specific key combination and release all
TSRs loaded after them. As the chain of ISRs is singly linked, there is no provision for
discovering the previous handler's address (other than attempting to trace back the interrupt
chain), or to inform its predecessor that it needs to update its "next address to which to
jump" not to point to the TSR which desires to remove itself, so that in order to safely
#include"dos.h"
#include<conio.h>
#include<stdlib.h>
//interrupt declarations
void interrupt (*prevtimer)();
void interrupt mytimer();
void writechar(char ch,int row,int col,int attr);
//timer function
Exercise
Core java vol. 1,2 By Cay Horstmann; Gary Cornell (Available in Departmental and Central Library at
University campus)