Java Lab Mannual

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

BANNARI AMMAN INSTITUTE OF TECHNOLOGY

SATHYAMANGALAM

Department of Computer Science And Engineering

11Z408
CONCURRENT PROGRAMMING
LABORATORY

LAB MANUAL

Concurrent Programming Lab Manual 1 CSE Department


11Z408 - CONCURRENT PROGRAMMING LABORATORY
List of Experiments

S.No Title of the Experiments Page No


1. Program on Classes and Methods. 3
2. Implementation of Inheritance. 5
3. Implementation of Interfaces and Packages. 7
4. Implementation of Multithreaded Programming. 13
5. Develop a program to implement String Handling Methods. 15
6. Implementation of Collections Interfaces and Classes. 17
7. Implementation of Exception handling mechanisms. 19
8. Write a program to perform I/O Streams. 21
9. Implementation of Applet Programs. 22
10. Implementation of AWT Controls. 25
11. Implementation of I/O files. 27
12. Write a program to implement Event Classes 29
13. Implementation of JDBC concepts. 31

Concurrent Programming Lab Manual 2 CSE Department


EX.NO: 1 Program on Classes and Methods.
AIM:
Write a java program to generate the Fibonacci series, given number of n values.

ALGORITHM:
1. Start the program.
2. Create a class and variables with data types.
3. Declaration of the main class.
4. Read a string with inputstreamReader(System.in).
5. convert the string into Integer.parseInt(stdin.readLine());
6. By using for loop rotating the integer value.
7. Repeats enter the value until end of loop.
8. End of class and main method.
9. Stop the program.

PROGRAM:
import java.io.*; //importing io package
import java.lang.*; //importing Lang package
class A
{
int a,b,c;
A( int f1,int f2 )
{
a=f1;
b=f2;
} //End of constructor A
void Feb()
{
c=a+b;
System.out.print("\t" + c);
a=b;
b=c;
} //End of method Feb
} //End of class A

class Febinocci
{
public static void main(String args[]) throws IOException
{
int n,f3, i;
A a=new A(0,1);
System.out.println("Enter how many numbers you want in febinoci series");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(stdin.readLine());
System.out.println("The febinocci series is as follows");
System.out.print("\t" + 0);
System.out.print("\t" + 1);
for(i=0;i<(n-2);i++)
{
a.Feb();
} //End of for loop

Concurrent Programming Lab Manual 3 CSE Department


} //End of main
} //End of class Febinocci

OUTPUT
Enter how many numbers you want in febinoci series 3
The febinocci series is as follows 0 1 1
Enter how many numbers you want in febinoci series 6
The febinocci series is as follows 0 1 1 2 3 5
Enter how many numbers you want in febinoci series 10
The febinocci series is as follows 0 1 1 2 3 5 8 13 21 34

RESULT:

Thus the program for generating Fibonacci series was executed and its output was verified.

Concurrent Programming Lab Manual 4 CSE Department


EX.NO: 2 Implementation of Inheritance
AIM:
Write a java program to implement the Inheritance program.

ALGORITHM:
1. Start the program, import the packages.
2. Create a class and variables with data types.
3. Declare the methods in different names in Arithmetic operations.
4. Arguments give a throws IOExceptions.
5. Read a string with inputstreamReader(System.in).
6. convert the string into Integer.parseInt(stdin.readLine());
7. Create a object to call the procedure.
8. Print the concatenation of string.
9. Stop the program.

PROGRAM:

import java.io.*;
import java.lang.*;
class Add
{
int c;
void add(int a,int b)
{
c=a+b;
System.out.println("Result of adding is "+ c);
}
}
class Sub extends Add
{
void sub(int a,int b)
{
c=a-b;
System.out.println("Result of subtracting is "+ c);
}
}
class Mul extends Sub
{
void mul(int a,int b)
{
c=a*b;
System.out.println("Result of multiplying is "+ c);
}
}
class Inherit
{
public static void main(String args[]) throws IOException
{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 2 numbers to perform add,sub and mul");

Concurrent Programming Lab Manual 5 CSE Department


int i=Integer.parseInt(stdin.readLine());
int j=Integer.parseInt(stdin.readLine());
Mul m=new Mul();
m.mul(i,j);
m.add(i,j);
m.sub(i,j);
}
}

