NANDHA ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
ERODE-52.
PG AND RESEARCH DEPARTMENT OF COMPUTER SCIENCE
RECORD NOTE BOOK
II YEAR 2025 - 2026
24UCOSCP301 – PROGRAMMING LAB-JAVA
NAME : _________________________________________________
[Link] : _________________________________________________
CLASS : _________________________________________________
NANDHA ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
ERODE-52.
PG AND RESEARCH DEPARTMENT OF COMPUTER SCIENCE
This is certified that the bonafide record of practical work done by
…………………………………. [Link] …………………. during the academic year
2025 - 2026.
STAFF INCHARGE HEAD OF THE DEPARTMENT
Submitted for the End Semester Practical Examination held on
………………… in the PG and Research Department of Computer Science in
Nandha Arts and Science College(Autonomous), Erode.
INTERNAL EXAMINER EXTERNAL EXAMINER
[Link] DATE CONTENTS SIGNATURE
1. Class & Object
2. Recursive method
3. Constructor Overloading method
4. Interfaces
5. User-defined Packages
6. Exception Handling
7. Reading and Writing process in Files
8. Handling Mouse Events
9. Menus
10. Student details using GUI
[Link]
CLASS & OBJECT
DATE:
Aim:
To Develop a Simple Application using Class & Object.
Algorithm:
Step 1: Start the program.
Step 2: Create a class Car with two data members:
model (String)
year (int)
Step 3: Define a constructor Car (String modelName, int modelYear) to initialize the car
details.
Step 4: Define a method displayDetails() to print the car’s model and manufacturing year.
Step 5: Create another class CarDemo with the main() method.
Step 6: Inside main(), create two objects of the Car class with different values.
Step 7: Call the method displayDetails() for both objects to display their details.
Step 8: Stop the program.
1
Coding:
class Car
{
String model;
int year;
public Car(String modelName, intmodelYear)
{
model = modelName;
year = modelYear;
}
public void displayDetails()
{
[Link]("Car Model: " + model);
[Link]("Manufacturing Year: " + year);
}
}
public class CarDemo
{
public static void main(String[] args)
{
Car car1 = new Car("Toyota Corolla", 2020);
Car car2 = new Car("Honda Civic", 2022);
[Link]();
[Link]();
}
}
2
Output:
Car Model: Toyota Corolla
Manufacturing Year: 2020
Car Model: Honda Civic
Manufacturing Year: 2022
Result:
Thus the program has been executed successfully.
3
[Link]
RECURSIVE METHOD
DATE:
Aim:
To Implement the concept of Recursive method
Algorithm
Step 1: Start the program.
Step 2: Create a class factorial with a recursive method fact(int n).
Step 3: Inside fact(n):
If n == 0 or n == 1, return 1 (base case).
Otherwise, return n * fact(n - 1) (recursive call).
Step 4: Create another class recursion with the main() method.
Step 5: In main(), create an object of the factorial class.
Step 6: Call the method fact() with values (e.g., 3, 4, 5) and print the results.
Step 7: Stop the program.
4
Coding:
class factorial
int fact(int n)
int result;
if (n == 1)
return 1;
result = fact(n - 1) * n;
return result;
class recursion
public static void main(String[] args)
factorial f = new factorial();
[Link]("Factorial of 3 is "+ [Link](3));
[Link]("Factorial of 4 is "+ [Link](4));
[Link]("Factorial of 5 is "+ [Link](5));
5
Output:
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120.
Result:
Thus the program has been executed successfully.
6
[Link]
CONSTRUCTOR OVERLOADING METHOD
DATE:
Aim:
To Showcase the Constructor Overloading method.
Algorithm:
Step 1: Start the program.
Step 2: Create a class student with two data members:
name (String)
age (int)
Step 3: Define three constructors:
A constructor with no parameters that sets default values.
A constructor with one parameter (name only).
A constructor with two parameters (name and age).
Step 4: Define a method display() to print the student’s name and age.
Step 5: In the main() method:
Create an object using the no-argument constructor.
Create an object using the one-argument constructor.
Create an object using the two-argument constructor.
Step 6: Call the display() method for all three objects to print their details.
Step 7: Stop the program.
7
Coding:
public class student
{
String name;
int age;
public student()
{
name = "Unknown";
age = 0;
}
public student(String studentname)
{
name = studentname;
age = 0;
}
public student(String studentname, int studentage)
{
name = studentname;
age = studentage;
}
public void display()
{
[Link]("Name: " + name + ", Age: " + age);
}
public static void main(String[] args)
{
student s1 = new student();
student s2 = new student("Alice");
student s3 = new student("Bob", 21);
[Link]();
[Link]();
[Link](); } }
8
Output:
Name: Unknown, Age: 0
Name: Alice, Age: 0
Name: Bob, Age: 21
Result:
Thus the program has been executed successfully.
9
[Link]
INTERFACES
DATE:
Aim:
To demonstrate the use of interfaces in Java .
Algorithm:
Step 1: Define an interface Animal with two abstract methods: sound() and eat().
Step 2: Create a class Dog that implements the Animal interface:
Implement the sound() method to display dog’s sound.
Implement the eat() method to display dog’s eating habit.
Step 3: Create a class Cat that implements the Animal interface:
Implement the sound() method to display cat’s sound.
Implement the eat() method to display cat’s eating habit.
Step 4: In the InterfaceExample class:
Create an Animal reference pointing to a Dog object.
Call the sound() and eat() methods.
Create an Animal reference pointing to a Cat object.
Call the sound() and eat() methods.
Step 5: Display the output showing how different classes implement the same interface
methods.
10
Coding:
interface Animal
{
void sound();
void eat();
}
class Dog implements Animal
{
public void sound()
{
[Link]("Dog barks: Woof Woof!");
}
public void eat()
{
[Link]("Dog eats bones");
}
}
class Cat implements Animal
{
public void sound()
{
[Link]("Cat meows: Meow Meow!");
}
public void eat()
{
[Link]("Cat eats fish");
}
}
public class InterfaceExample
{
public static void main(String[] args)
{
Animal d = new Dog();
[Link]();
[Link]();
11
Animal c = new Cat();
[Link]();
[Link]();
}
}
12
Output:
Dog barks: Woof Woof!
Dog eats bones
Cat meows: Meow Meow!
Cat eats fish
Result:
Thus the program has been executed successfully.
.
13
[Link]
USER-DEFINED PACKAGES
DATE:
Aim:
To Implement the User-defined Packages.
Algorithm:
Step 1: Start the program.
Step 2: Create a package named example.
Step 3: Inside the package, define a class Test with a method show() that prints a message.
Step 4: In the main() method of Test, create an object and call show().
Step 5: Compile the class with javac -d . [Link] so that the .class file is stored inside the
example folder.
Step 6: Create another class Mypack outside the example package.
Step 7: Import the package [Link] using the import keyword.
Step 8: In the main() method of Mypack, create an object of Test and call its show() method.
Step 9: Compile and run [Link].
Step 10: End the program.
Coding:
14
public class Test
{
public void show()
{
[Link]("Hello!! How are you?");
}
public static void main(String[] args)
{
Test t = new Test();
[Link]();
}
}
import [Link];
public class Mypack
{
public static void main(String[] args)
{
Test t = new Test();
[Link]();
}
}
Output:
15
Hello!! How are you?
Result:
Thus the program has been executed successfully.
16
[Link]
EXCEPTION HANDLING
DATE:
Aim:
To Implement Exception Handling with Multiple Catch Statements
Algorithm:
Step 1: Start the program.
Step 2: Inside the main() method, declare and initialize an integer array.
Step 3: Write a try block with two possible exceptions:
Accessing an invalid index (n[5]) → ArrayIndexOutOfBoundsException
Division by zero (10 / 0) → ArithmeticException
Step 4: Provide multiple catch blocks:
First catch handles ArrayIndexOutOfBoundsException.
Second catch handles ArithmeticException.
Third catch handles any other general Exception.
Step 5: Print a suitable error message in each catch block.
Step 6: After exception handling, continue program execution by printing a message.
Step 7: End the program.
Coding:
public class multiplecatchexample
17
{
public static void main(String[] args)
{
try
{
int[] n= {1, 2, 3};
[Link](n[5]);
int r = 10 / 0;
[Link](r); /
}
catch (ArrayIndexOutOfBoundsException e)
{
[Link]("Error: Array index is out of bounds!");
}
catch (ArithmeticException e)
{
[Link]("Error: Cannot divide by zero!");
}
catch (Exception e)
{
[Link]("Error: An unexpected exception occurred!");
}
[Link]("Program continues...");
} }
Output:
18
Error: Array index is out of bounds!
Program continues...
Result:
Thus the program has been executed successfully.
[Link] READING AND WRITING PROCESS IN FILES
19
DATE:
Aim:
To Display the Reading and Writing process in Files’
Algorithm:
Step 1: Check if the file exists:
If the file doesn’t exist, create it.
Step 2: Write to the file:
Open the file using FileWriter and write the string "Files in java are seriously good!" to
the file.
Step 3: Close the file after writing.
Step 4: Read from the file:
Open the file using Scanner and read the contents line by line.
Step 5: Handle any exceptions:
Handle any IO Exception that may occur during both reading and writing processes.
Coding:
import [Link];
import [Link];
import [Link];
20
import [Link];
public class ReadWriteFile
{
public static void main(String[] args)
{
String filename = "[Link]";
try
{
FileWriter writer = new FileWriter(filename);
[Link]("Files in java are perfectly good!");
[Link]();
[Link]("Successfully Written!");
}
catch (IOException e)
{
[Link]("An error occurred during writing.");
[Link]();
return;
}
File file = new File(filename);
if (![Link]())
{
[Link]("File not found: " + [Link]());
return;
}
try
{
Scanner reader = new Scanner(file);
[Link]("Reading from the file:");
while ([Link]())
{
String data = [Link]();
[Link](data);
}
[Link]();
21
}
catch (IOException e)
{
[Link]("An error occurred during reading.");
[Link]();
}
}
}
Output:
Successfully Written!
Reading from the file:
Files in java are perfectly good!
22
Result:
Thus the program has been executed successfully.
[Link]
HANDLING MOUSE EVENTS
DATE:
23
Aim:
To Create an application to Handle Mouse Events.
Algorithm:
Step 1: Create a JFrame (Window):
Set the title of the window ("Simple Mouse Event Demo").
Set the window size to 400x300 pixels.
Ensure that the window closes properly when the user exits
(setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)).
Make the window visible by calling setVisible(true).
Step 2: Implement MouseListener Interface:
Implement the MouseListener interface, which contains 5 methods for handling mouse
events.
Each method corresponds to a specific mouse action.
Step 3: Add Mouse Listener:
Add the MouseListener to the JFrame to listen for mouse events.
Step 4: Override Mouse Listener Methods:
mouseClicked(MouseEvent e): Prints the coordinates of the mouse click.
mousePressed(MouseEvent e): Prints a message when the mouse button is pressed.
mouseReleased(MouseEvent e): Prints a message when the mouse button is released.
mouseEntered(MouseEvent e): Prints a message when the mouse enters the window.
mouseExited(MouseEvent e): Prints a message when the mouse exits the window.
Step 5: Run the Program:
Initialize the window and start the event loop by calling the main method.
Coding:
import [Link].*;
import [Link].*;
import [Link].*;
public class SimpleMouseEvent extends JFrame implements MouseListener
24
{
public SimpleMouseEvent()
{
super("Simple Mouse Event Demo");
addMouseListener(this);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
[Link]("Mouse Clicked at (" + [Link]() + ", " + [Link]() + ")");
}
public void mousePressed(MouseEvent e)
{
[Link]("Mouse Pressed");
}
public void mouseReleased(MouseEvent e)
{
[Link]("Mouse Released");
}
public void mouseEntered(MouseEvent e)
{
[Link]("Mouse Entered the window");
}
public void mouseExited(MouseEvent e)
{
[Link]("Mouse Exited the window");
}
public static void main(String[] args)
{
new SimpleMouseEvent();
}
}
25
Output:
1. Clicking the mouse:
Mouse Clicked at (x, y)
2. Pressing the mouse:
26
Mouse Pressed
3. Releasing the mouse:
Mouse Released
4. Entering the window:
Mouse Entered the window
5. Exiting the window:
Mouse Exited the window
Result:
Thus the program has been executed successfully.
[Link]
MENUS
DATE:
Aim:
To Showcase the concept of Menus.
27
Algorithm:
Step 1: Create JFrame window.
Step 2: Create a menu bar (JMenuBar).
Step 3: Create "File" menu (JMenu) and add menu items ("New", "Open", separator, "Exit").
Step 4: Create "Edit" menu and add menu items ("Cut", "Copy", "Paste").
Step 5: Add both menus to the menu bar.
Step 6: Set the menu bar on the JFrame.
Step 7: Add an action listener to the "Exit" menu item to close the program.
Step 8: Set JFrame properties like title, size, default close operation, and visibility.
Step 9: Run the program and display the window with menus.
Coding:
import [Link].*;
import [Link].*;
public class SimpleMenuDemo extends JFrame
{
28
public SimpleMenuDemo()
{
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newItem = new JMenuItem("New");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem exitItem = new JMenuItem("Exit");
[Link](newItem);
[Link](openItem);
[Link]();
[Link](exitItem);
JMenu editMenu = new JMenu("Edit");
JMenuItem cutItem = new JMenuItem("Cut");
JMenuItem copyItem = new JMenuItem("Copy");
JMenuItem pasteItem = new JMenuItem("Paste");
[Link](cutItem);
[Link](copyItem);
[Link](pasteItem);
[Link](fileMenu);
[Link](editMenu);
setJMenuBar(menuBar);
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link](0);
}
});
setTitle("Simple Menu Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
29
new SimpleMenuDemo();
}
}
Output:
30
Result:
Thus the program has been executed successfully.
31
[Link]
STUDENT DETAILS USING GUI
DATE:
AIM:
To Create an application to display Student details using GUI Components.
Algorithm:
Step 1: Start the program.
Step 2: Create the GUI window with the title "Student Details Application".
Step 3: Add input fields for:
Name
Roll Number
Course
Step 4: Add a button labeled "Display Details".
Step 5: Add a non-editable text area to display the student details.
Step 6: When the user clicks the "Display Details" button:
Read the text entered in the Name field.
Read the text entered in the Roll Number field.
Read the text entered in the Course field.
Clear the text area.
Display the collected details in the text area in the following format:
Student Details:
Name: [Name entered]
Roll No: [Roll Number entered]
Course: [Course entered]
Step 7: Keep the window open until the user closes it.
Step 8: End the program when the window is closed.
Coding:
32
import [Link].*;
import [Link].*;
import [Link].*;
public class StudentDetailsGUI extends JFrame implements ActionListener
{
private JTextField nameField, rollField, courseField;
private JButton displayButton;
private JTextArea outputArea;
public StudentDetailsGUI()
{
JLabel nameLabel = new JLabel("Name:");
JLabel rollLabel = new JLabel("Roll No:");
JLabel courseLabel = new JLabel("Course:");
nameField = new JTextField(15);
rollField = new JTextField(15);
courseField = new JTextField(15);
displayButton = new JButton("Display Details");
[Link](this);
outputArea = new JTextArea(5, 25);
[Link](false);
JPanel inputPanel = new JPanel(new GridLayout(3, 2, 5, 5));
[Link](nameLabel);
[Link](nameField);
[Link](rollLabel);
[Link](rollField);
[Link](courseLabel);
[Link](courseField);
setLayout(new BorderLayout());
add(inputPanel, [Link]);
add(displayButton, [Link]);
add(new JScrollPane(outputArea), [Link]);
setTitle("Student Details Application");
setSize(400, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
33
}
public void actionPerformed(ActionEvent e)
{
String name = [Link]();
String roll = [Link]();
String course = [Link]();
[Link]("Student Details:\n");
[Link]("Name: " + name + "\n");
[Link]("Roll No: " + roll + "\n");
[Link]("Course: " + course + "\n");
}
public static void main(String[] args)
{
new StudentDetailsGUI();
}
}
Output:
34
Result:
Thus the program has been executed successfully.
35