25PMCA21 - Java Practical
25PMCA21 - Java Practical
2025 - 2026
NAME :
REG. NO :
April 2026
DEPARTMENT OF COMPUTER APPLICATIONS-PG
CERTIFICATE
Certified that this is a bonafide record of practical work done by
Mr./Ms.________ _____ . [Link]. _______________ in
the M.C.A.-(Master of Computer Application) Laboratory during the Year
2025-2026.
A. ADDITION OF TWO
12-01-2026 1
NUMBERS
B. PRODUCT OF THREE
19-01-2026 NUMBERS
2
D. CONVERISON OF
29-01-2026 5
CENTIGRADE TO FARENHEIT
E. CONVERSION OF
02-02-2026 FARENHEIT TO CENTIGRADE
7
IMPLEMENTATION OF A
3 16-02-2026
CLASS USING INTERFACE
11
5 23-02-2026 PALINDROME 15
6 26-02-2026 CONSTRUCTOR 17
METHOD OVERLOADING
A. CHANGING NUMBER OF
02-03-2026 19
7 ARGUMENTS
8 09-03-2026 INHERTANCE 23
ABSTRACTION
10 A. ABSTRACTION USING AN
16-03-2026 28
ABSTRACT CLASS
B. ABSTRACTION USING
20-03-2026 30
INTERFACE
11 23-03-2026 MULTITHREADING 32
EXCEPTION HANDLING
13 02-04-2026 ARITHMETIC EXCEPTION
37
APPLET PROGRAMMING
NETWORK PROGRAMMING
READING CONTENTS OF
18-04-2026 51
WEBSITES USING URL CLASS
CALCULATOR APPLICATION
16 18-04-2026 USING SWING COMPONENTS
53
1
PROCEDURE:
CODING:
class Add
{
public static void main (String args[])
{
int a = 10, b =
20, sum; sum =
a + b;
[Link]("Sum of " +a+ " and " +b+ " is " +sum);
}
}
OUTPUT:
PROCEDURE:
CODING:
class Product1
{
public static void main(String args[])
{
int a = 3, b = 5, c = 8, product;
product = a*b*c;
[Link]("Product of " +a+ ", " +b+ " and " +c+ " is " +product);
}
}
OUTPUT:
AIM: To calculate the simple interest of the given principle amount, number of years and
PROCEDURE:
Step 1: Start the program and import the required package [Link] to read input
Step 2: Declare the class SimpleInterest and define the main() method. Declare variables
Step 3: Create a Scanner object to accept input from the user through the keyboard.
Step 4: Get the principal amount, number of years, and rate of interest from the user using
nextDouble() method.
CODING:
import [Link].*;
class SimpleInterest1
{
public static void main(String args[])
{
double p,n,r,si;
Scanner sc=new Scanner([Link]);
[Link]("Enter the principle amount:");
p = [Link]();
[Link]("Enter the [Link] years:");
n = [Link]();
[Link]("Enter the rate of interest:");
r = [Link]();
si = (p*n*r)/100;
[Link]("Simple Interest=" +si);
}
}
OUTPUT:
PROCEDURE:
Step 1: Start the program and import the package [Link] to take input from the
user.
Step 2: Declare the class Farenheit and create the main() method. Declare two variables
Step 4: Ask the user to enter the temperature in Celsius and read the value using
[Link]().
CODING:
import [Link];
class Fahrenheit1
{
public static void main(String args[])
{
double c,f;
Scanner sc=new Scanner([Link]);
[Link]("Enter the value of celcius:");
c = [Link]();
f = (c * 9.0/5) + 32;
[Link]("Fahrenheit = " +f);
}
}
6
OUTPUT:
PROCEDURE:
Step 1: Start the program and import the package [Link] to read input
Step 2: Declare the class Celcius and define the main() method. Declare two
Step 4: Ask the user to enter the temperature in Fahrenheit and read the value using
[Link]().
CODING:
import [Link];
class Celsius1
{
public static void main(String args[])
{
double c,f;
Scanner sc=new Scanner([Link]);
[Link]("Enter the value of farenheit:");
f = [Link]();
c = (f - 32) * 5.0/9 ;
[Link]("Celsius = " +c);
}
}
8
OUTPUT:
Ex. No. 02
FACTORIAL OF A NUMBER
Date:09-02-2026
PROCEDURE:
CODING:
import [Link];
class Factorial
{
public static void main(String args[])
{
int fact,i,n;
fact=1;
Scanner s=new Scanner([Link]);
[Link]("Enter a number"); n=[Link]();
for(i=1;i<=n;i++)
{
fact=fact*i
}
[Link]("Factorial of the given number is " +fact);
}
}
10
OUTPUT:
PROCEDURE:
CODING:
class StudentMark
{
public static void main(String args[])
{
Student s1=new Student();
[Link](123,"Raja",75,88,90);
[Link]();
[Link]();
Student s2=new Student();
[Link](124,"Harini",85,98,90);
[Link]();
[Link]();
Student s3=new Student();
[Link](125,"Brindha",99,96,80);
[Link]();
[Link]();
}
}
class Student
{
int rno;
String Stname;
int m1,m2,m3,total;
double avg;
void getData(int no, String name,int x,int y, int z)
{
12
rno=no; Stname=name;
m1=x;
m2=y;
m3=z;
}
void calculate()
{
total=m1+m2+m3; avg=total/3.0;
}
void display()
{
[Link]("Reg no: " +rno);
[Link]("Name: " +Stname);
[Link]("Mark1: " +m1);
[Link]("Mark2: " +m2);
[Link]("Mark3: " +m3);
[Link]("Total: " +total);
[Link]("Average: " +avg);
}
}
OUTPUT:
AIM: To write a java program to find the sum and average of n numbers.
PROCEDURE:
Step 5: Calculate the sum of the numbers by iterating through the array.
Step 6: Calculate the average using the formula: average = sum / N
Step 7: Display the sum and average.
Step 8: End the program.
CODING:
import [Link];
public class Arraysum
{
public static void main(String args[])
{
int
n,sum=0;
float avg;
Scanner s=new Scanner([Link]);
[Link]("Enter limit of 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++)
{sum=sum+a[i]; avg=sum/n;}
14
[Link]("Sum="+sum);
[Link]("Average="+avg);
}
}
OUTPUT:
Ex. No. 05
PALINDROME
Date:23-02-2026
AIM: To generate and check whether the given word is a palindrome or not using java
program.
PROCEDURE:
CODING:
import [Link].*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner([Link]);
[Link]("Enter a string/number to check if it is a palindrome");
original = [Link]();
int length = [Link]();
for ( int i = length - 1; i >= 0; i-- ) {
reverse = reverse + [Link](i);
}
if ([Link](reverse)) {
16
OUTPUT:
Ex. No. 06
CONSTRUCTOR
Date:26-02-2026
AIM: To write a Java program to create an employee class, initialize objects using a
constructor, and display employee details.
PROCEDURE:
Step 2:Create a class (e.g., Employee) with data members to store object information.
Step 3: Define a constructor in the class with parameters to initialize the data members.
Step 5: Inside the main() method, create objects of the class and pass values to the
constructor.
Step 6: Call the method (e.g., info()) on each object to display details.
CODING:
class Employee {
int empid;
String empName;
Employee(int id, String name)
{
[Link]=id;
[Link]=name;
}
void info() {
[Link]("ID: "+empid+ "Name: "+empName);
}
public static void main(String args[]) {
Employee obj1=new Employee(10245,"Chaitanya");
Employee obj2=new Employee(92232,"Negan");
[Link]();
[Link]();
}
}
18
OUTPUT:
AIM: To implement the concept of method overloading by changing the number of arguments.
PROCEDURE:
CODING:
class Adder
{
static int add(int a,int b)
{
return a+b;
}
static int add(int a,int b,int c)
{
return a+b+c;
}
}
class MethodOverloading
{
public static void main(String args[])
{
[Link]([Link](11,11)); [Link]([Link](11,11,11));
}
}
OUTPUT:
PROCEDURE:
Step 2: Create methods with the same name but different argument data types.
Step 3: Overload the methods by changing the data type of the arguments.
Step 4: Implement the method logic based on the data types of the arguments.
Step 5: In the main method, create an object and call the overloaded methods
with different argument types.
CODING:
class Adder
{
static int add(int a,int b)
{
return a+b;
}
static double add(double a,double b)
{
return a+b;
}
}
class MethodOverloading1
{
public static void main(String args[])
{
[Link]([Link](11,11)); [Link]([Link](12.3,12.6));
}
}
22
OUTPUT:
Ex. No. 08
INHERTANCE
Date:09-03-2026
AIM: To implement inheritance for calculating the gross pay and net pay for employees
using java.
PROCEDURE:
Step 1: Define a base (parent) class that will contain common attributes and methods.
Step 2: Create a derived (child) class that extends the base class.
Step 3: In the child class, you can inherit the methods and variables from the parent class.
Step 4: Optionally, you can override methods in the child class to provide specific behavior.
Step 5: In the main method, create an object of the child class and access the inherited methods
and attributes.
CODING:
class Inheritance
{
public static void main(String args[])
{
pay p1=new pay();
[Link](101,"Saravanan","Manager");
[Link](45000);
[Link]();
[Link]();
[Link]();
pay p2=new pay();
[Link](102,"Hubert","General Manager");
[Link](400000);
[Link]();
[Link]();
[Link]();
}
}
class employee
{
int empid;
String name;
String desig;
void getdetails(int e, String s, String d)
{
empid = e;
name = s;
desig = d;
24
}
void putdetails()
{
[Link]("Empid:"+empid);
[Link]("Name:"+name);
[Link]("Designation:"+desig);
}
}
class pay extends employee
{
double bp;
double hra;
double da;
double pf;
double grosspay;
double netpay;
void getpay(double p)
{
bp=p;
}
void calculate()
{
da = bp * 120/100;
hra = bp * 20/100;
pf = bp * 12/100;
grosspay = bp + hra + da;
netpay = grosspay - pf;
}
void display()
{
[Link]("Basic Pay: "+bp);
[Link]("DA: "+da);
[Link]("HRA: "+hra);
[Link]("PF: "+pf);
[Link]("Gross Pay: "+grosspay);
[Link]("Net Pay: "+netpay);
}
}
25
OUTPUT:
Ex. No. 09
METHOD OVERRIDING
Date:12-03-2026
PROCEDURE:
Step 1: Define a parent (Animal) class with a method that you want to override
in the child class.
Step 2: Create a child (Dog) class that extends the parent class.
Step 3: In the child class, define a method with the same signature as the one in the
parent class.
Step 4: Implement the logic in the child class method, which replaces the behavior
of the parent class method.
Step 5: In the main method, create an object of the child class and call the
overridden method.
CODING:
class Animal{
public void displayinfo(){
[Link]("I am an animal.");
}
}
class Dog extends Animal
{
public void displayinfo(){
[Link]("I am a dog.");
}
}
class Main{
public static void main(String args[])
{
Dog d1=new Dog();
[Link]();
}
}
27
OUTPUT:
PROCEDURE:
Step 4: In the subclass, implement the abstract methods from the abstract class
with the required logic.
Step 5: In the main method, create an object of the subclass and call the
implemented methods.
CODING:
}
class Abstraction
{
public static void main(String args[])
{
Shape s=new
Circle1(); Shape
s1=new Rectangle();
[Link]();
[Link]();
}
}
OUTPUT:
PROCEDURE:
Step 1: Define an interface using the interface keyword, which can only contain abstract
methods (methods without a body).
Step 2: Create methods in the interface that represent the behaviors that classes
implementing the interface must define.
Step 3: Create a class that implements the interface using the implements keyword.
Step 4: In the implementing class, provide the method implementations for all abstract
methods declared in the interface.
Step 5: In the main method, create an object of the implementing class and call the
methods defined in the interface.
CODING:
class Main1
{
public static void main(String args[])
{
Rectangle r1=new Rectangle();
[Link](5,6);
}
}
interface Polygon
{
void getArea(int length, int breadth);
}
class Rectangle implements Polygon
{
public void getArea(int length, int breadth)
31
{
[Link]("The area of a rectangle is " +(length*breadth));
}
}
OUTPUT:
Ex. No. 11
MULTITHREADING
Date:23-03-2026
PROCEDURE:
Step 1: Define a class that extends the Thread class or implements the Runnable interface.
Step 2: Override the run() method in the class to define the code that will execute in the
thread.
Step 3: Create an instance of the class (either Thread or Runnable) in the main method.
Step 4: Start the thread using the start() method to invoke the run() method concurrently.
Step 5: Optionally, manage multiple threads by using [Link]() or other
synchronization mechanisms to ensure proper execution.
CODING:
class ThreadTest
{
public static void main(String args[])
{
A t1 = new A();
B t2 = new B();
C t3 = new C();
try
{
[Link]();
[Link](); // wait until A finishes
[Link]();
[Link](); // wait until B finishes
[Link](); // C starts only after B exits
}
catch(Exception e)
{
[Link](e);
33
}
}
}
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
[Link]("\t From thread A :=" + i);
}
[Link]("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
[Link]("\t From thread B:= " + j);
}
[Link]("Exit from B");
}}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
[Link]("\t From thread C:= " + k);
}
[Link]("Exit from C");
}}
34
OUTPUT:
Ex. No. 12
THREAD PRIOPRITY
Date:27-03-2026
PROCEDURE:
CODING:
[Link]().setPriority(Thread.MAX_PRIORITY);
thrun t1 = new thrun("Thread1", Thread.NORM_PRIORITY + 2);
thrun t2 = new thrun("Thread2", Thread.NORM_PRIORITY - 2);
[Link]("Main Thread: " + [Link]());
[Link]("Main Thread Priority: " +
[Link]().getPriority());
}
}
OUTPUT:
PROCEDURE:
Step 1: Define a block of code where an exception might occur, such as dividing by
zero or performing an invalid arithmetic operation.
Step 2: Use a try block to write the code that might throw an ArithmeticException.
Step 3: Catch the exception using a catch block to handle the error gracefully.
The catch block will specify the type of exception,such as
ArithmeticException.
Step 4: Inside the catch block, implement the code to handle the exception
(e.g., displaying an error message or providing an alternative action).
Step 5: Optionally, use a finally block to execute code that should run whether
or not an exception was thrown (e.g., closing resources).
CODING:
class JavaException1{
public static void main(String args[]){
try{
int d=0,n=20;
int fraction=n/d;
}catch(ArithmeticException e){
[Link]("In the catch block due to Exception = " +e);}
[Link]("End of Main");
}}
OUTPUT:
PROCEDURE:
CODING:
import [Link].*;
import [Link].*;
public class SampleApp extends Applet
{
public void paint(Graphics g)
{
[Link]("Welcome to Java Applet",400,200);
39
}
}
/*<applet code="[Link]" WIDTH="500" HEIGHT="500">
</applet> */
OUTPUT:
APPLET VIEWER:
PROCEDURE:
CODING:
import [Link].*;
import [Link].*;
/*
<applet code="ParamDemo" width="300" height="300">
<param name="A" value="20">
<param name="B" value="30">
</applet> */
41
OUTPUT:
APPLET VIEWER:
PROCEDURE:
Step 1: Create a class that extends Applet and override the paint() method.
Step 2: Use the Graphics object provided in the paint() method.
Step 3: Use drawing methods like drawLine(), drawRect(), drawOval(), etc. to draw
shapes.
Step 4: Specify the shape's coordinates and dimensions in the drawing methods.
Step 5: Compile and run the applet to see the shapes drawn in the applet window.
CODING:
import [Link];
import [Link].*;
public class GraphicsDemo extends Applet
{
public void paint(Graphics g)
{
[Link]([Link]);
[Link]("Welcome",50,50);
[Link]([Link]);
[Link](20,30,20,300);
[Link](70,100,30,30);
[Link](170,100,30,30);
[Link](70,200,30,30);
[Link]([Link]);
[Link](170,200,30,30);
}}
/* <applet code="[Link]" width="300" height="300">
</applet> */
43
OUTPUT:
APPLET VIEWER:
PROCEDURE:
Step 1: Create a class that extends Applet and override the paint() method.
Step 2: Inside the paint() method, use the Graphics object to draw three circles using the
drawOval() method.
Step 3: Set different positions and sizes for each circle by adjusting the parameters of
the drawOval() method.
Step 4: Compile the applet and run it to display the circles in the applet window.
CODING:
import [Link];
import [Link];
import [Link];
public class ThreeCircleApplet extends Applet {
public void paint(Graphics g) {
//First circle filled with BLUE color
[Link]([Link]);
[Link](50,50,50,50);
//First circle filled with GREEN color
[Link]([Link]);
[Link](50,100,50,50);
//First circle filled with YELLOW color
[Link]([Link]);
[Link](50,150,50,50);
}
}
/*<applet code="ThreeCircleApplet" width=200 height=250>
</applet>*/
45
OUTPUT:
APPLET VIEWER:
PROCEDURE:
Step 1: Create a class that extends Applet and override the paint() method.
Step 2: Inside the paint() method, use the Graphics object to draw multiple circles using
drawOval() method.
Step 3: For concentric circles, ensure all circles have the same center but different radii
(sizes).
Step 4: Adjust the circle’s dimensions by changing the width and height parameters in
drawOval().
Step 5: Compile and run the applet to display concentric circles in the applet window.
CODING:
import [Link].*;
import [Link].*;
}
}
/* <applet code=ConcentricCircles width=300 height=300>
</applet> */
OUTPUT:
APPLET VIEWER:
PROCEDURE:
CODING:
import [Link].*;
public class URLDemo
{
public static void main(String[] args)
{
try
{
URI uri = new URI("[Link]
URL url = [Link]();
[Link]("Protocol: "+[Link]());
[Link]("Host Name: "+[Link]());
[Link]("Port Number: "+[Link]());
[Link]("File Name: "+[Link]());
}
catch(Exception e)
{
[Link](e);
}
}
}
49
OUTPUT:
PROCEDURE:
CODING:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
OUTPUT:
PROCEDURE:
CODING:
[Link]
import [Link].*;
import [Link].*;
public class MyServer {
public static void main(String[] args){ try{
ServerSocket ss=new ServerSocket(6666);
Socket s=[Link]();
DataInputStream dis=new DataInputStream([Link]());
String str=(String)[Link]();
[Link]("message= "+str);
[Link](); }
catch(Exception e)
{
[Link](e);
}}}
[Link]
52
import [Link].*;
import [Link].*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream([Link]());
[Link]("Hello Server");
[Link]();
[Link]();
[Link]();
}catch(Exception e){[Link](e);}
}
}
OUTPUT:
PROCEDURE:
Step 1:Start
Step 2:Create JFrame and set properties
Step 3:Create JTextField for display
Step 4:Create buttons and add to panel
Step 5:Add ActionListener to buttons
Step 6:On button click:
a. Append numbers/operators to expression
b. Perform calculation on ‘=’
c. Clear on ‘C’
Step 7:Display result
Step 8:Stop
CODING:
import [Link].*;
import [Link].*;
import [Link].*;
public class SimpleCalculator extends JFrame implements ActionListener {
JTextField textField;
double num1 = 0, num2 = 0, result = 0;
char operator;
String expression = "";
public SimpleCalculator() {
setTitle("Simple Calculator");
setSize(300, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
textField = new JTextField();
[Link](new Font("Times New Roman", [Link], 20));
[Link]([Link]);
[Link](new Dimension(120, 80));
add(textField, [Link]);
JPanel panel = new JPanel();
[Link](new GridLayout(5, 4, 5, 5));
String[] buttons = {
"7","8","9","/",
54
"4","5","6","*",
"1","2","3","-",
"0",".","=","+",
"C"
};
for (String text : buttons) {
JButton btn = new JButton(text);
[Link](new Font("Times New Roman", [Link], 18));
[Link](this);
[Link](btn);
}
add(panel, [Link]);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = [Link]();
if (([Link](0) >= '0' && [Link](0) <= '9') || [Link](".")) {
expression += command;
[Link](expression);
}
else if ([Link]("C")) {
expression = "";
[Link]("");
num1 = num2 = result = 0;
}
else if ([Link]("+") || [Link]("-") ||
[Link]("*") || [Link]("/")) {
operator = [Link](0);
expression += command;
[Link](expression);
}
else if ([Link]("=")) {
try {
String[] parts = [Link]("[+\\-*/]");
num1 = [Link](parts[0]);
num2 = [Link](parts[1]);
switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num1 / num2; break;
}
[Link](expression + " = " + result);
expression = [Link](result); // allow further calculation
} catch (Exception ex) {
[Link]("Error");
}
}
}
public static void main(String[] args) {
55
new SimpleCalculator();
}
}
OUTPUT: