[Go to site: main page, start]

0% found this document useful (0 votes)
19 views24 pages

Java Programming Lab File BCA-5002P

The document outlines the lab work for the Computer Laboratory and Practical Work of Java Programming & Dynamic Webpage Design course at Maharana Pratap College of Professional Studies for BCA 3rd Year students. It includes a list of practical assignments with corresponding dates and a certificate of completion for the academic session 2025-26. The document also contains sample Java programs demonstrating various programming concepts and techniques.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views24 pages

Java Programming Lab File BCA-5002P

The document outlines the lab work for the Computer Laboratory and Practical Work of Java Programming & Dynamic Webpage Design course at Maharana Pratap College of Professional Studies for BCA 3rd Year students. It includes a list of practical assignments with corresponding dates and a certificate of completion for the academic session 2025-26. The document also contains sample Java programs demonstrating various programming concepts and techniques.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MAHARANA PRATAP COLLEGE OF PROFESSIONAL STUDIES

(Affiliated to C.S.J.M. University, Kanpur Uttar Pradesh)

LAB FILE
[Department of Computer Application]

Lab Name : Computer Laboratory and Practical Work of


Java Programming & Dynamic Webpage Design
Lab Code : BCA-5002P
Course : BCA
Semester : 5th
Faculty
: Mr. Raghvendra Singh
Name

Student Name Roll No:


SESSION 2025-26

CERTIFICATE

This is to certified that -------------------------------------- student of BCA-3rd Year

Semester – Vth, has satisfactorily completed the lab work of “Computer

Laboratory and Practical Work of Java Programming & Dynamic

Webpage Design” (BCA-5002P) prescribed by CSJMU during the

academic session 2025-26 under my supervision.

Date: Signature of the Faculty


MAHARANA PRATAP COLLEGE OF PROFESSIONAL STUDIES (KN-142)

BCA-3rd Year

Computer Laboratory &Practical Work of Java Programming & Dynamic Webpage Design (BCA-5002P)

S. No. Practical Name Date Remark


1 Construct a program to demonstrate use of different 6-8-2025
data types in Java.

2 Construct a program to find the largest among three 6-8-2025


numbers using if-else.

3 Construct a program to find the sum of array elements. 6-8-2025

4 Construct a program to perform string operations 6-8-2025


(length, uppercase, concatenate).

5 Construct a program using class and object to calculate 13-8-2025


area of rectangle.

6 Construct a program to demonstrate single inheritance. 13-8-2025

7 Construct a program to handle divide-by-zero 13-8-2025


exception.

8 Construct a program to create two threads and print 13-8-2025


numbers.

9 Construct a program to create a simple applet that 20-8-2025


displays “Welcome to Applet Programming”.

10 Construct a program to display user name passed 20-8-2025


through applet parameters.
11 Construct a program to create a window with a button 20-8-2025
and a label using AWT.

12 Construct a program to take input from user using 20-8-2025


TextField and display greeting message on button
click.

13 Construct a program to display selected item from a 10-9-2025


ComboBox (Choice).

14 Construct a program to display multiple items in a list 10-9-2025


and show selected item.

15 Construct a program to arrange components using 10-9-2025


FlowLayout.

16 Construct a program to arrange buttons in grid layout. 10-9-2025

17 Construct a program to load and register the JDBC 17-9-2025


driver for MySQL.

18 Construct a program to establish a connection between 17-9-2025


Java and MySQL database.

19 Construct a program to insert a record into the student 17-9-2025


table using Statement.

20 Construct a program to retrieve and display all student 17-9-2025


records using ResultSet.

21 Construct a program to demonstrate nested structure by 24-9-2025


Construct a program to update marks of a student using
PreparedStatement.
22 Construct a program to delete a student record based 24-9-2025
on roll number.

23 Construct a program to insert data using 24-9-2025


PreparedStatement (precompiled query).

24 Construct a program to create a basic servlet that 24-9-2025


displays a welcome message.

25 Construct a program to demonstrate the servlet 9-10-2025


lifecycle (init(), service(), destroy()).

26 Construct a program to handle an HTTP GET request 9-10-2025


and display parameters.

27 Construct a program to handle form data using the 9-10-2025


doPost() method.

28 Construct a program to store and read user data using 16-10-2025


cookies.

29 Construct a program to display a simple welcome 16-10-2025


message using JSP.

30 Construct a program to demonstrate JSP scripting 16-10-2025


elements.

31 Construct a program to display current system date and 30-10-2025


time using JSP.

32 Construct a program to use page directive for 30-10-2025


