Java Lab Mannual
Java Lab Mannual
Java Lab Mannual
SATHYAMANGALAM
11Z408
CONCURRENT PROGRAMMING
LABORATORY
LAB MANUAL
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
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.
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");
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.
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)
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
RESULT:
Thus the program Implementation of java interface for ADT stack has been successfully
executed verified and successfully.
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();
}
}
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.
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
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]
RESULT:
Thus the program to implement the concept of Multithreading was executed and its
output was verified
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));
}
}
RESULT:
Thus the program to implement the concept of String Manipulations was executed and its output
was verified.
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
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
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());
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.
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.
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);
RESULT:
Thus the program for creating a applet window was executed and its output was verified.
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();
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.
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]);
}
RESULT:
Thus the program for file handling was implemented and output was verified.
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)
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.
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
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.