OUTPUT
Enter 2 numbers to perform add,sub and mul
10 20
Result of multiplying is 300
Result of adding is 30
Result of subtracting is -10
Enter 2 numbers to perform add,sub and mul
5 10
Result of multiplying is 50
Result of adding is 15
Result of subtracting is -5
Enter 2 numbers to perform add,sub and mul
2 1
Result of multiplying is 2
Result of adding is 3
Result of subtracting is 1

RESULT:
Thus the program implementing inheritance and its output was verified.

Concurrent Programming Lab Manual 6 CSE Department


EX.NO: 3a. Implementation of Interfaces

AIM:
To design a Java interface for ADT Stack. Develop two different classes that implement this
interface, one using array and the other using linked-list. Provide necessary exception handling
in both the implementations.

ALGORITHM:
STEP 1: Create an interface which consists of three methods namely PUSH, POP and
DISPLAY
STEP 2: Create a class which implements the above interface to implement the concept
of stack through Array
STEP 3: Define all the methods of the interface to push any element, to pop the top
element and to display the elements present in the stack.
STEP 4: Create another class which implements the same interface to implement the
concept of stack through linked list.
STEP 5: Repeat STEP 4 for the above said class also.
STEP 6: In the main class, get the choice from the user to choose whether array
implementation or linked list implementation of the stack.
STEP 7: Call the methods appropriately according to the choices made by the user in the
previous step.
STEP 8: Repeat step 6 and step 7 until the user stops his/her execution

PROGRAM:
import java.util.*;
public class ListStack implements Stack
{
public ListStack()
{
topOfStack=null;
}
public boolean isEmpty()
{
return topOfStack==null;
}
public void push(Object x)
{
topOfStack=new ListNode(x,topOfStack);
}
public void pop()
{
if(isEmpty())
throw new UnderflowException("ListStack pop");
System.out.println(topOfStack.element+"is deleted");
topOfStack=topOfStack.next;
}
public void display()
{
DispNode=topOfStack;
while(DispNode!=null)

Concurrent Programming Lab Manual 7 CSE Department


{
System.out.println(DispNode.element+" ");
DispNode=DispNode.next;
}
}
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
ListStack theList=new ListStack();
int data=10;
int choice;
do
{
System.out.println();
System.out.println("-------------------------------------------------------------------");
System.out.println("STACK IMPLEMENTATION USING LINKED LIST");
System.out.println("-------------------------------------------------------------------");
System.out.println();
System.out.println("1.PUSH");
System.out.println("2.POP");
System.out.println("3.DISPLAY");
System.out.println("4.EXIT");
System.out.println("\n ENTER YOUR CHOICE:");
choice=in.nextInt();
switch(choice)
{
case 1:
System.out.println("\n enter the element to push:");
data=in.nextInt();
theList.push(data);
break;
case 2:
theList.pop();
break;
case 3:
System.out.println("the Stack elements are:");
theList.display();
break;
case 4:
break;
default:
System.out.println("wrong choice");
}
}
while(choice!=4);
}
private ListNode topOfStack;
private ListNode DispNode;
}
class UnderflowException extends RuntimeException
{

Concurrent Programming Lab Manual 8 CSE Department


public UnderflowException(String message)
{
super(message);
}
}
interface Stack
{
void push(Object x);
void pop();
void display();
boolean isEmpty();
}
class ListNode
{
public ListNode(Object theElement)
{
this(theElement,null);
}
public ListNode(Object theElement,ListNode n)
{
element=theElement;
next=n;
}
public Object element;
public ListNode next;
}

OUTPUT:

C:\jdk1.6.0_17\bin>javac ListStack.java
C:\jdk1.6.0_17\bin>java ListStack
------------------------------------------------------------------
STACK IMPLEMENTATION USING LINKED LIST
------------------------------------------------------------------
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR OPTION:1
Enter the element to push:100
-------------------------------------------------------------------
STACK IMPLEMENTATION USING LINKED LIST
-------------------------------------------------------------------
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR OPTION:3
100

