SRI CHANDRASEKHARENDRA SARASWATHI
VISWA MAHAVIDYALAYA
(UNIVERSITY ESTABLISHED UNDER SECTION 3 OF UGC ACT 1956)
ENATHUR, KANCHIPURAM – 631 561
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
JAVA PROGRAMMING LABORATORY
Name :
Reg. No :
Class : III- B.E (CSE)
Subject : BCSF185P70 –Java Programming Lab
SRI CHANDRASEKHARENDRA SARASWATHI
VISWAMAHAVIDYALAYA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
BONAFIDE CERTIFICATE
This is to certify that this is the bonafide record of work done by
Mr/Ms. ,
with Reg. No of III-B.E.(CSE) in the Java Programming
Lab (BCSF185P70) during the academic year 2023 – 24.
Station:
Date:
Staff-in-charge Head of the Department
Submitted for the Practical examination held on .
Examiner-1 Examiner-2
INDEX
Page Staff
[Link] Date Title No. Sign
Page Staff
[Link] Date Title No. Initials
Exp no : 1.a
Sort in Ascending Order
Date :
Aim :
Algorithm:
Source Code :
import [Link];
public class AscendingOrder
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner([Link]);
[Link]("Enter no. of elements you want in array:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = [Link]();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
[Link]("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
[Link](a[i] + ",");
}
[Link](a[n - 1]);
}
}
Output :
Enter no. of elements you want in array: 10
Enter all the elements:
2534860179
Ascending Order:0,1,2,3,4,5,6,7,8,9
Result :
Exp no : 1.b
Binary Search
Date :
Aim :
Algorithm:
Source Code :
import [Link];
class BinarySearch
{
public static void main(String ar[])
{
int i,mid,first,last,x,n,flag=0;
Scanner sc=new Scanner([Link]);
[Link]("Enter number of elements:");
n=[Link]();
int a[]=new int[n];
[Link]("Enter elements of array:");
for(i=0;i<n;++i)
a[i]=[Link]();
[Link]("Enter element to search:");
x=[Link]();
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2
;if(a[mid]>x)
last=mid-1;
else
if(a[mid]<x)
first=mid+1
;else
{
flag=1;
[Link]("element found");
break;
}
}
if(flag==0)
[Link]("element not found");
}
}
Output :
Enter number of elements:
5
Enter elements of array:
2 1 4 5 99
Enter element to search:
4
element found
Result :
Exp no : 2.a
Arithmetic Operations using
Date :
Switch Case
Aim:
Algorithm:
Source Code:
import [Link];
public class ArithmeticOperators
{
public static void main(String args[])
{
Scanner s = new
Scanner([Link]);while(true)
{
[Link]("");
[Link]("Enter the two numbers to perform operations ");
[Link]("Enter the first number : ");
int x = [Link]();
[Link]("Enter the second number : ");
int y = [Link]();
[Link]("Choose the operation you want to perform ");
[Link]("Choose 1 for ADDITION");
[Link]("Choose 2 for SUBTRACTION");
[Link]("Choose 3 for MULTIPLICATION");
[Link]("Choose 4 for DIVISION");
[Link]("Choose 5 for MODULUS");
[Link]("Choose 6 for EXIT");
int n = [Link]();
switch(n)
{
case 1:
int add;
add = x +
y;
[Link]("Result : "+add);
break;
case 2:
int sub;
sub = x -
y;
[Link]("Result : "+sub);
break;
case 3:
int mul;
mul = x *
y;
[Link]("Result : "+mul);break;
case 4:
float div;
div = (float) x / y;
[Link]("Result : "+div);break;
case 5:
int mod;
mod = x %
y;
[Link]("Result : "+mod);break;
case 6:
[Link](0);
}
}
}
Output :
Enter the two numbers to perform operations
Enter the first number : 78
Enter the second number : 133
Choose the operation you want to perform
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for
MULTIPLICATIONChoose 4
for DIVISION
Choose 5 for
MODULUSChoose 6
for EXIT
1
Result : 211
Result :
Exp no : 2.b
Even or Odd using Switch Case
Date :
Aim :
Algorithm:
Source Code:
import [Link].*;
class
EvenOddSwitch
{
public static void main(String args[])
{
int n,i;
Scanner s = new
Scanner([Link]);n = [Link]();
switch(n%2)
{
case 0 :
[Link]("This number is even");break;
case 1 :
[Link]("This number is odd");break;
}
}
}
Output :
5
This number is odd
Result :
Exp no : 3.a
Armstrong number
Date :
Aim :
Algorithm:
Source Code :
import [Link];
public class Armstrong
{
public static void main(String args[])
{
int n, nu, num=0, rem;
Scanner scan = new Scanner([Link]);
[Link]("Enter any Positive Number : ");
n = [Link]();
nu = n;
while(nu != 0)
{
rem = nu%10;
num = num + rem*rem*rem;
nu = nu/10;
}
if(num == n)
{
[Link]("Armstrong Number");
}
else
{
[Link]("Not an Armstrong Number");
}
}
}
Output :
Enter any Positive Number : 153
Armstrong Number
Result :
Exp no : 3.b
Fibonacci Series
Date :
Aim :
Algorithm:
Source Code :
import [Link];
public class FibonacciSeries
public static void main(String[] args)
{
Scanner s = new Scanner([Link]);
[Link]("Enter the value of n: ");
int n = [Link]();
fibonacci(n);
}
public static void fibonacci(int n)
{
if (n == 0)
{
[Link]("0");
}
else if (n == 1)
{
[Link]("0 1");
}
else
{
[Link]("0 1 ");
int a = 0;
int b = 1;
for (int i = 1; i < n; i++)
{
int nextNumber = a + b;
[Link](nextNumber + " ");
a = b;
b = nextNumber;
}
}
}
}
Output :
Enter the value of n: 5
011235
Result :
Exp no : 4.a
Substring of a String
Date :
Aim :
Algorithm:
Source Code :
import [Link];
class
SubstringsOfAString
{
public static void main(String args[])
{
String string, sub;
int i, n = 0, c,
length;
Scanner in = new Scanner([Link]);
[Link]("Enter a string to print it's all substrings");
string = [Link]();
length = [Link]();
[Link]("Substrings of \""+string+"\" are:");
for (c = 0; c < length; c++)
{
for(i = 1; i <= length - c; i++)
{
sub = [Link](c,
c+i);[Link](sub);
}
n = n + i;
}
[Link]("No of substrings present are: "+n);
}
}
Output :
java
Substrings of "java" are:
j
ja
jav
java
a
av
ava
v
va
a
No. of substrings present are: 14
Result :
Exp no : 4.b
Arranging String in Ascending
Date : Order
Aim :
Algorithm:
Source Code :
import [Link].*;
public class
AlphaOrder
{
public static void main(String args[])
{
int count;
String temp;
Scanner scan=new Scanner ([Link]);
[Link]("Enter total number of string to be entered: ");
count=[Link]();
String str[] = new String [count];
Scanner scan2 = new Scanner([Link]);
[Link]("Enter the string one by one:");
for (int i = 0;i<count;i++)
{
str[i] = [Link]();
}
[Link]();
[Link]();
for(int i = 0;i<count;i++)
{
for(int j=i+1;j<count;j++)
{
if(str[i].compareTo(str[j])>0)
{
temp=str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
[Link]("Strings in sorted order:");
for(int i = 0;i<=count-1;i++)
{
[Link](str[i]);
}
}
}
Output :
Enter total number of string to be entered:2
Enter the string one by one:
python
java
Strings in sorted order:
java
python
Result :
Exp no : 5.a
Pre-defined Exception Handling
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
import [Link].*;
class Predefined
{
public static void main(String args[])
{
int num1,num2;
try
{
num1=0;
num2=62/num1;
[Link]("try block msg");
}
catch(ArithmeticException e)
{
[Link]("error divided by zero");
}
[Link]("out of catch block");
}
}
Output :
error divided by zero
out of catch block
Result :
Exp no : 5.b User-defined Exception
Handling
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
import [Link].*;
class MyException extends Exception
{
String str1;
MyException(String str2)
{
str1=str2;
}
public String toString()
{
return("output String="+str1);
}
}
class Userdefined
{
public static void main(String[]args)
{
try
{
throw new MyException("custom");
}
catch(MyException exp)
{
[Link]("this is catch block");
[Link]("exp");
}
}
}
Output :
this is catch block
exp
Result :
Exp no : 6.a Account details using inheritance
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
class Account
{
int accountNumber, balance;
Account(int accountNumber, int balance)
{
[Link] = accountNumber;
[Link] = balance;
}
void credit(int amount)
{
balance = balance + amount;
}
void debit(int amount)
{
balance = balance - amount;
}
void displayBalance()
{
[Link]("Balance = "+balance);
}
}
class SBAccount extends Account
{
double rate;
SBAccount(int accountNumber, int balance, double rate)
{
super(accountNumber, balance);
[Link] = rate;
}
void displayInterestRate()
{
[Link]("Interest Rate = " +rate);
}
}
class AccountDemo
{
public static void main(String args[])
{
SBAccount obj1;
obj1 = new SBAccount(1001, 5000,
6.5);[Link](2000);
[Link](1000);
[Link]();
[Link]();
}
}
Output :
Balance = 6000
Interest Rate = 6
Result :
Exp no : 6.b College Details using inheritance
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
class College
{
String collegeName,principalName;
College(String collegeName,String principalName)
{
[Link] = collegeName;
[Link] = principalName;
}
void displayCollegeDetails()
{
[Link]("CollegeName:"+collegeName);
[Link]("PrincipalName:"+principalName)
;
}
}
class Department extends College
{
String departmentName,HODName;
Department(String collegeName,String principalName,String departmentName,String
HODName)
{
super(collegeName,principalName);
[Link]=departmentName;
[Link]=HODName;
}
void displayDepartmentDetails()
{
[Link]("DepartmentName:"+departmentName);
[Link]("HODName:"+HODName);
}
}
class SingleInherit
{
public static void main(String[]args)
{
Department obj;
obj=new Department("NIT","[Link]","Electronics","[Link]");
[Link]();
[Link]();
}
}
Output :
CollegeName:NIT
PrincipalName:[Link]
DepartmentName:Electronics
HODName:[Link]
Result :
Exp no : 7.a
Student Details Using Interface
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
interface Exam
{
void percent_cal();
}
class Student
{
String name;
int roll_no,mark1,mark2;
Student(String n, int r, int m1, int m2)
{
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
}
void display()
{
[Link] ("Name of the Student: "+name);
[Link] ("Roll No. of Student:
"+roll_no);[Link] ("Marks of Subject 1:
"+mark1); [Link] ("Marks of Subject 2:
"+mark2);
}
}
class Result extends Student implements Exam
{
Result(String n, int r, int m1, int m2)
{
super(n,r,m1,m2);
}
public void percent_cal() {int
total=(mark1+mark2);float percent=total*100/200;
[Link] ("Percentage: "+percent+"%");
void display()
{
[Link]();
}
}
public class StuDetail
{
public static void main(String[] args)
{
Result R = new Result("Ragini",12,93,84);
[Link]();R.percent_cal();
}
Output :
Name of the Student:
RaginiRoll No. of
Student: 12 Marks of
Subject 1: 93
Marks of Subject 2: 84
Percentage: 88.0%
Result :
Exp no : 7.b Arithmetic Operations using
Packages
Date :
Aim :
Algorithm:
Source Code :
package add;
public class Add
{
int res;
public void addop(int a, int b)
{
res = a + b;
[Link]("Add :"+res);
}
}
package sub;
public class Sub
{
int res;
public void subop(int a,int b)
{
res = a - b;
[Link]("Sub:"+res);
}
}
package mul;
public class Mul
{
int res;
public void mulop(int a,int b)
{
res = a * b;
[Link]("Mul :"+res);
}
}
package div;
public class Div
{
int res;
public void divop(int a,int b)
{
res = a / b;
[Link]("Div :"+res);
}
}
import [Link].*;
import add.*;
import sub.*;
import mul.*;
import div.*;
public class ArithDemo
{
public static void main(String args[])
{
Add ad = new Add();
Sub su = new Sub();
Mul mu = new
Mul();Div di = new
Div();
[Link](20,10);
[Link](20,10);
[Link](20,10);
[Link](20,10);
}
}
Output :
Add :30
Sub:10
Mul :200
Div :2
Result :
Exp no : 8.a
Creating Simple Thread
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==1
)
yield();
[Link]("from thread A i="+i);
}
[Link]("exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
[Link]("from thread B
j="+j);if(j==3)
[Link]("exit from 5");
stop();
}
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
[Link]("thread c ="+k);
if(k==1)
try
{
sleep(1500);
}
catch(Exception c)
{
[Link]("exit from c");
}
}
}
}
class Threadtest
{
public static void main(String[]args)
{
A a = new
A(); B b =
new B();C c =
new C();
[Link]("Start thread A");
[Link]();
[Link]();
[Link]();
[Link]("exit from main thread");
}
}
Output :
Start thread A
exit from main thread
Thread A started
Thread B started
from thread A i=1
thread C started
from thread A
i=2 from thread B
j=1 from thread A
i=3thread c =1
from thread A
i=4from thread B
j=2exit from A
thread c =2
from thread B j=3
thread c =3
thread c =4
from thread B j=4
exit from c
exit from B
Result :
Exp no : 8.b Thread Priority
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
class A extends Thread
{
public void run()
{
[Link]("Thread A started");
for(int i=1;i<=4;i++)
{
[Link]("from thread A i="+i);
}
[Link]("exit from A");
}
}
class B extends Thread
{
public void run()
{
[Link]("Thread B started");
for(int j=1;j<=4;j++)
{
[Link]("from thread B j="+j);
}
[Link]("exit from B");
}
}
class C extends Thread
{
public void run()
{
[Link]("thread C started");
for(int k=1;k<=4;k++)
{
[Link]("thread c ="+k);
}
[Link]("exit from c");
}
}
class ThreadPriority
{
public static void main(String[]args)
{
AthreadA = newA();
BthreadB = new B();
CthreadC = new C();
[Link](Thread.MAX_PRIORIT
Y);
[Link]([Link]()+1);
[Link](Thread.MIN_PRIORITY
);[Link]("start thread A");
[Link]();
[Link]("start thread B");
[Link]();
[Link]("start thread C");
[Link]();
[Link]("end of main thread");
}
}
Output :
start thread A
start thread B
Thread A started
start thread C
from thread A
i=1Thread B
started thread C
started from
thread A i=2
end of main thread
from thread A i=3
thread c =1
from thread B j=1
thread c =2
thread c =3
thread c =4
from thread A i=4
exit from c
from thread B j=2
exit from A
from thread B j=3
from thread B j=4
exit from B
Result :
Exp no : 9.a
File Writer
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
class Filewriter
{
public static void main(String[]args)
{
try
{
FileWriter fw= new
FileWriter("[Link]");for(char
i=65;i<91;i++)
{
[Link](i);
}
[Link]();
}
catch(Exception e)
{
[Link]("Exception :"+e);
}
}
}
Output :
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Result :
Exp no : 9.b
File Reader
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
class Filereader
{
public static void main(String[]args)
{
try
{
FileReader fr=new
FileReader("[Link]");int i;
while((i=[Link]())!=-1)
{
[Link]((char)i);
}
[Link]();
}
catch(Exception e)
{
[Link]("Exception:"+e);
}
}
}
Output :
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Result :
Exp no : 10.a
Awaking a Human Face using
Date : Applets
Aim :
Algorithm:
Source Code :
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="[Link]" height=300 width=300>
</applet>*/
public class face extends Applet
{
public void paint(Graphics g)
{
[Link](40,40,120,150);
[Link](57,75,30,20);
[Link](110,75,30,20);
[Link](68,81,10,10);
[Link](121,81,10,10);
[Link](85,100,30,30);
[Link](60,125,80,40,180,180)
;
[Link](25,92,15,30);
[Link](160,92,15,30)
;
}
}
Output :
Result :
Exp no : 10.b
Drawing Line and Rectangle
Date : using Applets
Aim :
Algorithm:
Source Code :
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="[Link]" height=300 width=300>
</applet>*/
public class linerect extends Applet
{
public void paint(Graphics g)
{
[Link](10,10,50,50);
[Link](10,60,40,30);
[Link](60,10,30,80);
[Link](10,100,80,50,10,10);
[Link](20,110,60,30,5,5);
[Link](100,10,230,140);
[Link](100,140,230,10);
}
}
Output :
Result :
Exp no : 11.a
Grid Layout
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="[Link]" height=300 width=300>
</applet>*/
public class gld extends Applet
{
public void init()
{
setLayout(new
GridLayout(5,2));Button
b1=new Button("A"); Button
b2=new Button("B"); Button
b3=new Button("C"); Button
b4=new Button("D"); Button
b5=new Button("E"); Button
b6=new Button("F"); Button
b7=new Button("G"); Button
b8=new Button("H"); Button
b9=new Button("I"); Button
b10=new Button("J"); add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
}
public Insets getInsets()
{
return new Insets(20,20,20,20);
}
}
Output :
Result :
Exp no :
11.b Card Layout
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code="carddemo" width=250 height=150>
</applet>
*/
public class carddemo extends Applet implements ActionListener
{
Button b1,b2,b3,b4;
Panel buttonPanel;
CardLayout
buttonCardLayout;public void
init()
{
buttonPanel=new Panel();
add(buttonPanel);
buttonCardLayout=new CardLayout();
[Link](buttonCardLayout);
b1=new Button("first Button");
[Link](this);
[Link](b1,"first Button");
b2=new Button("second Button");
[Link](this);
[Link](b2,"second Button");
b3=new Button("third button");
[Link](this);
[Link](b3,"third Button");
}
public void actionPerformed(ActionEvent e)
{
[Link](buttonPanel);
}
}
Output :
Result :
Exp no : 11.c
Border Layout
Date :
Aim :
Algorithm:
Source Code :
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code="insertdemo" width=400 height=200>
</applet>
*/
public class insertdemo extends Applet
{
public void init()
{
setBackground([Link]);
setLayout(new BorderLayout());
add(new Button("this is cross the top"),[Link]);
add(new Button("the footer messsage migth go here"),[Link]);
add(new Button("RIGHT"),[Link]);
add(new Button("LEFT"),[Link]);
String msg="the reasonable man adapts\n\n";
add(new TextArea(msg),[Link]);
}
public Insets getInsets()
{
return new Insets(10,10,10,10);
}
}
Output :
Result :
Exp no : 12
Student Name Using Database
Date : Implementation
Aim :
Algorithm:
Source Code :
import [Link].*;
public class
JDBCtest
{
public static void main(String args[])
{
try
{
[Link]("[Link]").newInstance();
Connection con = [Link]("jdbc:mysql://localhost:3306/test",
"test", "tiger");
Statement st = [Link]();
String sql = ("SELECT * FROM student ORDER BY Srno
ASC;");ResultSet rs = [Link](sql);
while([Link]())
{
int id = [Link]("Srno");
String str1 =
[Link]("Sname");
[Link](id+" "+str1);
}
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
Output :
1 Ramesh
2 Suresh
3 Rama
4 Arun
Result :