Final Java Lab Manual - 2025
Final Java Lab Manual - 2025
STUDENT NAME :
REGISTER
NUMBER :
SECTION :
YEAR &
SEMESTER : 2025 & I semester
OCT 2025
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
FACULTY OF SCIENCE AND HUMANITIES
DEPARTMENT OF COMPUTER APPLICATIONS
SRM Nagar, Kattankulathur – 603 203
CERTIFICATE
__________________________________________________________Register
2 Product of 2 numbers
3 Swap2 numbers
4 Area of circle
5 Odd or even
10 Fibonacci series
11 Palindrome Checking
12 Triangle printing
13 Matrix multiplication.
16 Inheritance-Single
17 Printing count of
vowels,consonants and digits
18 Add 2 dates
20 Threads
Ex. No: 1 Date:
SIMPLE PROGRAM
AIM
To write a Java program to take input from the user.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class.
STEP 4: Declare float, int, string and assign values.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
String S=s.nextLine();
System.out.println("You entered a String "+S);
int a=s.nextInt();
System.out.println("You entered an integer "+a);
float b=s.nextFloat();
System.out.println("You entered a float "+b);
}
}
OUTPUT
D
You entered a String D
11
You entered an integer 11
11
You entered a float 11.0
RESULT
The program has been executed successfully.
Ex. No: 2 Date:
PRODUCT
AIM
To write a Java program to print the product of two numbers.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare float a, b and p and assign values.
STEP 4: Use the formula p=a*b.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter any number : ");
float a=s.nextFloat();
System.out.print("Enter any number : ");
float b=s.nextFloat();
float p=a*b;
System.out.println("The product is "+p);
}
}
OUTPUT
Enter any number : 12
Enter any number : 5
The product is 60.0
RESULT
The program has been executed successfully.
Ex. No: 3 Date:
SWAP
AIM
To write a Java program to swap two numbers.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare int a, b, c and assign values
STEP 4: Swap the two numbers.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter any number : ");
int a=s.nextInt();
System.out.print("Enter any number : ");
int b=s.nextInt();
System.out.println("Before swap.");
System.out.println("The value of a is "+a+" and the value of b is "+b+".");
int c=a;
a=b;
b=c;
System.out.println("After swap.");
System.out.println("The value of a is "+a+" and the value of b is "+b+".");
}
}
OUTPUT
Enter any number : 4
Enter any number : 3
Before swap.
The value of a is 4 and the value of b is 3.
After swap.
The value of a is 3 and the value of b is 4.
RESULT
The program has been executed successfully.
Ex. No: 4 Date:
AREA
AIM
To write a Java program to find the area of a circle.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare float a, r and assign values
STEP 4: Area = 22/7*r*r.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter radius : ");
float r=s.nextFloat();
float a= 22/7*r*r;
System.out.println("Area of the circle : "+a);
}
}
OUTPUT
Enter radius : 5
Area of the circle : 75.0
RESULT
The program has been executed successfully.
Ex. No: 5 Date:
ODD OR EVEN
AIM
To write a Java program to find whether the given number is odd or even.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare int a and assign values
STEP 4: Use if else condition statement.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter any number : ");
int a=s.nextInt();
if (a%2==0)
System.out.println(a+" is even.");
else
System.out.println(a+" is odd.");
}
}
OUTPUT
Enter any number : 5
5 is odd.
RESULT
The program has been executed successfully.
Ex. No: 6 Date:
AIM
To write a Java program to find the greatest of 3 numbers.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare int a,b,c and assign values
STEP 4: Use if else if condition statement.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter a : ");
int a=s.nextInt();
System.out.print("Enter b : ");
int b=s.nextInt();
System.out.print("Enter c : ");
int c=s.nextInt();
if (a>b&&a>c)
System.out.println(a+" is greater.");
else if (b>a&&b>c)
System.out.println(b+" is greater.");
else
System.out.println(c+" is greater.");
}
}
OUTPUT
Enter a : 1
Enter b : 2
Enter c : 3
3 is greater.
RESULT
The program has been executed successfully.
Ex. No: 7 Date:
BASIC CALCULATOR
AIM
To write a Java program that mimics a calculator.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility,io,lang,lang.Math packages and define class name.
STEP 3: Use scanner class, Declare float a,b,p and assign values
STEP 4: Use switch case.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
import java.io.*;
import java.lang.*;
import java.lang.Math;
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter a : ");
int a=s.nextInt();
System.out.print("Enter b : ");
int b=s.nextInt();
System.out.print("Enter any of these operators [+,-,*,/] :");
char o=s.next().charAt(0);
float c=0;
switch (o) {
case '+':
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
case '/':
c=a/b;
break;
default:
System.out.println("Invalid PROGRAM.");
}
System.out.println("The final results : ");
System.out.println(a+" "+o+" "+b+" = "+c);
}
}
OUTPUT
Enter a : 5
Enter b : 5
Enter any of these operators [+,-,*,/] :*
The final results :
5 * 5 = 25.0
RESULT
The program has been executed successfully.
Ex. No: 8 Date:
FACTORIAL
AIM
To write a Java program to find the factorial of the given number.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare int i,f,n and assign values
STEP 4: Use for loop statement.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter any number : ");
int n=s.nextInt();
int i=1,f=1;
for (i=1;i<=n ;i++ ) {
f=f*i;
}
System.out.println("Factorial of "+n+" : "+f);
}
}
OUTPUT
Enter any number : 5
Factorial of 5 : 120
RESULT
The program has been executed successfully.
Ex. No: 9 Date:
AIM
To write a Java program to find the factorial of the given number using recursion.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare int n and long f and assign values
STEP 4: Use if else conditional statement.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter any number : ");
int n=s.nextInt();
long f = F(n);
System.out.println("Factorial of " + n + " = " + f);
}
public static long F(int n) {
if (n >= 1)
return n * F(n - 1);
else
return 1;
}
}
OUTPUT
Enter any number : 5
Factorial of 5 = 120
RESULT
The program has been executed successfully.
Ex. No: 10 Date:
FIBONACCI SERIES
AIM
To write a Java program to print the Fibonacci series of the given number.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility and io packages and define class name.
STEP 3: Use scanner class, Declare int i, n, n1, n2, n3 and assign values
STEP 4: Use for loop statement.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
import java.io.*;
class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter any number : ");
int n=s.nextInt();
int n1=0,n2=1,n3=0;
System.out.println("The fibonacci series of " + n + " are as follows : ");
for (int i=0;i<=n ;i++ ) {
System.out.println(n3+" ");
n3=n2+n1;
n1=n2;
n2=n3;
}
}
}
OUTPUT
Enter any number : 5
The fibonacci series of 5 are as follows :
0
1
2
3
5
8
RESULT
The program has been executed successfully.
Ex. No: 11 Date:
PALINDROME
AIM
To write a Java program to find whether the given number is palindrome or not.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare int m, n, N and assign values
STEP 4: Use while and if else conditional statements.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter some numbers : ");
int n=s.nextInt();
int m,N=0;
m=n;
while(n>0) {
N=N*10+n%10;
n=n/10;
}
System.out.println("The reverse of the numbers is "+N);
if (m==N)
System.out.println("So, it is a palindromic number.");
else
System.out.println("So, it is not a palindromic number.");
}
}
OUTPUT
Enter some numbers : 58285
The reverse of the number is 58285
So, it is a palindromic number.
RESULT
The program has been executed successfully.
Ex. No: 12 Date:
TRIANGLE
AIM
To write a Java program to print a number triangle.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare int i,j,n and assign values
STEP 4: Use for loop statement twice.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter some number : ");
int n=s.nextInt();
int i,j;
System.out.println("Here is a number triangle...");
for (i=1;i<=n ;i++ ) {
for (j=1;j<=i ;j++ ) {
System.out.print(j);
}
System.out.println();
}
}
}
OUTPUT
Enter some number : 5
Here is a number triangle...
1
12
123
1234
12345
RESULT
The program has been executed successfully.
Ex. No: 13 Date:
ARRAY MATRIX
AIM
To write a Java program to multiply using array matrix.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import io package and define class name.
STEP 3: Use scanner class, Declare int i, j, k, N and assign values
STEP 4: Use arrays and for loop statement.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.io.*;
class Main {
static int N = 4;
static void multiply (int mat1[][],int mat2[][],int res[][]) {
int i,j,k;
for (i=0;i<N ;i++ ) {
for (j=0;j<N ;j++ ) {
res[i][j]=0;
for (k=0;k<N ;k++ )
res[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
public static void main (String[] args) {
int mat1[][]={{1,1,1,1,1},{2,2,2,2,2},{3,3,3,3,3},{4,4,4,4,4},{5,5,5,5,5}};
int mat2[][]={{1,1,1,1,1},{2,2,2,2,2},{3,3,3,3,3},{4,4,4,4,4},{5,5,5,5,5}};
int res[][]=new int[N][N];
int i,j;
multiply(mat1,mat2,res);
System.out.println("Result matrix "+" is");
for (i=0;i<N ;i++ ){
for (j=0;j<N ;j++ )
System.out.print(res[i][j]+" ");
System.out.println();
}
}
}
OUTPUT
Result matrix is
10 10 10 10
20 20 20 20
30 30 30 30
40 40 40 40
RESULT
The program has been executed successfully.
Ex. No: 14 Date:
AIM
To write a Java program to print the area and perimeter of a rectangle of the given length and
breadth.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare double length and breadth and assign values
STEP 4: Area=length*breadth; Perimeter=2*(length+breadth).
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Rectangle {
private double length;
private double breadth;
public Rectangle(double length,double breadth) {
this.length=length;
this.breadth=breadth;
}
public double area() {
return length*breadth;
}
public double perimeter() {
return 2*(length+breadth);
}
}
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter length : ");
double length=s.nextDouble();
System.out.println("Enter breadth : ");
double breadth=s.nextDouble();
Rectangle rec=new Rectangle(length,breadth);
System.out.println("Area of the Rectangle : "+rec.area());
System.out.println("Perimeter of the Rectangle : "+rec.perimeter());
}
}
OUTPUT
Enter length : 5
Enter breadth : 5
Area of the Rectangle : 25.0
Perimeter of the Rectangle : 20.0
RESULT
The program has been executed successfully.
Ex. No: 15 Date:
AIM
To write a Java program to find whether the given number is odd or even.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare int n, N and assign values
STEP 4: Use if else conditional statement.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class EvenOddChecker {
private int n;
public EvenOddChecker(int n) {
this.n=n;
}
public void check() {
if (n%2==0) {
System.out.println(n+" is Even.");
}
else {
System.out.println(n+" is Odd.");
}
}
}
class Main {
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter a number : ");
if(!s.hasNextInt()) {
System.out.println("Invalid PROGRAM.Please enter an integer.");
s.close();
return;
}
int N=s.nextInt();
EvenOddChecker checker=new EvenOddChecker(N);
checker.check();
s.close();
}
}
OUTPUT
Enter a number : 5
5 is Odd.
RESULT
The program has been executed successfully.
Ex. No: 16 Date:
INHERITANCE
AIM
To write a Java program to calculate the area and perimeter of a circle using inheritance.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare double getArea, getPerimeter, radius, r1, r2, c1, c2 and assign
values
STEP 4: Use inheritance and override method.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Shape {
public double getPerimeter() {
return 0.0;
}
public double getArea() {
return 0.0;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Circle 1");
System.out.print("Enter radius : ");
double r1 = s.nextDouble();
Circle c1 = new Circle(r1);
System.out.printf("Perimeter of circle 1 : %2f" , c1.getPerimeter());
System.out.println();
System.out.printf("Area of circle 1 : %2f" , c1.getArea());
System.out.println();
System.out.println("Circle 2");
System.out.print("Enter radius : ");
double r2 = s.nextDouble();
Circle c2 = new Circle(r2);
System.out.printf("Perimeter of circle 2 : %2f" , c2.getPerimeter());
System.out.println();
System.out.printf("Area of circle 2 : %2f" , c2.getArea());
}
}
OUTPUT
Circle 1
Enter radius : 2
Perimeter of circle 1 : 12.566371
Area of circle 1 : 12.566371
Circle 2
Enter radius : 3
Perimeter of circle 2 : 18.849556
Area of circle 2 : 28.274334
RESULT
The program has been executed successfully.
Ex. No: 17 Date:
AIM
To write a Java program to find the number of vowels, consonants, digits and spaces in a sentence.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class, Declare char ch, string line and int i and assign values
STEP 4: Use if else if(3) conditional statement.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter a sentence with digits : ");
String line = s.nextLine();
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
line = line.toLowerCase();
for (int i = 0; i < line.length(); ++i) {
char ch = line.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
++vowels;
}
else if ((ch >= 'a' && ch <= 'z')) {
++consonants;
}
else if (ch >= '0' && ch <= '9') {
++digits;
}
else if (ch == ' ') {
++spaces;
}
}
System.out.println("The number of vowels in the sentence : " + vowels);
System.out.println("The number of consonants in the sentence : " + consonants);
System.out.println("The number of digits in the sentence : " + digits);
System.out.println("The number of spaces between the words in the sentence : " + spaces);
}
}
OUTPUT
Enter a sentence with digits : I am DD, I was born on 11.11.2007.
The number of vowels in the sentence : 6
The number of consonants in the sentence : 9
The number of digits in the sentence : 8
The number of spaces between the words in the sentence : 7
RESULT
The program has been executed successfully.
Ex. No: 18 Date:
AIM
To write a Java program to add two dates.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class.
STEP 4: Use calendar class.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
class Main {
public static void main(String[] args) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
Calendar cTotal = (Calendar) c1.clone();
cTotal.add(Calendar.YEAR, c2.get(Calendar.YEAR));
cTotal.add(Calendar.MONTH, c2.get(Calendar.MONTH) + 1);
cTotal.add(Calendar.DATE, c2.get(Calendar.DATE));
cTotal.add(Calendar.HOUR_OF_DAY, c2.get(Calendar.HOUR_OF_DAY));
cTotal.add(Calendar.MINUTE, c2.get(Calendar.MINUTE));
cTotal.add(Calendar.SECOND, c2.get(Calendar.SECOND));
cTotal.add(Calendar.MILLISECOND, c2.get(Calendar.MILLISECOND));
System.out.format("%s", c1.getTime());
System.out.println();
System.out.format("+ %s", c2.getTime());
System.out.println();
System.out.format("= %s", cTotal.getTime());
}
}
OUTPUT
Thu Sep 25 16:04:00 GMT 2025
+ Thu Sep 25 16:04:00 GMT 2025
= Fri Jul 21 08:08:00 GMT 4051
RESULT
The program has been executed successfully.
Ex. No: 19 Date:
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility package and define class name.
STEP 3: Use scanner class.
STEP 4: Use calendar class.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.util.*;
public class Main {
public static void main(String[] args)
{
Date d1 = new Date();
System.out.println("The current date is " + d1+".");
}
}
OUTPUT
The current date is Thu Sep 25 16:22:51 GMT 2025.
RESULT
The program has been executed successfully.
Ex. No: 20 Date:
THREAD
AIM
To write a Java program to print fibonacci series and reverse of a number using thread.
PROCEDURE
STEP 1: Start the program.
STEP 2: Import utility and io packages and extend classes from thread and define class name.
STEP 3: Use scanner class, Declare int a, b, c, n, N and assign values
STEP 4: Use run method and try and catch method and set priority for the program.
STEP 5: Print the program.
STEP 6: Stop execution.
PROGRAM
import java.io.*;
import java.util.*;
class Fibonacci extends Thread {
public void run() {
try {
Scanner s=new Scanner(System.in);
System.out.print("Enter some number : ");
int n=s.nextInt();
int a=0, b=1, c=1;
System.out.println("\n=================================");
System.out.println("Fibonacci series of "+n+" is : ");
System.out.println("\n=================================");
while (n>0) {
System.out.println(c+" ");
a=b;
b=c;
c=a+b;
n=n-1;
System.out.println("=================================\n");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
class Reverse extends Thread {
public void run() {
try {
Scanner s=new Scanner(System.in);
System.out.println("\n=================================");
int n=s.nextInt();
System.out.println("=================================");
System.out.println("Reverse of 1 to "+n+" is : ");
System.out.println("=================================");
for (int i=n; i >= 1 ; i-- ) {
System.out.print(i+" ");
}
System.out.println("\n=================================\n");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
try {
Reverse r = new Reverse();
r.setPriority(10);
r.start();
Fibonacci f = new Fibonacci();
f.start();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
OUTPUT
Enter some number :
=================================
5
=================================
Fibonacci series of 5 is :
=================================
1
=================================
2
=================================
3
=================================
5
=================================
8
=================================
6
=================================
Reverse of 1 to 6 is :
=================================
6 5 4 3 2 1
=================================
RESULT
The program has been executed successfully.