Concurrent Programming Lab Manual 9 CSE Department


-------------------------------------------------------------------
STACK IMPLEMENTATION USING LINKED LIST
-------------------------------------------------------------------
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR OPTION:2
100is deleted
-------------------------------------------------------------------
STACK IMPLEMENTATION USING LINKED LIST
-------------------------------------------------------------------
1. PUSH
2. POP
3. DISPLAY
4. EXIT
ENTER YOUR OPTION:3
-------------------------------------------------------------------
STACK IMPLEMENTATION USING LINKED LIST
-------------------------------------------------------------------
1. PUSH
2. POP
3. DISPLAY
4. EXIT

RESULT:

Thus the program Implementation of java interface for ADT stack has been successfully
executed verified and successfully.

Concurrent Programming Lab Manual 10 CSE Department


EX.NO: 3 b. Implementation of Packages.

AIM:
To write a java program to implement the concept of Packages

ALGORITHM:
Step1: Start
Class A:
Step2: Create a package pack1 and define a class “A” inside that package
Step3:Define a method to find the area of the circle inside class “A”
Class Test:
Step4: import the package pack1 and its class
Step5: Define the main method
Step6: Create an object for the class A inside the package pack1
Step7: Call the method defined inside class A using the object
Sep8: Compile the program inside the package and then come out of the package and compile
the test class
Step 9: Execute the test class.
Step10: Stop.

PROGRAM:
Class A:

package Pack1;
public class A
{
public void display()
{
final double pie=3.14;
double r=5.0;
double area=0.0;
area=pie*r*r;
System.out.println("Area of the circle:"+area);
}
}
Class Test:
import pack1.A;
public class Test
{
public static void main(String args[])
{
A a=new A();
a.display();
}
}

Concurrent Programming Lab Manual 11 CSE Department


OUTPUT:

S:\04cs31\Folder\pack1\javac A.java
S:\04cs31\Folder\javac Test.java
S:\04cs31\Folder\java Test
Area of the circle is :78.5

RESULT:

Thus the program for creating package was executed and its output was verified.

Concurrent Programming Lab Manual 12 CSE Department


EX.NO: 4 Implementation of Multithreaded Programming.
AIM:
To write a java program to implement the concept of Multithreading.

ALGORITHM:
Step1: Start
Step2: Define a class that implements Runnable interface
Step3: Create a new thread and start the thread.
Step4: Use run and sleep methods
Step5: Use try/catch blocks to handle exceptions
Step6:Compile and execute the program
Step7:Stop

