JAVA PROGRAMMIMG PRACTICAL LAB FILE
BCA IV – (405)
Summited To: - Summited By: - Samar Pratap
Ass. Prof. Ashish Yadav Singh Rajawat
Roll no: - 39 (B)
i
DECLARATION
I, student of BCA IV Sem. of Prestige Institute of Management & Research,
Gwalior, hereby declare that the Java Programming Practical Lab File is submitted by
me in the line of partial fulfilment of course objectives for the Bachelor of Computer
Application.
I assure that this practical lab file is the result of my own efforts and that any other
institute for the award of any degree or diploma has not submitted it.
Date: Name of the Students
Samar Pratap Singh Rajawat
ii
CERTIFICATE
This is to certify that, Samar Pratap Singh Rajawat of BCA IV Semester of Prestige Institute
of Management & Research Gwalior, have successfully completed their Java Programming
Practical Lab File. They have prepared this Lab Report under my direct supervision and
guidance.
Date: Name of the faculty Guide
Mr. Ashish Yadav
iii
ACKNOWLEDGEMENT
“Gratitude is the hardest of all emotions to express. There is no word capable of conveying all
that one feels until I reach the world where thoughts can be adequately expressed in words,”
Thank you” will have to do. “I would also like to thank Dr. Nirmalya Bandyopadhyay,
Director of PIMRG for his guidance and moral support in our academic persuasion and
providing insight to this topic. I would also like to thank my college for supporting me with
resources, which beyond any doubt have helped me. It is a great pleasure for me to put on records
my appreciation and gratitude towards Ass. Prof Ashish Yadav for his valuable support and
suggestions for the improvement and editing of this practical lab file. I would like to thank all the
faculty members of Prestige Institute of Management Gwalior, for providing me the required
knowledge, information and support.
Name of the student
Samar Pratap Singh Rajawat
iv
INDEX
[Link] Title Page No.
1a Java Program To Print 1
1b JDK & JRE Practical / Theory 1
2a Area of Rectangle Calculation 2
2b Celsius to Fahrenheit 2
Converter
3a Even or Odd check using if- 3
else
3b Calculator using switch-case 3
4 Object Creation: Student 4
Class
5a Base Class Shape - Area & 5
Perimeter
5b Inheritance: Rectangle & 6
Circle
6a Interface: Drawable 7
6b Abstract Class: Vehicle 7
7a Packages: 8
[Link]
7b Access Modifiers 9
Demonstration
8a Reverse String using 10
StringBuilder
8b Count word occurrences in 10
sentence
9a ArrayList: Student Name 11
Management
9b HashMap: Student ID & 11
Names
10a Handling Arithmetic & 12
NullPointer Exception
10b Custom Exception: 12
InvalidAgeException
11 AWT Basics: Simple GUI 13-15
Application
12a Swing: Advanced GUI with 16-25
Layouts
v
Practical 1
a. Write a Java program to print 'Hello World!'
Code:
class Hello {
public static void main(String[] args){
[Link]("Hello World!");
[Link]("Name: Samar Rajawat");
}
}
b. Ensure JDK is correctly installed and configured in your IDE.
JDK (Java Development Kit) is a complete package used for developing Java programs.
It
contains:
JRE (Java Runtime Environment)
Compiler (javac)
Tools like debugger, archiver, etc.
JDK = Everything needed to write + compile + run Java programs
What is JRE?
JRE (Java Runtime Environment) is used to run Java programs only. It contains:
JVM (Java Virtual Machine)
Libraries and supporting files
JRE = Only needed to run Java programs (not for development)Difference Between JDK
and JRE
1
Practical 2. Variables, Data Types, and Operators
a. Write a program to calculate the area of a rectangle given its length and width.
Code:
class Rect {
public static void main(String[] args){
int l=6,w=3;
[Link]("Area="+(l*w));
[Link]("Name: Samar Rajawat");
}
}
b. Implement a temperature converter program that converts Celsius to Fahrenheit.
Code:
class Temp {
public static void main(String[] args){
int c=30;
float f=(c*9/5)+32;
[Link]("F="+f);
[Link]("Name: Samar Rajawat");
}
}
Output:
2
Practical 3. Control Statements
a. Create a program that checks whether a given number is even or odd using if-else
statements.
Code:
class EO {
public static void main(String[] args){
int n=8;
if(n%2==0) [Link]("Even");
else [Link]("Odd");
[Link]("Name: Samar Rajawat");
}
}Output:
b. Implement a calculator program using switch-case statements for basic arithmetic
operations.
Code:
class Calc {
public static void main(String[] args){
int a=10,b=2;
char op='*';
switch(op){
case '+': [Link](a+b); break;
case '-': [Link](a-b); break;
case '*': [Link](a*b); break;
case '/': [Link](a/b); break;
}
[Link]("Name: Samar Rajawat");
}
}
Output:
3
Practical 4. Object Creation
Create a class Student with attributes such as name, age, and grade. Write methods to
set and get these attributes and demonstrate object creation.
Code:
class Student {
String name="Samar Rajawat";
int age=21;
void show(){
[Link](name+" "+age);
}
public static void main(String[] args){
new Student().show();
}
}
Output:
4
Practical 5. Inheritance and Polymorphism
a. Define a base class Shape with methods to calculate area and perimeter.
Code:
class Shape {
void area() {
[Link]("Area");
void perimeter() {
[Link]("Perimeter");
public class Main {
public static void main(String[] args) {
Shape s = new Shape();
[Link]();
[Link]();
} Output:
5
b. Create derived classes Rectangle and Circle that inherit from Shape and override
these methods.
Code:
class Rectangle extends Shape {
void area(){[Link]("Rectangle Area");}
}
class Circle extends Shape {
void area(){[Link]("Circle Area");}
}
class Test {
public static void main(String[] args){
new Rectangle().area();
new Circle().area();
[Link]("Name: Samar Rajawat");
}
}
Output:
6
Practical 6. Interfaces and Abstract Classes
a. Define an interface Drawable with a method draw() and implement it in classes
like Circle, Rectangle, etc.
Code:
interface Drawable {
void draw();
}
class Rect implements Drawable {
public void draw(){[Link]("Rectangle");}
}
class Test {
public static void main(String[] args){
new Rect().draw();
[Link]("Name: Samar Rajawat");
}
} Output:
b. Create an abstract class Vehicle with abstract methods like start() and stop(), and
implement it in derived classes Car and Motorcycle.
Code:
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start(){[Link]("Car Start");}
}
class Test {
public static void main(String[] args){
new Car().start();
[Link]("Name: Samar Rajawat");
} } Output:
7
Practical 7. Packages and Access Modifiers
a. Create a package [Link] and move the Rectangle and Circle classes
into it.
Code:
package [Link];
public class Rectangle {
public void area() {
[Link]("Area of Rectangle");
public void perimeter() {
[Link]("Perimeter of Rectangle");
// Circle class
public class Circle {
public void area() {
[Link]("Area of Circle");
public void perimeter() {
[Link]("Perimeter of Circle");
} }
b. Demonstrate usage of access modifiers (public, private, protected, default) across
classes.
Access Modifiers in Java control the visibility of variables and methods.
Types:
8
private → accessible only within the same class
public → accessible from anywhere
protected → accessible within same package + subclasses
default → accessible within the same package
Code:
class Demo {
private int x = 5; // private variable
public int y = 10; // public variable
protected int z = 15; // protected variable
int a = 20; // default variable
public static void main(String[] args) {
Demo d = new Demo();
[Link](d.x);
[Link](d.y);
[Link](d.z);
[Link](d.a);
[Link]("Name: Samar Rajawat");
Output:
9
Practical 8. Working with Strings
a. Write a program to reverse a given string using StringBuffer or StringBuilder.
Code:
class Rev {
public static void main(String[] args){
String s="Java";
[Link](new StringBuilder(s).reverse());
[Link]("Name: Samar Rajawat");
}
} Output:
b. Implement a program to count occurrences of a specific word in a sentence using
String methods.
Code:
class Count {
public static void main(String[] args){
String s="hello hello";
int count=[Link]("hello",-1).length-1;
[Link](count);
[Link]("Name: Samar Rajawat");
}
}
Output:
10
Practical 9. Collections Framework
a. Create a program to manage a list of student names using ArrayList.
Code:
import [Link].*;
class A {
public static void main(String[] args) {
ArrayList<String> l = new ArrayList<>();
// Adding student names
[Link]("Ram");
[Link]("Shyam");
// Displaying the list
[Link](l);
[Link]("Name: Samar Rajawat");
} Output:
b. Implement a program using HashMap to store student IDs and names, and perform
basic operations like adding, retrieving, and iterating.
Code:
import [Link].*;
class H {
public static void main(String[] args){
HashMap<Integer,String> m=new HashMap<>();
[Link](1,"A");
[Link](m);
[Link]("Name: Samar Rajawat");}}
Output:
11
Practical 10. Exception Handling
a. Write a program to handle ArithmeticException and NullPointerException.
Code:
class Ex {
public static void main(String[] args){
try{int a=10/0;}
catch(Exception e){[Link]("Error");}
[Link]("Name: Samar Rajawat");}}
Output:
b. Create a custom exception InvalidAgeException and use it in a program to validate
age.
Code:
class MyEx extends Exception{}
class Test{
public static void main(String[] args){
try{throw new MyEx();}
catch(Exception e){[Link]("Custom Exception");}
[Link]("Name: Samar Rajawat");}}
Output:
12
Practical 11. AWT Basics
Develop a simple GUI application using Frame, Button, and Label to perform basic
operations (e.g., calculator).
import [Link].*;
import [Link].*;
public class SimpleCalculator extends Frame implements ActionListener {
Label l1, l2, result;
TextField t1, t2;
Button add, sub, mul, div;
SimpleCalculator() {
setLayout(new FlowLayout());
l1 = new Label("Enter First Number:");
t1 = new TextField(10);
l2 = new Label("Enter Second Number:");
t2 = new TextField(10);
add = new Button("Add")
sub = new Button("Subtract");
mul = new Button("Multiply");
div = new Button("Divide");
result = new Label("Result: ");
add(l1); add(t1);
add(l2); add(t2);
add(add); add(sub); add(mul); add(div);
add(result);
[Link](this);
13
[Link](this);
[Link](this);
[Link](this);
setTitle("Simple Calculator");
setSize(300, 250);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose(); } }); }
public void actionPerformed(ActionEvent e) {
try {
double num1 = [Link]([Link]());
double num2 = [Link]([Link]());
double res = 0;
if ([Link]() == add) {
res = num1 + num2;
} else if ([Link]() == sub) {
res = num1 - num2;
} else if ([Link]() == mul) {
res = num1 * num2;
} else if ([Link]() == div) {
res = num1 / num2; }
[Link]("Result: " + res);
} catch (Exception ex) {
[Link]("Invalid Input!");}}
14
public static void main(String[] args) {
new SimpleCalculator; }
Output:
15
Practical 12. Java Swing
a. Create a more advanced GUI application using JFrame, JPanel, JButton, and
JTextField.
import [Link].*;
import [Link].*;
import [Link].*;
public class main extends JFrame {
private JPanel mainPanel, inputPanel, buttonPanel, displayPanel;
private JTextField nameField, emailField, ageField, resultField;
private JButton addButton, clearButton, submitButton;
private JTextArea outputArea;
private JLabel titleLabel, nameLabel, emailLabel, ageLabel;
public main() {
// Frame settings
setTitle("Advanced User Information System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 500);
setLocationRelativeTo(null);
setResizable(true);
// Main panel with BorderLayout
mainPanel = new JPanel(new BorderLayout(10, 10));
[Link]([Link](15, 15, 15, 15));
16
[Link](new Color(240, 240, 240));
// Title label
titleLabel = new JLabel("User Registration Form");
[Link](new Font("Arial", [Link], 24));
[Link]([Link]);
[Link](new Color(0, 102, 204));
[Link](titleLabel, [Link]);
// Input panel with GridLayout
inputPanel = new JPanel(new GridLayout(3, 2, 10, 10));
[Link](new Color(240, 240, 240));
// Name field
nameLabel = new JLabel("Name:");
[Link](new Font("Arial", [Link], 12));
nameField = new JTextField();
[Link](new Font("Arial", [Link], 12));
[Link](nameLabel);
[Link](nameField);
// Email field
emailLabel = new JLabel("Email:");
[Link](new Font("Arial", [Link], 12));
emailField = new JTextField();
17
[Link](new Font("Arial", [Link], 12));
[Link](emailLabel);
[Link](emailField);
// Age field
ageLabel = new JLabel("Age:");
[Link](new Font("Arial", [Link], 12));
ageField = new JTextField();
[Link](new Font("Arial", [Link], 12));
[Link](ageLabel);
[Link](ageField);
[Link](inputPanel, [Link]);
// Button panel
buttonPanel = new JPanel(new FlowLayout([Link], 10, 10));
[Link](new Color(240, 240, 240));
addButton = new JButton("Add User");
[Link](new Font("Arial", [Link], 12));
[Link](new Color(0, 153, 76));
[Link]([Link]);
[Link](false);
[Link](new Cursor(Cursor.HAND_CURSOR));
[Link](new ActionListener() {
18
@Override
public void actionPerformed(ActionEvent e) {
addUser();
});
clearButton = new JButton("Clear");
[Link](new Font("Arial", [Link], 12));
[Link](new Color(255, 102, 0));
[Link]([Link]);
[Link](false);
[Link](new Cursor(Cursor.HAND_CURSOR));
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearFields();
});
submitButton = new JButton("Submit");
[Link](new Font("Arial", [Link], 12));
[Link](new Color(0, 102, 204));
[Link]([Link]);
[Link](false);
[Link](new Cursor(Cursor.HAND_CURSOR));
19
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
submitForm();
});
[Link](addButton);
[Link](clearButton);
[Link](submitButton);
[Link](buttonPanel, [Link]);
// Display panel with JTextArea
displayPanel = new JPanel(new BorderLayout(10, 10));
[Link](new Color(220, 220, 220));
[Link]([Link]("User Records"));
outputArea = new JTextArea();
[Link](false);
[Link](new Font("Courier New", [Link], 11));
[Link]([Link]);
[Link](true);
[Link](true);
20
JScrollPane scrollPane = new JScrollPane(outputArea);
[Link](new Dimension(550, 150));
[Link](scrollPane, [Link]);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, mainPanel,
displayPanel);
[Link](0.6);
add(splitPane);
setVisible(true);
private void addUser() {
String name = [Link]().trim();
String email = [Link]().trim();
String age = [Link]().trim();
if ([Link]() || [Link]() || [Link]()) {
[Link](this, "Please fill all fields!", "Input Error",
JOptionPane.ERROR_MESSAGE);
return;
try {
int ageValue = [Link](age);
if (ageValue < 0 || ageValue > 150) {
21
[Link](this, "Please enter a valid age!", "Input
Error", JOptionPane.ERROR_MESSAGE);
return;
} catch (NumberFormatException e) {
[Link](this, "Age must be a number!", "Input Error",
JOptionPane.ERROR_MESSAGE);
return;
String record = [Link]("Name: %-20s | Email: %-25s | Age: %-3s\n", name,
email, age);
[Link](record);
[Link](this, "User added successfully!", "Success",
JOptionPane.INFORMATION_MESSAGE);
clearFields();
private void clearFields() {
[Link]("");
[Link]("");
[Link]("");
[Link]();
private void submitForm() {
22
int count = [Link]().split("\n").length - 1;
if (count == 0) {
[Link](this, "No users added yet!", "Info",
JOptionPane.INFORMATION_MESSAGE);
} else {
[Link](this, "Total users registered: " + count,
"Submission Report", JOptionPane.INFORMATION_MESSAGE);
public static void main(String[] args) {
[Link](new Runnable() {
@Override
public void run() {
new main();
});
Output:
23
b. Use different layout managers (FlowLayout, BorderLayout, GridLayout) to organize
components.
Code:
import [Link].*;
import [Link].*;
import [Link].*;
public class MultiLayoutCalculator {
public static void main(String[] args) {
JFrame frame = new JFrame("Multi Layout Calculator");
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new BorderLayout());
JPanel topPanel = new JPanel();
[Link](new FlowLayout());
JLabel label = new JLabel("Calculator");
JTextField display = new JTextField(20);
[Link](label);
[Link](display);
[Link](topPanel, [Link]);
JPanel centerPanel = new JPanel();
[Link](new GridLayout(4, 4, 5, 5));
String[] buttons = {
"7","8","9","/",
"4","5","6","*",
"1","2","3","-",
"0","C","=","+" };
24
for (String text : buttons) {
JButton btn = new JButton(text);
[Link](btn);
[Link](centerPanel, [Link]);
JPanel bottomPanel = new JPanel();
[Link](new FlowLayout());
JButton clearBtn = new JButton("Clear");
JButton exitBtn = new JButton("Exit");
[Link](clearBtn);
[Link](exitBtn);
[Link](bottomPanel, [Link]);
[Link](e -> [Link](0));
[Link](e -> [Link](""));
[Link](true); }
Output:
25