Java Practicals Solutions
A Collection of Java Programs for Practical Exercises
Prepared by: [Your Name]
Date: May 28, 2025
Submitted as part of Java Programming Course Requirements
Contents
1 Practical 1: Create an Applet 2
2 Practical 2: Applet to Display Employee Information 2
3 Practical 3: Display 4 Buttons Using AWT 3
4 Practical 4: Display Text Field, Check Box, and Radio Button Using
Swing 4
5 Practical 5: Create Multiple Frames with a Back Button 5
6 Practical 6: Create a Frame with a Push Button to Display an Image 6
7 Practical 7: Execute Select Query Using JDBC 7
8 Practical 8: Basic Arithmetic Functions Using JSP 8
9 Practical 9: Create a Simple Servlet and Test It 10
10 Practical 10: Create a Bean to Display Employee Details 11
1
1 Practical 1: Create an Applet
import java . applet . Applet ;
import java . awt . Graphics ;
public class SimpleApplet extends Applet {
public void paint ( Graphics g ) {
g . drawString ( " Hello , this is a simple
Applet ! " , 20 , 20) ;
}
}
Note: Applets are outdated technology and not supported in modern browsers. This is a
basic example for educational purposes.
2 Practical 2: Applet to Display Employee Informa-
tion
import java . applet . Applet ;
import java . awt . Graphics ;
public class EmployeeApplet extends Applet {
String name , designation ;
double salary , tax ;
public void init () {
name = getParameter ( " name " ) ;
designation = getParameter ( " designation " ) ;
salary =
Double . parseDouble ( getParameter ( " salary " ) ) ;
tax =
Double . parseDouble ( getParameter ( " tax " ) ) ;
}
public void paint ( Graphics g ) {
Page 2 of 12
g . drawString ( " Name : " + name , 20 , 20) ;
g . drawString ( " Designation : " +
designation , 20 , 40) ;
g . drawString ( " Salary : " + salary , 20 , 60) ;
g . drawString ( " Tax : " + tax , 20 , 80) ;
}
}
HTML to run the applet (for reference):
< applet code = " EmployeeApplet . class " width = " 300 "
height = " 150 " >
< param name = " name " value = " John Doe " >
< param name = " designation " value = " Manager " >
< param name = " salary " value = " 50000 " >
< param name = " tax " value = " 5000 " >
</ applet >
3 Practical 3: Display 4 Buttons Using AWT
import java . awt .*;
public class FourButtonsAWT extends Frame {
FourButtonsAWT () {
setTitle ( " AWT Buttons " ) ;
setSize (300 , 200) ;
setLayout ( new FlowLayout () ) ;
Button b1 = new Button ( " Button 1");
Button b2 = new Button ( " Button 2");
Button b3 = new Button ( " Button 3");
Button b4 = new Button ( " Button 4");
add ( b1 ) ;
add ( b2 ) ;
Page 3 of 12
add ( b3 ) ;
add ( b4 ) ;
setVisible ( true ) ;
}
public static void main ( String [] args ) {
new FourButtonsAWT () ;
}
}
4 Practical 4: Display Text Field, Check Box, and
Radio Button Using Swing
import javax . swing .*;
import java . awt .*;
public class SwingComponents {
public static void main ( String [] args ) {
JFrame frame = new JFrame ( " Swing
Components " ) ;
frame . setSize (300 , 200) ;
frame . setLayout ( new FlowLayout () ) ;
frame . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ;
JTextField textField = new JTextField (15) ;
JCheckBox checkBox = new JCheckBox ( " Check
Me " ) ;
JRadioButton radioButton = new
JRadioButton ( " Select Me " ) ;
frame . add ( textField ) ;
frame . add ( checkBox ) ;
frame . add ( radioButton ) ;
Page 4 of 12
frame . setVisible ( true ) ;
}
}
5 Practical 5: Create Multiple Frames with a Back
Button
import javax . swing .*;
public class MultipleFrames {
public static void main ( String [] args ) {
JFrame frame1 = new JFrame ( " Frame 1 " ) ;
frame1 . setSize (300 , 200) ;
frame1 . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ;
frame1 . setLayout ( null ) ;
JButton openFrame2 = new JButton ( " Open
Frame 2 " ) ;
openFrame2 . setBounds (50 , 50 , 150 , 30) ;
frame1 . add ( openFrame2 ) ;
frame1 . setVisible ( true ) ;
JFrame frame2 = new JFrame ( " Frame 2 " ) ;
frame2 . setSize (300 , 200) ;
frame2 . setLayout ( null ) ;
JButton backButton = new JButton ( " Back " ) ;
backButton . setBounds (50 , 50 , 100 , 30) ;
frame2 . add ( backButton ) ;
openFrame2 . addActionListener ( e ->
frame2 . setVisible ( true ) ) ;
Page 5 of 12
backButton . addActionListener ( e ->
frame2 . setVisible ( false ) ) ;
frame1 . setVisible ( true ) ;
}
}
6 Practical 6: Create a Frame with a Push Button
to Display an Image
import javax . swing .*;
import java . awt .*;
public class ImageButtonSwing {
public static void main ( String [] args ) {
JFrame frame = new JFrame ( " Image Button " ) ;
frame . setSize (400 , 300) ;
frame . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ;
frame . setLayout ( new FlowLayout () ) ;
JLabel label = new JLabel ( " Click to see
image " ) ;
JButton button = new JButton ( " Show Image " ) ;
JLabel imageLabel = new JLabel () ;
button . addActionListener ( e -> {
// For this example , we assume an
image file " sample . jpg " exists in
the project directory
ImageIcon icon = new
ImageIcon ( " sample . jpg " ) ;
imageLabel . setIcon ( icon ) ;
}) ;
Page 6 of 12
frame . add ( label ) ;
frame . add ( button ) ;
frame . add ( imageLabel ) ;
frame . setVisible ( true ) ;
}
}
Note: Replace "sample.jpg" with the actual path to an image file on your system.
7 Practical 7: Execute Select Query Using JDBC
import java . sql .*;
public class SelectQueryJDBC {
public static void main ( String [] args ) {
try {
// Load the JDBC driver (e.g., for
MySQL )
Class . forName ( " com . mysql . cj . jdbc . Driver " ) ;
// Establish connection
Connection con =
DriverManager . getConnection ( " jdbc : mysql :// localhost :
" username " , " password " ) ;
// Create statement
Statement stmt = con . createStatement () ;
// Execute query
ResultSet rs =
stmt . executeQuery ( " SELECT * FROM
employees " ) ;
// Process result
while ( rs . next () ) {
Page 7 of 12
System . out . println ( " ID : " +
rs . getInt ( " id " ) + " , Name : " +
rs . getString ( " name " ) ) ;
}
// Close connections
rs . close () ;
stmt . close () ;
con . close () ;
} catch ( Exception e ) {
e . printStackTrace () ;
}
}
}
Note: Replace "yourdb", "username", and "password" with your actual database details.
Ensure the MySQL JDBC driver is added to your project.
8 Practical 8: Basic Arithmetic Functions Using JSP
Add.jsp:
<% @ page language = " java " contentType = " text / html ;
charset = UTF -8 " pageEncoding = " UTF -8 " % >
<! DOCTYPE html >
< html >
< head >
< title > Arithmetic Operations </ title >
</ head >
< body >
<h2 > Arithmetic Operations </ h2 >
< form method = " post " >
Number 1: < input type = " number " name = " num1 "
required > < br > < br >
Number 2: < input type = " number " name = " num2 "
required > < br > < br >
Page 8 of 12
< input type = " submit " value = " Calculate " >
</ form >
<%
if
( request . getMethod () . equalsIgnoreCase ( " POST " ) )
{
double num1 =
Double . parseDouble ( request . getParameter ( " num1 " ) ) ;
double num2 =
Double . parseDouble ( request . getParameter ( " num2 " ) ) ;
out . println ( " <h3 > Results : </ h3 > " ) ;
out . println ( " Addition : " + ( num1 +
num2 ) + " <br > " ) ;
out . println ( " Subtraction : " + ( num1 -
num2 ) + " <br > " ) ;
out . println ( " Multiplication : " + ( num1
* num2 ) + " <br > " ) ;
if ( num2 != 0) {
out . println ( " Division : " + ( num1 /
num2 ) ) ;
} else {
out . println ( " Division : Cannot
divide by zero " ) ;
}
}
%>
</ body >
</ html >
Note: Deploy this JSP file on a server like Tomcat to test it.
Page 9 of 12
9 Practical 9: Create a Simple Servlet and Test It
SimpleServlet.java:
import java . io .*;
import javax . servlet .*;
import javax . servlet . http .*;
public class SimpleServlet extends HttpServlet {
protected void doGet ( HttpServletRequest
request , HttpServletResponse response )
throws ServletException , IOException {
response . setContentType ( " text / html " ) ;
PrintWriter out = response . getWriter () ;
out . println ( " < html > < body > " ) ;
out . println ( " <h2 > Hello from Simple
Servlet ! </ h2 > " ) ;
out . println ( " </ body > </ html > " ) ;
}
}
web.xml (Deployment Descriptor):
<web - app >
< servlet >
< servlet - name > SimpleServlet </ servlet - name >
< servlet - class > SimpleServlet </ servlet - class >
</ servlet >
< servlet - mapping >
< servlet - name > SimpleServlet </ servlet - name >
<url - pattern >/ simple </ url - pattern >
</ servlet - mapping >
</ web - app >
To Test: Deploy on a server like Tomcat and access http://localhost:8080/yourapp/simple.
Page 10 of 12
10 Practical 10: Create a Bean to Display Employee
Details
EmployeeBean.java:
public class EmployeeBean {
private String name ;
private double salary ;
private String designation ;
private String company ;
// Getters and Setters
public String getName () { return name ; }
public void setName ( String name ) { this . name =
name ; }
public double getSalary () { return salary ; }
public void setSalary ( double salary ) {
this . salary = salary ; }
public String getDesignation () { return
designation ; }
public void setDesignation ( String designation )
{ this . designation = designation ; }
public String getCompany () { return company ; }
public void setCompany ( String company ) {
this . company = company ; }
}
TestEmployeeBean.java:
public class TestEmployeeBean {
public static void main ( String [] args ) {
EmployeeBean emp = new EmployeeBean () ;
emp . setName ( " John Doe " ) ;
emp . setSalary (60000) ;
emp . setDesignation ( " Developer " ) ;
emp . setCompany ( " Tech Corp " ) ;
Page 11 of 12
System . out . println ( " Name : " +
emp . getName () ) ;
System . out . println ( " Salary : " +
emp . getSalary () ) ;
System . out . println ( " Designation : " +
emp . getDesignation () ) ;
System . out . println ( " Company : " +
emp . getCompany () ) ;
}
}
Page 12 of 12