PROGRAM:
class Mul implements Runnable
{
String name;
Thread t;
Mul(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("new thread"+t);
t.start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println(name+":"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)

{
System.out.println(name+"Interrupted");
}
System.out.println(name+"Exiting");
}
}
class Multithread
{
public static void main(String args[])
{
new Mul("First thread");
new Mul("Second thread");
try

Concurrent Programming Lab Manual 13 CSE Department


{
Thread.sleep(10000);
}

catch(InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting");
}
}

OUTPUT:
S:\04cs70>javac Multithread.java
S:\04cs70>java Multithread
New thread Thread[First thread,5,main]

New thread Thread[Second thread,5,main]


First thread:5
Second thread:5
First thread:4
Second thread:4
First thread:3
Second thread:3
First thread:2
Second thread:2
First thread:1
Second thread:1
First thread Exiting
Second thread Exiting
Main thread Exiting

RESULT:

Thus the program to implement the concept of Multithreading was executed and its
output was verified

Concurrent Programming Lab Manual 14 CSE Department


EX.NO: 5 Develop a program to implement String Handling Methods.

AIM:
To write a java program to implement the concept of String manipulations

ALGORITHM:
Step1: Start
Step2: import all the necessary packages .
Step3: Declare a class “Manipulation”
Step4: Create a Buffer using BufferReader
Step5: Read the String ‘s1’,’s2’ from the user.
Step6: Find the length of the String using the function s1.length()
Step7:Concatenate another String with the existing one using the function s1.concat(s2)
Step8: Compare the two Strings using the function s1.equals(s2)
Step9: Change the case of the String using the functions s1.toLowerCase() and
s2.toUpperCase()
Step10: Append a new string with existing one using the functions s1.append(‘!’)
Step11: Compile and execute the program
Step12:Stop

PROGRAM:
import java.io.*;
class Manipulation
{
public static void main(String args[])throws Exception
{
String s1,s2;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string :");
s1=b.readLine();
System.out.println("Length of the string is :"+s1.length());
System.out.println("Enter a string to concatenate with your string :");
s2=b.readLine();
System.out.println("The Concatenated String is :"+s1.concat(s2));
System.out.println("Comparing java with JAVA");
String s3="JAVA";
String s4="java";
System.out.println(s3.equals(s4));
System.out.println(s3.equalsIgnoreCase(s4));
System.out.println("The character at the position 2 is :"+s4.charAt(2));
System.out.println("Converting s4 to lowercase:"+s3.toLowerCase());
StringBuffer c=new StringBuffer("dog");
System.out.println("When '!' is append to DOG,the result string is:"+c.append('!'));
System.out.println("Replacement Operation:"+"dog".replace('d','m'));
System.out.println(c.delete(0,2));
}
}

Concurrent Programming Lab Manual 15 CSE Department


OUTPUT:
S:\04cs31\javac Manipulation.java
S:\04cs31\java Manipulation
Enter a String
Sun
Length of the String is:3
Enter a string to concatenate with your String:
Microsoft
The Concatenated String is Sun Microsystem
Comparing java with JAVA
False
True
The character at position 2 is:v
Converting s4 to lowercase:java
When ! is appended to Dog the result string is: Dog!
Reverse of Dog is God
Replacement operation:mog
G!

RESULT:

Thus the program to implement the concept of String Manipulations was executed and its output
was verified.

Concurrent Programming Lab Manual 16 CSE Department


EX.NO: 6 Implementation of Collections Interfaces and Classes
AIM:
To write a java program to implement the concept of Interface

ALGORITHM:
Step1: Start
Class Student
Step2: Read and display the roll number of the student
Class Test
Step3: inherit the class Super class using extends keyword
Step4: Define the method that reads the marks from the user and displays it
Interface Sports:
Step5: Declare a final variable and a static method
Class Result:
Step6: Implement the multiple inheritance of java using implements keyword
Step7: Define the methods display and putscore
Class Multiple:
Step8: Define the main method
Step9: Create the object and call the methods
Step10: Stop

PROGRAM:
class Student
{
int rno;
void setrno(int x)
{
rno=x;
}
void putrno()
{
System.out.println("Roll number of the student:"+rno);
}
}
class Test extends Student
{
int mark1;
int mark2;
void setmarks(int x,int y)
{
mark1=x;
mark2=y;
}
void putmarks()
{
System.out.println("subject1 mark:"+mark1);
System.out.println("subject2 mark:"+mark2);
}
}
interface Sports

Concurrent Programming Lab Manual 17 CSE Department


{
final int mark3=80;
abstract void putscore();
}
class Result extends Test implements Sports
{
int total;
void display()
{
total=mark1+mark2+mark3;
putmarks();
putscore();
System.out.println("Total Scored by the student is:"+total);
}
public void putscore()
{
System.out.println("subject3 mark:"+mark3);
}
}
class Multiple
{
public static void main(String args[])
{
Result r=new Result();
r.setrno(10);
r.setmarks(70,80);
r.display();
}
}

OUTPUT:
S:\04cs70>javac Multiple.java
S:\04cs70>java Multiple
Subject1 mark:70
Subject2 mark:80
Subject3 mark:85
Total Scored by the student is: 235

RESULT:

Thus the program to implement the concept of Interface was executed and its output was verified

Concurrent Programming Lab Manual 18 CSE Department


EX.NO: 7 Implementation of Exception handling mechanisms

AIM:
To write a sample java program that implements exception handling techniques and
concepts.
ALGORITHM:
1. Start.
2. Create a base class that extends Exception.
3. Create another class called “vehicle” with all the necessary variables and input streams.
4. Accept age from the user.
5. Check for the condition,
If age > 18 and < 50 Display “License Allowed”
Else Raise an exception called “myexp”
6. For invalid raised exception, display “License not allowed”.
7. For Number Format Exception, display “Invalid Input”.
8. Stop the program execution.

PROGRAM:
import java.lang.Exception;
import java.io.*;
class myex extends Exception
{
myex(String message)
{
super(message);
}
}
class vehicle
{
public static void main(String args[])throws IOException
{
int age=0;
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.println("Enter the age");
age=Integer.parseInt(in.readLine());

if((age>18) && (age<50))


System.out.println("Licence Allowed...");
else
throw new myex("Not Permitted!!!");
}
catch(NumberFormatException e)
{
System.out.println("Invalid Input!!!");
}
catch(myex e)

Concurrent Programming Lab Manual 19 CSE Department


{
System.out.println("Age limit Exception!!!");
System.out.println(e.getMessage());
}
}
}

OUTPUT:

D:\java\bin>javac vehicle.java
Note: vehicle.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

D:\java\bin>java vehicle
Enter the age
20
Licence Allowed...

D:\java\bin>java vehicle
Enter the age
12
Age limit Exception!!!
Not Permitted!!!

RESULT:

Thus the program for exception handling is executed and its output was verified.

Concurrent Programming Lab Manual 20 CSE Department


EX.NO: 8 Write a program to perform I/O Streams.
AIM:
To write a java program to perform I/O Streams.
ALGORITHM:
1. Start.
2. Read the files using FileInputStream with given file name.
3. Write the files using FileOutputStream to corresponding user specified file name.
4. Store the file content to specified file name.
5. Stop.

PROGRAM:
import java.io.*;
public class CopyFile {
public static void main(String args[]) throws IOException
{
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}

OUTPUT:

javac CopyFile.java
java CopyFile

RESULT:

Thus the program to perform I/O streams was executed and its output was verified.

Concurrent Programming Lab Manual 21 CSE Department


EX.NO: 9 Implementation of Applet Programs.

AIM:
To write a java program to implement the concept of applets.

ALGORITHM:
Step1: Start
Step2: import all the necessary packages like applet,awt etc
Step3: Declare a class that extends Applet
Step4: Initialize the applet using init method.
Step5: Define a paint method
Step6:Create a HTML file displaying the applet window
Step7:Compile the applet file
Step8:Execute the program using appletviewer.
Step9:Stop

PROGRAM:
//Design an Applet program to perform Arithmetic Calculations

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="Calculation" width=565 height=160>
</applet>*/
public class Calculation extends Applet implements ActionListener {
TextField t1,t2;
Label l1,l2,l3,l4;
Button add,sub,mul,div,mod;
double res;
int res1;
public void init() {
setFont(new Font("Arial",Font.BOLD,20));
l1=new Label("Enter 1st Operand: ");
l2=new Label("Enter 2nd Operand: ");
t1=new TextField(25);
t2=new TextField(25);
l3=new Label("Result: ");
l4=new Label(" ");
add=new Button("Addition");
sub=new Button("Subtraction");
mul=new Button("Multiplication");
div=new Button("Division");
mod=new Button("Modulus");
add(l1);
add(t1);
add(l2);
add(t2);
add(add);
add(sub);
add(mul);

Concurrent Programming Lab Manual 22 CSE Department


add(div);
add(mod);
add(l3);
add(l4);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
String x=ae.getActionCommand();
if(x.equals("Addition")) {
res=(Double.parseDouble(t1.getText()))+(Double.parseDouble(t2.getText()));
l4.setText(""+res);
}
if(x.equals("Subtraction")) {
res=(Double.parseDouble(t1.getText()))-
(Double.parseDouble(t2.getText()));
l4.setText(""+res);
}
if(x.equals("Multiplication")) {
res=(Double.parseDouble(t1.getText()))*(Double.parseDouble(t2.getText()));
l4.setText(""+res);
}
if(x.equals("Division")) {
res=(Double.parseDouble(t1.getText()))/(Double.parseDouble(t2.getText()));
l4.setText(""+res);
}
if(x.equals("Modulus")) {
res1=(Integer.parseInt(t1.getText()))%(Integer.parseInt(t2.getText()));
l4.setText(""+res1);
}
}
}

Concurrent Programming Lab Manual 23 CSE Department


OUTPUT:

Z:\cs2k944> javac Calculation.java

Z:\cs2k944> AppletViewer Calculation.java

RESULT:
Thus the program for creating a applet window was executed and its output was verified.

Concurrent Programming Lab Manual 24 CSE Department


EX.NO: 10 Implementation of AWT Controls.
AIM:
To write a java program to implement the AWT Controls.

ALGORITHM:
Step1: Start.
Step2: import all the necessary packages like applet,awt etc
Step3: Declare a class that extends Applet
Step4: Initialize the applet using init method.
Step5: Define a paint method
Step6:Create a HTML file displaying the applet window
Step7:Compile the applet file
Step8:Execute the program using appletviewer.
Step9:Stop.

PROGRAM:
// Design an Applet program to perform Mouse Event using AWT controls.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="MouseEventDemo" width=400 height=300>
</applet>*/
public class MouseEventDemo extends Applet implements
MouseListener,MouseMotionListener {
String msg="";
public void init() {
setFont(new Font("Arial",Font.BOLD,18));
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent e) {
msg="Mouse Clicked";
repaint();
}
public void mouseEntered(MouseEvent e) {
msg="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent e) {
msg="Mouse Exited";
repaint();
}
public void mousePressed(MouseEvent e) {
msg="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent e) {
msg="Mouse Released";
repaint();

Concurrent Programming Lab Manual 25 CSE Department


}
public void mouseDragged(MouseEvent e) {
msg="Mouse Dragged";
repaint();
}
public void mouseMoved(MouseEvent e) {
msg="Mouse Moved";
repaint();
}
public void paint(Graphics g) {
g.drawString(msg,120,150);
}
}

OUTPUT:
Z:\cs2k944> javac MouseEventDemo.java
Z:\cs2k944> AppletViewer MouseEventDemo.java

RESULT:
Thus the program for creating a applet window was executed and its output was verified.

Concurrent Programming Lab Manual 26 CSE Department


EX.NO: 11 Implementation of I/O files.

AIM:
To write a java program to implement copy the contents of one file to another file.

ALGORITHM:
Step1: create a file.
Step2: Write contents to it.
Step3: Create another file.
Step4 copy the contents of previous file.
Step5: stop.

PROGRAM:
import java.io.*;
class filecopy
{
public static void main(String args[])throws Exception
{
System.out.println("FILE COPYING");
BufferedReader b=new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter the File Name");
String fn;
String str;
int av,m;
fn=b.readLine();
File f=new File(fn);
if(f.isFile())
{
System.out.println("This is file");
InputStream fr=new FileInputStream(fn);
System.out.println("Total available Bytes of File");
System.out.println(av=fr.available());
System.out.println("Enter the no of byte to copied");
str=b.readLine();
m=Integer.parseInt(str);
char ch[]=new char[m];
for(int i=0;i<m;i++)
{
ch[i]=(char)fr.read();
}
String str1=new String(ch);
fr.close();
String f2n="priya.java";
File f2=new File(f2n);
OutputStream fc=new FileOutputStream(f2n)
byte by[]=str1.getBytes();
for(int k=0;k<by.length;k++)
{
fc.write(by[k]);
}

Concurrent Programming Lab Manual 27 CSE Department


System.out.println("The Content of File "+ fn +" is Copy into "+f2n);
fc.close();
InputStream fc2=new FileInputStream(f2n);
System.out.println("Total available Bytes of File");
System.out.println(av=fc2.available());
char chs[]=new char[av];
for(int i=0;i<av;i++)
{
chs[i]=(char)fc2.read();
}
String str2=new String(chs);
System.out.println(str2);
fr.close();}
else
{
System.out.println("this is directory");
System.out.println("The Content of File");
String sl[]=f.list();
for(int i=0;i<sl.length;i++)}
System.out.println(sl[i]);}}
System.out.println("Enter the extention of file");
String ex;
ex=b.readLine();
System.out.println("The "+ex+ "File are:\n");
for(int j=0;j<sl.length;j++){
if(sl[j].endsWith(ex)){
System.out.println(sl[j]);}}}}

