0% found this document useful (0 votes)
98 views

JAVA LAB Manual

The document contains a lab manual with 7 Java programs covering topics like string comparison, string reversal, digit sum of a number, multiplication table, prime number checking, array sorting, and stack operations. Each program is presented with its source code and sample input/output. The manual provides examples and step-by-step instructions for learning common Java concepts and data structures.

Uploaded by

irfanahmed.dba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

JAVA LAB Manual

The document contains a lab manual with 7 Java programs covering topics like string comparison, string reversal, digit sum of a number, multiplication table, prime number checking, array sorting, and stack operations. Each program is presented with its source code and sample input/output. The manual provides examples and step-by-step instructions for learning common Java concepts and data structures.

Uploaded by

irfanahmed.dba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

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

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 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

LABPRG 3: Write a program check to find the SUM OF A DIGITS of a given


number

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);
}
}

Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -2-


JAVA Lab Manual

LABPRG 4: Write a program to display the MULTIPLICATION TABLE


import java.io.*;
class Multipletab
{
public static void main(String a[]) throws IOException
{
int i,prod, num,limit; OUTPUT:
System.out.println("Enter the number :"); Enter the number : 8
Enter the Limit : 10
DataInputStream ds = new DataInputStream (System.in);
Multiplication Table
num=Integer.parseInt(ds.readLine()); 8 * 1=8
System.out.println("Enter the Limit :"); 8 * 2=16
limit=Integer.parseInt(ds.readLine()); 8 * 3=24
System.out.println("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
System.out.println (num+ " * " +i+ "=" +prod); 8 * 9=72
} 8 * 10=80
}
}
LABPRG 5: Write a program to display all the PRIME numbers between the
range.
import java.io.*;
class Prime
{
public static void main (String arg[]) throws IOException
{
int a,l,u;
DataInputStream ds=new DataInputStream(System.in);
System.out.print("Enter the lower limit : ");
l=Integer.parseInt (ds.readLine());
System.out.print("Enter the upper limit : ");
u=Integer.parseInt(ds.readLine());
System.out.print("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
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]);
}

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 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;

Ramalinga Reddy.S Lecturer, SSCASC, Tumkur, -5-


JAVA Lab Manual

case 5 : System.out.print("Enter the element to be searched ");


int c= Integer.parseInt(br.readLine());
int k=st.search(new Integer(c));
if (k==-1)
System.out.println ("Element not found");
else
System.out.println("Element found at position " +k);
break;
case 6 : System.exit(0);

}
} 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

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 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

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 java.io.*;
import java.util.*;
class Mathsfun
{
public static void main(String arg[]) throws IOException
{
System.out.println("Sin value = "+Math.sin(60));
System.out.println("Cos value = "+Math.cos(60));
System.out.println("Tan value = "+Math.tan(60));
System.out.println("Sin inverse value = "+Math.asin(60));
System.out.println("Cos inverse value = "+Math.acos(60));
System.out.println("Tan inverse value = "+Math.atan(60));
System.out.println("Exponentional value of 3 = "+Math.exp(3));
System.out.println("power value of 4 to 3 = " +Math.pow(4,3));
System.out.println("Absolute value = "+Math.abs(-55));
System.out.println("Round Value = "+Math.round(15.547));
System.out.println("Ceil Value = "+Math.ceil(15.6));
System.out.println("Floor Value ="+Math.floor(15.547));
System.out.println("Square root value =" +Math.sqrt(16));
System.out.println("Maximum Value is = "+Math.max(5,125));
System.out.println("Minimum Value is = "+Math.min(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-

You might also like