JAVA Lab Manual
LABPRG 1: Write a program check whether two strings are equal or not.
import [Link]. *;
class strequal
{
public static void main(String arg[]) throws IOException
{
[Link]("Enter the First String:");
DataInputStream ds1 = new DataInputStream([Link]);
String s1 = [Link]();
[Link]("First String entered is :" +s1);
[Link]("Enter the Second String :");
String s2 = [Link]();
[Link]("Second String entered is :" +s2);
if([Link](s2))
[Link]("Both the Strings are Equal");
else
[Link]("Strings are not equal");
}
}
OUTPUT:
1. Enter the First String:
Summit
First String entered is :Summit
Enter the Second String :
SUMMIT
Second String entered is :SUMMIT
Both the Strings are Equal
2. Enter the First String:
Summit
First String entered is :Summit
Enter the Second String :
Success
Second String entered is :Success
Strings are not equal
Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -1-
JAVA Lab Manual
LABPRG 2: Write a program to display the reverse of string.
import [Link].*;
class strreverse
{
public static void main(String arg[]) throws IOException
{
[Link]("Enter a string:");
DataInputStream ds1=new DataInputStream([Link]);
String s1=[Link]();
[Link]("String entered is:" +s1);
StringBuffer sb=new StringBuffer(s1);
[Link]("reversed String is :" +[Link]());
}
}
OUTPUT:
1. Enter a string: [Link] a string:
SIDDAGANGA MALAYALAM
String entered is:SIDDAGANGA String entered is:MALAYALAM
reversed String is :AGNAGADDIS reversed String is :MALAYALAM
LABPRG 3: Write a program check to find the SUM OF A DIGITS of a given
number
import [Link].*;
class Sumofdigits
{
public static void main(String arg[]) throws IOException
{
int num,rem,sum=0;
DataInputStream ds =new DataInputStream([Link]);
[Link]("Enter a Number :");
num=[Link]([Link]());
while(num>0)
{ OUTPUT
rem=num%10; Enter a Number :
sum=sum+rem; 4568
num=num/10; Sum of digits of the given number is : 23
}
[Link]("Sum of digits of the given number is : " +sum);
}
}
Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -2-
JAVA Lab Manual
LABPRG 4: Write a program to display the MULTIPLICATION TABLE
import [Link].*;
class Multipletab
{
public static void main(String a[]) throws IOException
{
int i,prod, num,limit; OUTPUT:
[Link]("Enter the number :"); Enter the number : 8
Enter the Limit : 10
DataInputStream ds = new DataInputStream ([Link]);
Multiplication Table
num=[Link]([Link]()); 8 * 1=8
[Link]("Enter the Limit :"); 8 * 2=16
limit=[Link]([Link]()); 8 * 3=24
[Link]("Multiplication Table"); 8 * 4=32
8 * 5=40
for(i=1;i<=limit;i++)
8 * 6=48
{ 8 * 7=56
prod=num*i; 8 * 8=64
[Link] (num+ " * " +i+ "=" +prod); 8 * 9=72
} 8 * 10=80
}
}
LABPRG 5: Write a program to display all the PRIME numbers between the
range.
import [Link].*;
class Prime
{
public static void main (String arg[]) throws IOException
{
int a,l,u;
DataInputStream ds=new DataInputStream([Link]);
[Link]("Enter the lower limit : ");
l=[Link] ([Link]());
[Link]("Enter the upper limit : ");
u=[Link]([Link]());
[Link]("Prime Numbers are : ");
for( a=l; a<=u;a++)
{
int i=2;
int flag=1;
while(i<a-1)
{
Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -3-
JAVA Lab Manual
if(a%i==0)
{
flag=0;
break; OUTPUT:
} 1. Enter the lower limit : 5
Enter the upper limit : 20
else
Prime Numbers are : 5 7 11 13 17 19
i++;
} 2. Enter the lower limit : 2
if(flag==1) Enter the upper limit : 35
[Link](a ); Prime Numbers are : 2 3 5 7 11 13 17 19 23 29 31
}
}
}
LABPRG 6: Write a program to SORT an existing elements in an array
import [Link].*;
class Elesort
{
public static void main(String aeg[]) throws IOException
{
int[] a =new int[20];
int i,j,n,temp;
DataInputStream ds = new DataInputStream ([Link]);
[Link]("Enter the number of elements in the array : ");
n=[Link]([Link]());
[Link]("Enter the elements in the array : ");
for(i=0;i<n;i++)
a[i]= [Link]([Link]());
for(i=0;i<n;i++) OUTPUT
{ 1. Enter the number of elements in the array : 5
for(j=0;j<n-1;j++) Enter the elements in the array :
{ 45 69 85 2 3
if(a[j]>a[j+1]) The array after sorting is
{ 2 3 45 69 85
temp=a[j];
2. Enter the number of elements in the array : 6
a[j]=a[j+1]; Enter the elements in the array :
a[j+1]=temp; 45 87 52 1 65 8
} The array after sorting is
} 1 8 45 52 65 87
}
[Link]("The array after sorting is ");
for(i=0;i<n;i++)
[Link](a[i]);
}
Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -4-
JAVA Lab Manual
}
LABPRG 7: Write a program to create an object of a STACK and use all the
methods.
import [Link].*;
import [Link].*;
class Obstack
{
public static void main(String arg[]) throws IOException
{
Stack st= new Stack();
int ch;
InputStreamReader ins = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(ins);
String s= new String();
do
{
[Link]("\n\n-------- MENU--------\n\n [Link] \n [Link] \n [Link] \n
[Link] \n [Link] \n 6. EXIT \n");
[Link]("Enter the Choice: ");
ch=[Link]([Link]());
switch(ch)
{
case 1 : [Link]("Enter the element to be pushed onto the Stack :");
s=[Link]();
[Link] (new Integer(s));
break;
case 2 : try
{
[Link] ("Element popped is " +[Link]());
}
catch(Exception e)
{
[Link]("Stack is empty");
}
break;
case 3 : try
{
Integer a=(Integer)[Link]();
[Link]("Element on top of the stack is " +a);
}
catch (Exception e)
{
[Link] ("Empty Stack");
}
break;
case 4 : [Link](" "+st);
break;
Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -5-
JAVA Lab Manual
case 5 : [Link]("Enter the element to be searched ");
int c= [Link]([Link]());
int k=[Link](new Integer(c));
if (k==-1)
[Link] ("Element not found");
else
[Link]("Element found at position " +k);
break;
case 6 : [Link](0);
}
} while(ch!=6);
}
}
OUTPUT:
-------- MENU--------
[Link]
[Link]
[Link]
[Link]
[Link]
6. EXIT
Enter the Choice: 1
Enter the element to be pushed onto the Stack : 65
Enter the Choice: 1
Enter the element to be pushed onto the Stack : 25
Enter the Choice: 1
Enter the element to be pushed onto the Stack : 85
Enter the Choice : 2
Element popped is 85
Enter the Choice: 3
Element on top of the stack is 25
Enter the Choice: 4
[65, 25]
Enter the Choice: 5
Enter the element to be searched 65
Element found at position 2
Enter the Choice: 6
Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -6-
JAVA Lab Manual
LABPRG8: Write a program to Create object of Treeset and use all the methods.
import [Link].*;
import [Link].*;
class ObTreeSet
{
public static void main(String arg[]) throws IOException
{
TreeSet ts = new TreeSet();
InputStreamReader ins = new InputStreamReader([Link]);
BufferedReader br =new BufferedReader(ins);
String s= new String();
[Link]("\n Enter the number of elements ");
int n= [Link]([Link]());
for(int i=0;i<n;i++)
{
[Link]("Enter the Element" +i);
s=[Link]();
[Link](s);
}
[Link]("\n The elements on the Treeset are " +ts);
[Link]("The First element on the treeset is " +[Link]());
[Link]("The Last element is " +[Link]());
[Link]("Enter the element to be removed ");
String sr=[Link]();
[Link](sr);
[Link]("Elements on the treeset currently are " +ts);
[Link]("Size of the treeset is " +[Link]());
}
OUTPUT
}
1. Enter the number of elements 5
Enter the Element0 45
Enter the Element1 65
Enter the Element2 85
Enter the Element3 15
Enter the Element4 36
The elements on the Treeset are [15, 36, 45, 65, 85]
The First element on the treeset is 15
The Last element is 85
Enter the element to be removed 45
Elements on the treeset currently are [15, 36, 65, 85]
Size of the treeset is 4
Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -7-
JAVA Lab Manual
2. Enter the number of elements 4
Enter the Element0 M
Enter the Element1 N
Enter the Element2 O
Enter the Element3 P
The elements on the Treeset are [M, N, O, P]
The First element on the treeset is M
The Last element is P
Enter the element to be removed M
Elements on the treeset currently are [N, O, P]
Size of the treeset is 3
LABPRG 9: Wrtie a program to check all MATHS FUNCTIONS
import [Link].*;
import [Link].*;
class Mathsfun
{
public static void main(String arg[]) throws IOException
{
[Link]("Sin value = "+[Link](60));
[Link]("Cos value = "+[Link](60));
[Link]("Tan value = "+[Link](60));
[Link]("Sin inverse value = "+[Link](60));
[Link]("Cos inverse value = "+[Link](60));
[Link]("Tan inverse value = "+[Link](60));
[Link]("Exponentional value of 3 = "+[Link](3));
[Link]("power value of 4 to 3 = " +[Link](4,3));
[Link]("Absolute value = "+[Link](-55));
[Link]("Round Value = "+[Link](15.547));
[Link]("Ceil Value = "+[Link](15.6));
[Link]("Floor Value ="+[Link](15.547));
[Link]("Square root value =" +[Link](16));
[Link]("Maximum Value is = "+[Link](5,125));
[Link]("Minimum Value is = "+[Link](5,125));
}
}
Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -8-
JAVA Lab Manual
OUTPUT:
Sin value = -0.3048106211022167
Cos value = -0.9524129804151563
Tan value = 0.320040389379563
Sin inverse value = NaN
Cos inverse value = NaN
Tan inverse value = 1.554131203080956
Exponentional value of 3 = 20.085536923187668
power value of 4 to 3 = 64.0
Absolute value = 55
Round Value = 16
Ceil Value = 16.0
Floor Value =15.0
Square root value =4.0
Maximum Value is = 125
Minimum Value is = 5
Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -9-