RESULT:

Thus the program for file handling was implemented and output was verified.

Concurrent Programming Lab Manual 28 CSE Department


EX.NO: 12 Write a program to implement Event Classes
AIM:
To create an applet application using the Key Event class and KeyListener interface.

ALGORITHM:
1. Start.
2. Declare all the required variables along with the header files.
3. An applet code using html codes are written in the program.
4. We create a class that extends Applet and also implements KeyListener interface.
5. We create a textfield and a textarea where the text is entered.
6. The textarea is used to display the various actions done when a key is pressed.
7. The keyPressed() function is displayed, whenever a key is pressed.
8. We display the typed char when the KeyTyped() method is called.
9. The KeyReleased() gets triggered, when we release the typed key.
10. End of the program execution.

PROGRAM:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="impkey" width=200 height=300>
</applet>*/
public class impkey extends Applet implements KeyListener
{
TextField t;
TextArea ta;
public void init()
{
t=new TextField(20);
ta=new TextArea();
add(t);
add(ta);
t.addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
if(ke.getSource()==t)
ta.append("key Pressed");
}
public void keyReleased(KeyEvent ke)
{
if(ke.getSource()==t)
ta.append("Key Released");
}
public void keyTyped(KeyEvent ke)
{
if(ke.getSource()==t)

Concurrent Programming Lab Manual 29 CSE Department


{
char c=ke.getKeyChar();
ta.append("Key Typed"+c);
}
}
}

