Advanced Java Programming Lab Report
Advanced Java Programming Lab Report
SIGMA UNIVERSITY
CERTIFICATE
1
Enrollment No: -23DIT0055 Advanced Java Programming
Branch: Semester :
6 Develop a program that has only one button in the frame, clicking
on the button cycles through the colors: red->green- >blue and so
on. One color changes per click.(use getBackGround() method to
get the current color)
2
Enrollment No: -23DIT0055 Advanced Java Programming
16 Develop a simple JSP program for user login form with static and
dynamic database.
Submitted by :
3
Enrollment No: -23DIT0055 Advanced Java Programming
Submitted to :
Faculty Name : Bhumi Ataliya HOD Name : Kalyani Adawadkar
4
5
Enrollment No:23DIT0055 Advanced Java Programming
Practical-1
Aim :- Develop an applet that draws a circle. The dimension of the applet should be
500 x 300 pixels. The circle should be centered in the applet and have a radius of 100
pixels. Display your name centered in a circle.( using drawOval() method)
import [Link].*;
import
[Link].*;
public class CircleSwing extends
JPanel {
@Override public void
paintComponent(Graphics g) {
[Link](g);
int width =
1000; int
height = 1000;
int radius = 100;
// Calculate
center int
centerX = width / 5;
int centerY = height / 5;
// Draw circle
[Link](centerX - radius, centerY - radius, 2
* radius, 2 * radius);
// Draw name inside the circle
[Link](new Font("Arial", [Link], 16));
[Link]("Ansh", centerX - 35, centerY + 5); //
adjust for centering
} public static void main(String[] args)
{ JFrame frame = new JFrame("Centered
Circle"); CircleSwing panel = new
CircleSwing();
[Link](500,
300);
6
Enrollment No:23DIT0055 Advanced Java Programming
[Link](JFrame.EXIT_ON_CLOSE);
[Link](panel); [Link](true);
}
}
Output :
7
Enrollment No:23DIT0055 Advanced Java Programming
Practical-2
Aim :- Draw ten red circles in a vertical column in the center of the applet
import [Link].*;
import
[Link].*;
public class Circles extends
JPanel
{
@Override
public void paintComponent(Graphics g){
[Link](g);
[Link]([Link]); [Link](275, 50,
50, 50);
[Link]([Link]);
[Link](275, 105, 50, 50);
[Link]([Link]);
[Link](275, 160, 50, 50);
[Link]([Link]);
[Link](275, 215, 50, 50);
[Link]([Link]);
[Link](275, 270, 50, 50);
[Link]([Link]);
[Link](275, 325, 50, 50);
[Link]([Link]);
[Link](275, 380, 50, 50);
[Link]([Link]);
[Link](275, 434, 50, 50);
[Link]([Link]);
[Link](275, 490, 50, 50);
[Link]([Link]);
[Link](275, 545, 50, 50);
} public static void
main(String[] args) { Circles c =
8
Enrollment No:23DIT0055 Advanced Java Programming
Output :
9
Enrollment No:23DIT0055 Advanced Java Programming
Practical-3
Aim :- Built an applet that displays a horizontal rectangle in its center. Let the rectangle
fill with color from left to right.
import [Link].*;
import
[Link].*;
public class Rectangle
extends JPanel { public
void paintComponent(Graphics
g){
[Link](g);
[Link]([Link]);
[Link](175, 100, 250, 100);
}
public static void main(String[] args) {
Rectangle r = new
Rectangle(); JFrame f = new
JFrame();
[Link]("Shapes");
[Link](400,600);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](r);
}
}
Output :
10
Enrollment No:23DIT0055 Advanced Java Programming
Practical-4
Aim : Develop an swing that display the position of the mouse at the upper left corner
of the applet when it is dragged or moved. Draw a 10x10 pixel rectangle filled with
black at the current mouse position.
setVisible(true);
} class DrawingPanel extends
JPanel implements
MouseMotionListener {
public DrawingPanel() {
addMouseMotionListener(this);
}
@Override
protected void
paintComponent(Graphics g) {
[Link](g);
// Show mouse position at top-left
[Link]([Link]);
[Link]("Mouse at (" + mouseX + ", " + mouseY
+
")", 10, 20);
// Draw black 10x10 square at mouse position
11
Enrollment No:23DIT0055 Advanced Java Programming
Output :
12
Enrollment No:23DIT0055 Advanced Java Programming
Practical-5
Aim : Develop an swing that uses the mouse listener, which overrides only two
methods which are mousePressed and mouseReleased.
[Link](true);
}
}
13
Enrollment No:23DIT0055 Advanced Java Programming
Output :
14
Enrollment No:23DIT0055 Advanced Java Programming
Practical-6
Aim : Develop a program that has only one button in the frame, clicking on the button
cycles through the colors: red->green- >blue and so on. One color changes per click.(use
getBackGround() method to get the current color)
15
Enrollment No:23DIT0055 Advanced Java Programming
[Link]().setBackground([Link]);
}
}
});
[Link](button);
}
}
Output :
Then, when I press the button(Change Color) the panel changes it’s color to RED
Then if I again press the button the panel changes it’s color to GREEN
16
Enrollment No:23DIT0055 Advanced Java Programming
And if I press the button present on the panel for the third time it changes it’s color to
BLUE
But when I again press the button it changes panel’s color to RED as per our code.
17
Enrollment No:23DIT0055 Advanced Java Programming
Practical-7
Aim : Develop an program that contains three check boxes and 30 x 30 pixel
[Link] three checkboxes should be labeled “Red”, “Green”,”Blue”. The selection of
the check boxes determine the color of the canvas. For example, if the user selects both
“Red” and “Blue”, the canvas should be purple.
18
Enrollment No:23DIT0055 Advanced Java Programming
[Link](colorchange);
[Link](colorchange);
[Link](colorchange);
[Link](red);
[Link](blue);
[Link](green);
[Link](panel);
[Link](true);
}
}
Output :
When I run the code the initial Frame that pops up will be as follows:
Then, If I select the Red checkbox the color RED appears on the preferred panel(30x30)
After that when I remove my selection of red and select on the blue checkbox the BLUE
color appears on the preferred panel
19
Enrollment No:23DIT0055 Advanced Java Programming
At last when I again remove my selection and go for the Green checkbox the GREEN
color will appear on the preferred panel.
After removing all my selection the preferred panel becomes BLACK as we have not set
the panel Background at the end.
From this you can select multiple checkbox and create new colors too, For Example:
20
Enrollment No:23DIT0055 Advanced Java Programming
21
Enrollment No:23DIT0055 Advanced Java Programming
Practical 8
Aim : Create an application that displays a frame with a menu bar. When a user selects
any menu or menu item, display that selection on a text area in the center of the frame.
22
Enrollment No:23DIT0055 Advanced Java Programming
setVisible(true);
} public void
actionPerformed(ActionEvent e) {
String cmd = [Link]();
if ([Link]("Exit")) [Link](0);
else [Link]("You selected: " +
cmd); } public static void
main(String[] args) { new
Practical8();
}
}
Output :
23
Enrollment No:23DIT0055 Advanced Java Programming
24
Enrollment No:23DIT0055 Advanced Java Programming
Practical 9
Aim : Develop a program that draws two sets of ever-decreasing rectangles — one in
outline form and one filled alternately in black and white.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Practical9 extends
JPanel {
@Override
protected void paintComponent(Graphics g) {
[Link](g);
int
x1 = 40;
int y1 = 40;
int width1 =
200;
int height1 =
120;
for (int i =
0; i < 5; i++)
{
[Link]([Link]);
[Link](x1, y1, width1, height1);
x1 += 10; y1 += 10; width1 -
= 20;
height1 -= 20;
} int x2 =
280; int y2 =
40; int width2 =
200; int
height2 = 120;
for (int i = 0; i < 5;
i++) { if
25
Enrollment No:23DIT0055 Advanced Java Programming
(i % 2 == 0) {
[Link]([Link]);
} else {
[Link]([Link]);
}
[Link](x2, y2, width2,
height2); x2 += 10;
y2 += 10; width2 -= 20;
height2 -= 20;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
[Link](JFrame.EXIT_ON_CLO
SE);
[Link](drawingPanel);
[Link]();
[Link](null);
[Link](true);
}
}
Output :
26
Enrollment No:23DIT0055 Advanced Java Programming
Practical 10
import [Link].*;
class Practical_10 { public static
void main(String args[]) { try {
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/testdb
"
, "root", "root1234");
Statement st = [Link]();
[Link]("CREATE TABLE IF NOT EXISTS student(id
INT, name VARCHAR(30))");
[Link]("INSERT INTO student VALUES(1,'Ansh')");
ResultSet rs = [Link]("SELECT * FROM
student"); while ([Link]()) {
[Link]([Link](1) + " " +
[Link](2));
}
[Link]();
[Link]("Done");
} catch (Exception e) {
[Link](e);
}
}
}
Output :
27
Enrollment No:23DIT0055 Advanced Java Programming
28
Enrollment No:23DIT0055 Advanced Java Programming
Practical 11
Aim : Develop a Graphical User Interface that performs the following SQL
operations: a) Insert b) Delete c)Update.
add(b1);
add(b2); add(b3);
29
Enrollment No:23DIT0055 Advanced Java Programming
[Link](this);
[Link](this);
[Link](this); try
{
[Link]("[Link]");
con =
[Link]("jdbc:mysql://localhost:3306/mydb",
"root", "root1234");
st = [Link]();
[Link]("CREATE TABLE IF NOT EXISTS student(id
INT PRIMARY
KEY, name VARCHAR(30))");
} catch (Exception e) {
[Link](e);
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void
actionPerformed(ActionEvent ae) {
try { int id =
[Link]([Link]());
String name = [Link]();
if ([Link]() ==
b1) {
[Link]("INSERT INTO student
VALUES(" + id + ",'" + name +
"')");
[Link](this, "Record
Inserted");
} else if ([Link]() == b2) {
[Link]("UPDATE student SET name='" + name + "' WHERE
id=" + id);
[Link](this, "Record
Updated");
} else if ([Link]() == b3) {
[Link]("DELETE FROM student WHERE
id="
30
Enrollment No:23DIT0055 Advanced Java Programming
+ id); [Link](this,
"Record Deleted");
}
} catch (Exception e) {
[Link](this,
[Link]());
}
} public static void
main(String args[]) { new
Practical_11();
}
}
Output :
Record Updated where ID = 1
31
Enrollment No:23DIT0055 Advanced Java Programming
Practical 12
Aim : Develop a program to present a set of choice for user to select a product
and display the price of product.
add(p1);
add(p2); add(p3);
show = new JButton("Show
Price");
result = new JLabel("Price will appear here.");
add(show); add(result);
[Link](this);
32
Enrollment No:23DIT0055 Advanced Java Programming
setDefaultCloseOperation(EXIT_ON_CLOSE)
; setVisible(true);
} public void
actionPerformed(ActionEvent e) { if
([Link]())
[Link]("Price: ₹15,000"); else
if
([Link]())
[Link]("Price: ₹55,000"); else if
([Link]())
[Link]("Price: ₹2,000");
else [Link]("Please select a
product.");
} public static void
main(String args[]) { new
Practical_12();
}
}
Output :
33
Enrollment No:23DIT0055 Advanced Java Programming
Practical 13
Aim : Develop a simple servlet program which maintains a counter for the
number of times it has been accessed since its loading, initialize the counter
using deployment descriptor
getServletContext().setAttribute("accessCount",
initialCount);
[Link]("Application Counter initialized to:
" + initialCount);
}
34
Enrollment No:23DIT0055 Advanced Java Programming
currentCount++;
getServletContext().setAttribute("accessCount",
currentCount);
}
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<!DOCTYPE
html>");
[Link]("<html>");
[Link]("<body>");
[Link]("<h1>Application Access Counter</h1>");
[Link]("<p>Total times this application has been
accessed: <strong>" + currentCount + "</strong></p>");
[Link]("<p>The counter started from the value defined in
[Link].</p>"); [Link]("</body>");
[Link]("</html>");
}
}
Output :
35
Enrollment No:23DIT0055 Advanced Java Programming
36
Enrollment No:23DIT0055 Advanced Java Programming
Practical 14
Aim : Create a web form which processes servlet and demonstrates use of
cookies and sessions.
[Link]("session_user", userName);
Integer visitCount = (Integer)
[Link]("visit_count"); if
(visitCount == null) { visitCount = 1;
} else {
visitCount++;
}
37
Enrollment No:23DIT0055 Advanced Java Programming
[Link]("visit_count",
visitCount);
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head><title>Status Page</title></head>");
[Link]("<body>");
[Link]("<h1>Hello, " + userName + "!</h1>");
[Link]("<h2>Session and Cookie Demonstration:</h2>");
[Link]("<hr>");
[Link]("<h3>1. Session Tracking
(Current Visit Status):</h3>");
[Link]("<p><b>Status:</b> You are logged in for this
session.</p>"); [Link]("<p><b>Session ID:</b> " +
[Link]() + "</p>"); [Link]("<p><b>Session
Page Views:</b> " + visitCount + "</p>");
[Link]("<h3>2. Cookie
Usage (Long-term Persistence):</h3>");
[Link]("<p>Your name is saved in a **Cookie** on your
browser.</p>"); [Link]("<p>If you close your
browser and come back later, the server can still retrieve the
cookie data (if not expired).</p>");
[Link]("<p><b>Cookie Name Saved:</b> " +
[Link]() + "</p>");
[Link]("</body>");
[Link]("</html>");
}
}
Output :
If the user enters their name on the first page, the second page displays that name
using a Cookie.
38
Enrollment No:23DIT0055 Advanced Java Programming
39
Enrollment No:23DIT0055 Advanced Java Programming
Practical 15
Aim : Develop a simple JSP program for user registration and then control will
be transfer it into second page.
[Link]
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<h1>New User Registration</h1>
<form action="[Link]" method="POST">
<label for="userName">Name:</label>
<input type="text" id="userName"
name="userName" required><br><br>
<label for="userEmail">Email:</label>
<input type="email" id="userEmail" name="userEmail"
required><br><br> <button type="submit">Register and
Proceed</button>
</form>
</body>
</html>
[Link]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<%
40
Enrollment No:23DIT0055 Advanced Java Programming
41
Enrollment No:23DIT0055 Advanced Java Programming
Practical 16
Aim : Develop a simple JSP program for user login form with static and
dynamic database
42
Enrollment No:23DIT0055 Advanced Java Programming
<label
for="password">Password:</label>
<input type="password" id="password" name="password"
required><br><br>
<button type="submit">Login</button>
</form>
<%
}
%>
</body>
</html>
43
Enrollment No:23DIT0055 Advanced Java Programming
} else {
// If not submitted, display the form
%>
<form action="[Link]" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"
required><br><br>
<label
for="password">Password:</label>
<input type="password" id="password" name="password"
required><br><br>
<button type="submit">Login</button>
</form>
<%
}
%>
</body>
</html>
Validate_static.jsp
44
Enrollment No:23DIT0055 Advanced Java Programming
if
([Link](STATIC_USER) &&
[Link](STATIC_PASS)) { %>
<h1> Welcome, <%= user %>!</h1>
<p>You have been successfully logged in using
**Static Credentials**.</p> <p>Control was
transferred from **[Link]**.</p>
<%
} else {
%>
<h1> Login Failed!</h1>
<p>Invalid username or password.</p>
<p><a href="[Link]">Try Again</a></p>
<%
}
%>
</body>
</html>
Validate_dynamic.jsp
45
Enrollment No:23DIT0055 Advanced Java Programming
"jdbc:mysql://localhost:3306/mydatabase";
String DB_USER = "dbuser";
String DB_PASS = "dbpassword";
Connection conn =
null;
PreparedStatement ps = null;
ResultSet rs = null;
boolean authenticated
= false;
try {
// 2. Load the JDBC Driver (for newer versions,
this step is often optional)
[Link]("[Link]");
// 3. Establish connection
conn = [Link](DB_URL, DB_USER,
DB_PASS);
} catch (Exception e) {
%>
<h1> Database Error</h1>
<p>Could not connect to the database or run the
query.</p>
<p>Error: <%= [Link]() %></p>
46
Enrollment No:23DIT0055 Advanced Java Programming
<%
return; // Stop execution on error
} finally {
// 6. Close all resources try { if (rs
!= null) [Link](); } catch
(SQLException ignored) {} try { if (ps != null)
[Link](); } catch
(SQLException ignored) {} try { if (conn != null)
[Link](); } catch (SQLException ignored) {}
} if
(authenticated) {
%>
<h1> Welcome, <%= user %>!</h1>
<p>You have been successfully logged in using a
**Dynamic Database** connection.</p>
<p>Control was transferred from **[Link]**.</p>
<%
} else {
%>
<h1> Login Failed!</h1>
<p>Invalid username or password (checked against
the
database).</p> <p><a href="[Link]">Try
Again</a></p>
<%
}
%>
</body>
</html>
Output :
For Static Login :
47
Enrollment No:23DIT0055 Advanced Java Programming
48
Enrollment No:23DIT0055 Advanced Java Programming
Practical 17
49
Enrollment No:23DIT0055 Advanced Java Programming
// 2. Determine the
Grade if
(averagePercent >= 90) { grade
= "A+ (Excellent)";
} else if (averagePercent >= 80)
{ grade = "A
(Very Good)"; }
else
50
Enrollment No:23DIT0055 Advanced Java Programming
51
Enrollment No:23DIT0055 Advanced Java Programming
</div>
<%
} // End try-catch
} // End if (form submitted)
%>
<hr>
<form action="calculate_grade.jsp" method="post">
<h3>Enter Marks (Max 100 per Subject):</h3>
<label for="mark1">Subject 1:</label>
<input type="number" id="mark1" name="mark1"
min="0" max="100" required><br><br>
<label for="mark2">Subject 2:</label>
<input type="number" id="mark2" name="mark2"
min="0" max="100" required><br><br>
<label for="mark3">Subject 3:</label>
<input type="number" id="mark3" name="mark3"
min="0" max="100" required><br><br>
<label for="mark4">Subject 4:</label>
<input type="number" id="mark4" name="mark4"
min="0" max="100" required><br><br>
<label for="mark5">Subject 5:</label>
<input type="number" id="mark5" name="mark5"
min="0" max="100" required><br><br>
<button type="submit">Calculate Grade</button>
</form>
</body>
</html>
Output :
52
Enrollment No:23DIT0055 Advanced Java Programming
53