importing packages and setting properties.
33 Construct a program to read form data (name and 30-10-2025
email) using JSP.
Program 1: Construct a program to demonstrate use of different data types in
Java.
class DataTypes {
public static void main(String[] args) {
int a = 10;
float b = 5.5f;
double c = 123.456;
char d = 'J';
boolean e = true;
String name = "Java";
[Link]("Integer: " + a);
[Link]("Float: " + b);
[Link]("Double: " + c);
[Link]("Character: " + d);
[Link]("Boolean: " + e);
[Link]("String: " + name);
}
}

Program 2: Construct a program to find the largest among three numbers using
if-else.
import [Link];
class Largest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter three numbers: ");
int a = [Link](), b = [Link](), c = [Link]();
if(a > b && a > c)
[Link]("Largest = " + a);
else if(b > c)
[Link]("Largest = " + b);
else
[Link]("Largest = " + c);
}
}

Program 3: Construct a program to find the sum of array elements.


import [Link];
class ArraySum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[] arr = new int[5];
int sum = 0;
[Link]("Enter 5 elements:");
for(int i=0; i<5; i++) {
arr[i] = [Link]();
sum += arr[i];
}
[Link]("Sum = " + sum);
}
}

Program 4: Construct a program to perform string operations (length,


uppercase, concatenate).
class StringDemo {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Programming";
[Link]("Length of s1: " + [Link]());
[Link]("Uppercase: " + [Link]());
[Link]("Concatenation: " + [Link](" ").concat(s2));
}
}

Program 5: Construct a program using class and object to calculate area of


rectangle.
class Rectangle {
int length, breadth;
void input(int l, int b) {
length = l;
breadth = b;
}
int area() {
return length * breadth;
}
public static void main(String[] args) {
Rectangle r = new Rectangle();
[Link](10, 5);
[Link]("Area = " + [Link]());
}
}

Program 6: Construct a program to demonstrate single inheritance.


class Animal {
void eat() {
[Link]("Animals eat food");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
class InheritanceDemo {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}

Program 7: Construct a program to handle divide-by-zero exception.


class ExceptionExample {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int c = a / b;
[Link](c);
} catch (ArithmeticException e) {
[Link]("Error: Division by zero not allowed!");
} finally {
[Link]("Program executed safely.");
}
}
}

Program 8: Construct a program to create two threads and print numbers.


class Thread1 extends Thread {
public void run() {
for(int i=1; i<=5; i++)
[Link]("Thread 1: " + i);
}
}
class Thread2 extends Thread {
public void run() {
for(int i=1; i<=5; i++)
[Link]("Thread 2: " + i);
}
}
class MultiThreadDemo {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
[Link]();
[Link]();
}
}
Program 9: Construct a program to create a simple applet that displays
“Welcome to Applet Programming”.
import [Link];
import [Link];
/*
<applet code="[Link]" width="300" height="150"></applet>
*/
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
[Link]("Welcome to Applet Programming", 50, 80);
}
}

Program 10: Construct a program to display user name passed through applet
parameters.
import [Link];
import [Link];
/*
<applet code="[Link]" width="300" height="150">
<param name="username" value="Raghavendra">
</applet>
*/
public class ParamApplet extends Applet {
String name;
public void init() {
name = getParameter("username");
if (name == null) name = "Guest";
}
public void paint(Graphics g) {
[Link]("Welcome, " + name, 60, 80);
}
}
Program 11: Construct a program to create a window with a button and a label
using AWT.
import [Link].*;
import [Link].*;
public class ButtonLabelExample extends Frame implements ActionListener {
Label lbl;
Button btn;
ButtonLabelExample() {
lbl = new Label("Click the button");
[Link](100, 80, 200, 30);
btn = new Button("Click Me");
[Link](120, 150, 100, 30);
[Link](this);
add(lbl);
add(btn);
setSize(350, 250);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked!");
}
public static void main(String[] args) {
new ButtonLabelExample();
}
}
Program 12: Construct a program to take input from user using TextField and
display greeting message on button click.
import [Link].*;
import [Link].*;
public class GreetingApp extends Frame implements ActionListener {
TextField tf;
Label lbl;
Button btn;
GreetingApp() {
lbl = new Label("Enter your name:");
[Link](50, 80, 120, 30);
tf = new TextField();
[Link](180, 80, 120, 30);
btn = new Button("Greet");
[Link](130, 130, 80, 30);
[Link](this);
add(lbl);
add(tf);
add(btn);
setSize(350, 250);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String name = [Link]();
[Link]("Hello, " + name + "!");
}
public static void main(String[] args) {
new GreetingApp();
}
}

Program 13: Construct a program to display selected item from a ComboBox