OUTPUT:
D:\java\bin>javac impkey.java

D:\java\bin>appletViewer impkey.java

RESULT:
Thus the program for Key Event class and KeyListener interface was implemented and output
was verified.

Concurrent Programming Lab Manual 30 CSE Department


EX.NO: 13 Implementation of JDBC concepts.
AIM:
To implement database connectivity with MS-Access using java.

ALGORITHM :
STEP1: Start the process.
STEP2: Open an Ms-Access window.
STEP3: Create a table containing five fields, name,rollno,mark1,mark2,total and save the table
with .mdp extension.
STEP4: Write the java code to implement the connectivity.
STEP5: Compile the program using javac compiler.
STEP6: Execute the java program.
STEP7: Calculate the total and display the result.
STEP8: Stop.

PROGRAM:
import java.sql.*;
import sun.jdbc.odbc.*;
class exjdbc
{
String name;
String rollno,m1,m2;
void listDetails() throws SQLException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
};
Connection con=DriverManager.getConnection("jdbc:odbc:DSN1");
Statement st=con.createStatement();
String str="INSERT INTO STUD VALUES('"+name+"','"+rollno+"',"+m1+","+m2+");";
ResultSet rs=st.executeQuery("SELECT * FROM STUD");
System.out.println(" NAME ROLLNO M1 M2 ");
while(rs.next())
{
name=rs.getString(1);
rollno=rs.getString(2);
m1=rs.getString(3);
m2=rs.getString(4);
System.out.println(name+" "+rollno+" "+m1+" "+m2);
}
}
public static void main(String a[])
{
exjdbc e1=new exjdbc();
try

Concurrent Programming Lab Manual 31 CSE Department


{
e1.listDetails();
}
catch(SQLException e2)
{
System.out.println(e2);
}
}
}

OUTPUT:
NAME ROLLNO M1 M2
A 1 45 67
B 2 66 90

RESULT:

Thus the program for database is connected using JDBC was executed and output are verified.

Concurrent Programming Lab Manual 32 CSE Department

You might also like