JAVA LAB Manual
JAVA LAB Manual
LABPRG 1: Write a program check whether two strings are equal or not.
import java.io. *;
class strequal
{
public static void main(String arg[]) throws IOException
{
System.out.println("Enter the First String:");
DataInputStream ds1 = new DataInputStream(System.in);
String s1 = ds1.readLine();
System.out.println("First String entered is :" +s1);
System.out.println("Enter the Second String :");
String s2 = ds1.readLine();
System.out.println("Second String entered is :" +s2);
if(s1.equalsIgnoreCase(s2))
System.out.println("Both the Strings are Equal");
else
System.out.println("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
import java.io.*;
class strreverse
{
public static void main(String arg[]) throws IOException
{
System.out.println("Enter a string:");
DataInputStream ds1=new DataInputStream(System.in);
String s1=ds1.readLine();
System.out.println("String entered is:" +s1);
StringBuffer sb=new StringBuffer(s1);
System.out.println("reversed String is :" +sb.reverse());
}
}
OUTPUT:
1. Enter a string: 2.Enter a string:
SIDDAGANGA MALAYALAM
String entered is:SIDDAGANGA String entered is:MALAYALAM
reversed String is :AGNAGADDIS reversed String is :MALAYALAM
import java.io.*;
class Sumofdigits
{
public static void main(String arg[]) throws IOException
{
int num,rem,sum=0;
DataInputStream ds =new DataInputStream(System.in);
System.out.println("Enter a Number :");
num=Integer.parseInt(ds.readLine());
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
}
System.out.println("Sum of digits of the given number is : " +sum);
}
}
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
System.out.print(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 java.io.*;
class Elesort
{
public static void main(String aeg[]) throws IOException
{
int[] a =new int[20];
int i,j,n,temp;
DataInputStream ds = new DataInputStream (System.in);
System.out.print("Enter the number of elements in the array : ");
n=Integer.parseInt(ds.readLine());
System.out.println("Enter the elements in the array : ");
for(i=0;i<n;i++)
a[i]= Integer.parseInt(ds.readLine());
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
}
System.out.println("The array after sorting is ");
for(i=0;i<n;i++)
System.out.print(a[i]);
}
}
LABPRG 7: Write a program to create an object of a STACK and use all the
methods.
import java.io.*;
import java.util.*;
class Obstack
{
public static void main(String arg[]) throws IOException
{
Stack st= new Stack();
int ch;
InputStreamReader ins = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ins);
String s= new String();
do
{
System.out.println("\n\n-------- MENU--------\n\n 1.PUSH \n 2.POP \n 3.PEEK \n
4.DISPLAY \n 5.SEARCH \n 6. EXIT \n");
System.out.println("Enter the Choice: ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1 : System.out.print("Enter the element to be pushed onto the Stack :");
s=br.readLine();
st.push (new Integer(s));
break;
case 2 : try
{
System.out.print ("Element popped is " +st.pop());
}
catch(Exception e)
{
System.out.println("Stack is empty");
}
break;
case 3 : try
{
Integer a=(Integer)st.peek();
System.out.print("Element on top of the stack is " +a);
}
catch (Exception e)
{
System.out.println ("Empty Stack");
}
break;
case 4 : System.out.println(" "+st);
break;
}
} while(ch!=6);
}
}
OUTPUT:
-------- MENU--------
1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.SEARCH
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
LABPRG8: Write a program to Create object of Treeset and use all the methods.
import java.io.*;
import java.util.*;
class ObTreeSet
{
public static void main(String arg[]) throws IOException
{
TreeSet ts = new TreeSet();
InputStreamReader ins = new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(ins);
String s= new String();
System.out.print("\n Enter the number of elements ");
int n= Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
System.out.print("Enter the Element" +i);
s=br.readLine();
ts.add(s);
}
System.out.println("\n The elements on the Treeset are " +ts);
System.out.println("The First element on the treeset is " +ts.first());
System.out.println("The Last element is " +ts.last());
System.out.println("Enter the element to be removed ");
String sr=br.readLine();
ts.remove(sr);
System.out.println("Elements on the treeset currently are " +ts);
System.out.println("Size of the treeset is " +ts.size());
}
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
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