(Choice).
import [Link].*;
import [Link].*;
public class ComboBoxExample extends Frame implements ItemListener {
Choice choice;
Label lbl;
ComboBoxExample() {
lbl = new Label("Select a language:");
[Link](50, 80, 150, 30);
choice = new Choice();
[Link](200, 80, 120, 30);
[Link]("C");
[Link]("C++");
[Link]("Java");
[Link]("Python");
[Link](this);
add(lbl);
add(choice);
setSize(400, 250);
setLayout(null);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
[Link]("Selected: " + [Link]());
}
public static void main(String[] args) {
new ComboBoxExample();
}
}

Program 14: Construct a program to display multiple items in a list and show
selected item.
import [Link].*;
import [Link].*;
public class ListExample extends Frame implements ActionListener {
List list;
Label lbl;
Button btn;
ListExample() {
lbl = new Label("Select an item:");
[Link](50, 60, 120, 30);
list = new List(4);
[Link](180, 60, 100, 80);
[Link]("Red");
[Link]("Green");
[Link]("Blue");
[Link]("Yellow");
btn = new Button("Show Selected");
[Link](120, 160, 120, 30);
[Link](this);
add(lbl);
add(list);
add(btn);
setSize(350, 250);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String item = [Link]();
[Link]("Selected: " + item);
}
public static void main(String[] args) {
new ListExample();
}
}

Program 15: Construct a program to arrange components using FlowLayout.


import [Link].*;
public class FlowLayoutExample extends Frame {
FlowLayoutExample() {
setLayout(new FlowLayout());
add(new Button("One"));
add(new Button("Two"));
add(new Button("Three"));
add(new Button("Four"));
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new FlowLayoutExample();
}
}
Program 16: Construct a program to arrange buttons in grid layout.
import [Link].*;
public class GridLayoutExample extends Frame {
GridLayoutExample() {
setLayout(new GridLayout(2, 3)); // 2 rows, 3 columns
for(int i=1; i<=6; i++)
add(new Button("Button " + i));

setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new GridLayoutExample();
}
}

Program 17: Construct a program to load and register the JDBC driver for
MySQL.
import [Link].*;
class LoadDriver {
public static void main(String[] args) {
try {
[Link]("[Link]");
[Link]("JDBC Driver Loaded Successfully.");
} catch (ClassNotFoundException e) {
[Link]("Driver not found: " + e);
}
}
}

Program 18: Construct a program to establish a connection between Java and


MySQL database.
import [Link].*;
class DBConnection {
public static void main(String[] args) {
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/studentdb", "root", "yourpassword");
[Link]("Database Connected Successfully!");
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}

Program 19: Construct a program to insert a record into the student table using
Statement.
import [Link].*;
import [Link].*;
class InsertRecord {
public static void main(String[] args) {
try {
Scanner sc = new Scanner([Link]);
[Link]("Enter Roll, Name, Marks: ");
int roll = [Link]();
String name = [Link]();
float marks = [Link]();
Connection con = [Link](
"jdbc:mysql://localhost:3306/studentdb", "root", "yourpassword");
Statement st = [Link]();
String sql = "INSERT INTO student VALUES (" + roll + ", '" + name +
"', " + marks + ")";
[Link](sql);
[Link]("Record Inserted Successfully!");
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}

Program 20: Construct a program to retrieve and display all student records
using ResultSet.
import [Link].*;
class DisplayRecords {
public static void main(String[] args) {
try {
Connection con = [Link](
"jdbc:mysql://localhost:3306/studentdb", "root", "yourpassword");
Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM student");
[Link]("Roll\tName\tMarks");
while ([Link]()) {
[Link]([Link](1) + "\t" + [Link](2) + "\t" +
[Link](3));
}

[Link]();
} catch (Exception e) {
[Link](e);
}
}
}

Program 21: Construct a program to update marks of a student using


PreparedStatement.
import [Link].*;
import [Link].*;
class UpdateRecord {
public static void main(String[] args) {
try {
Scanner sc = new Scanner([Link]);
[Link]("Enter Roll and New Marks: ");
int roll = [Link]();
float marks = [Link]();
Connection con = [Link](
"jdbc:mysql://localhost:3306/studentdb", "root", "yourpassword");
PreparedStatement ps = [Link](
"UPDATE student SET marks = ? WHERE roll = ?");
[Link](1, marks);
[Link](2, roll);
[Link]();
[Link]("Record Updated Successfully!");
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}

Program 22: Construct a program to delete a student record based on roll


number.
import [Link].*;
import [Link].*;

class DeleteRecord {
public static void main(String[] args) {
try {
Scanner sc = new Scanner([Link]);
[Link]("Enter Roll Number to Delete: ");
int roll = [Link]();
Connection con = [Link](
"jdbc:mysql://localhost:3306/studentdb", "root", "yourpassword");
PreparedStatement ps = [Link]("DELETE FROM student
WHERE roll = ?");
[Link](1, roll);
[Link]();
[Link]("Record Deleted Successfully!");
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}

Program 23: Construct a program to insert data using PreparedStatement


(precompiled query).
import [Link].*;
import [Link].*;
class PreparedInsert {
public static void main(String[] args) {
try {
Connection con = [Link](
"jdbc:mysql://localhost:3306/studentdb", "root", "yourpassword");
PreparedStatement ps = [Link]("INSERT INTO student
VALUES (?, ?, ?)");
Scanner sc = new Scanner([Link]);
[Link]("Enter Roll, Name, Marks: ");
int roll = [Link]();
String name = [Link]();
float marks = [Link]();
[Link](1, roll);
[Link](2, name);
[Link](3, marks);
[Link]();
[Link]("Record Inserted Successfully Using
PreparedStatement!");
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}

Program 24: Construct a program to create a basic servlet that displays a


welcome message.
import [Link].*;
import [Link].*;
import [Link].*;
public class WelcomeServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h2>Welcome to Java Servlets!</h2>");
[Link]();
}
}
[Link] Entry:
<web-app>
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>

Program 25: Construct a program to demonstrate the servlet lifecycle (init(),


service(), destroy()).
import [Link].*;
import [Link].*;
import [Link].*;
public class LifeCycleServlet extends HttpServlet {
public void init() throws ServletException {
[Link]("Servlet Initialized!");
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h3>Servlet is in Service Phase</h3>");
}
public void destroy() {
[Link]("Servlet Destroyed!");
}
}

Program 26: Construct a program to handle an HTTP GET request and display
parameters.
import [Link].*;
import [Link].*;
import [Link].*;
public class GetExample extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();

String name = [Link]("name");


[Link]("<h3>Hello, " + name + "! This is a GET request.</h3>");
[Link]();
}
}
HTML form to call servlet:
<form action="GetExample" method="get">
Enter Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>

Program 27: Construct a program to handle form data using the doPost()
method.
import [Link].*;
import [Link].*;
import [Link].*;
public class PostExample extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String name = [Link]("name");
String city = [Link]("city");
[Link]("<h3>Name: " + name + "</h3>");
[Link]("<h3>City: " + city + "</h3>");
[Link]();
}
}
HTML form:
<form action="PostExample" method="post">
Name: <input type="text" name="name"><br>
City: <input type="text" name="city"><br>
<input type="submit" value="Send">
</form>

Program 28: Construct a program to store and read user data using cookies.
import [Link].*;
import [Link].*;
import [Link].*;
public class CookieExample extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
Cookie user = new Cookie("username", "Raghav");
[Link](user);
[Link]("<h3>Cookie added successfully!</h3>");
}
}
To Read Cookie (Second Servlet):
import [Link].*;
import [Link].*;
import [Link].*;

public class ReadCookie extends HttpServlet {


public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
Cookie[] ck = [Link]();
for(Cookie c : ck) {
[Link]("<h4>" + [Link]() + " = " + [Link]() + "</h4>");
}
}
}

Program 29: Construct a program to display a simple welcome message using


JSP.
<%@ page contentType="text/html" %>
<html>
<body>
<h2>Welcome to Java Server Pages!</h2>
</body>
</html>

Program 30: Construct a program to demonstrate JSP scripting elements.


<%@ page contentType="text/html" %>
<html>
<body>
<%! int a = 10, b = 20; %> <!-- Declaration -->
<%
int sum = a + b; // Scriptlet
%>
<h3>Sum using Expression: <%= sum %></h3>
</body>
</html>

Program 31: Construct a program to display current system date and time using
JSP.
<%@ page import="[Link].*" %>
<html>
<body>
<h3>Current Date and Time: <%= new Date() %></h3>
</body>
</html>

Program 32 Construct a program to use page directive for importing packages


and setting properties.
<%@ page language="java" contentType="text/html" import="[Link]"
%>
<html>
<body>
<h3>Today’s Date: <%= new Date() %></h3>
<p>This page uses JSP page directive to import the [Link] package.</p>
</body>
</html>

Program 33: Construct a program to read form data (name and email) using
JSP.
HTML Form ([Link]):
<form action="[Link]" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
[Link]:
<%@ page contentType="text/html" %>
<html>
<body>
<%
String name = [Link]("name");
String email = [Link]("email");
%>
<h3>Hello <%= name %>!</h3>
<p>Your Email: <%= email %></p>
</body>
</html>

You might also like