0% found this document useful (0 votes)
2 views25 pages

LabManual AL406 Java

The document is a lab manual for the Computer Programming Lab (Java Programming) at Gyan Ganga Institute of Technology & Sciences, Jabalpur, detailing a series of practical experiments for 4th-semester students. It includes various Java programming exercises such as basic arithmetic operations, function overloading, inheritance, exception handling, JDBC, applets, and servlets. Each experiment is accompanied by sample code and expected outputs, aimed at enhancing students' programming skills in Java.

Uploaded by

sahupappu4554
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)
2 views25 pages

LabManual AL406 Java

The document is a lab manual for the Computer Programming Lab (Java Programming) at Gyan Ganga Institute of Technology & Sciences, Jabalpur, detailing a series of practical experiments for 4th-semester students. It includes various Java programming exercises such as basic arithmetic operations, function overloading, inheritance, exception handling, JDBC, applets, and servlets. Each experiment is accompanied by sample code and expected outputs, aimed at enhancing students' programming skills in Java.

Uploaded by

sahupappu4554
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

Gyan Ganga

Institute of Technology & Sciences


Jabalpur

Lab Manual
AL - 406
Computer Programming Lab
[ Java Programming ]

Submitted By: Submitted To:


Name : Prof. L. N. Pandey
Roll Num: Asst. Prof.
Dept. of CSE

Lab Manual AL-406 Java Lab Page 14


SESSION : 2025-26
List of Practicals
SEM : 4th [EVEN]

S. Batch A Batch B
Name of Experiment Sign
No. Completion Completion
Program that accepts two numbers from the user and print their sum.
1
Program to calculate addition of two number using prototyping of
2 methods.
Program to demonstrate function overloading for calculation of
3 average.
Program to demonstrating overloaded constructor for calculating box
4 volume.
Program to show the detail of students using concept of inheritance.
5
Program to demonstrate package concept.
6
Program to demonstrate implementation of an interface which
7 contains two methods declaration square and cube.
Program to demonstrate exception handling in case of division by
8 zero error.
Program to demonstrate multithreading.
9
Program to demonstrate JDBC concept using create a GUI based
10 application for student information.
Program to create an application using concept of swing.
11
Program to demonstrate student registration functionality using
12 servlets with session management.

Lab Manual AL-406 Java Lab Page 15


SESSION : 2025-26
Experiment 1
SEM : 4th [EVEN]

Write a program that accepts two numbers from the user and print their
sum.
import [Link];
class Test
{ public static void main(String args[])
{ Scanner input = new Scanner([Link]);
int x, y, z;

[Link]("Enter first integer value : ");


x = [Link]();

[Link]("Enter first integer value : ");


y = [Link]();

z = x + y ;

[Link](x + " + " + y + " = " + z);


}
}

Output:

Lab Manual AL-406 Java Lab Page 16


Lab Manual AL-406 Java Lab Page 17
SESSION : 2025-26
Experiment 2
SEM : 4th [EVEN]

Write a program to calculate addition of two number using prototyping of


methods.
import [Link];
public class Exp02
{ public static void main(String args[])
{ Scanner input = new Scanner([Link]);
int x,y,z;

[Link]("Enter first integer value : ");


x = [Link]();

[Link]("Enter first integer value : ");


y = [Link]();

z = sum(x,y) ;

[Link](x + " + " + y + " = " + z);


}
static int sum(int a, int b)
{ int s;
s = a + b ;
return s;
}
}

Output:

Lab Manual AL-406 Java Lab Page 18


SESSION : 2025-26
Experiment 3
SEM : 4th [EVEN]

Program to demonstrate function overloading for calculation of average.


public class Exp03
{ public static void main(String args[])
{
[Link]("Avg : " + avg(10,20));
[Link]("Avg : " + avg(10,20,30));
[Link]("Avg : " + avg(10,20,30,40));
}
static double avg(int a,int b)
{
return (a+b)/2;
}
static double avg(int a,int b,int c)
{
return (a+b+c)/3;
}
static double avg(int a,int b,int c,int d)
{
return (a+b+c+d)/4;
}
}

Output:

Lab Manual AL-406 Java Lab Page 19


SESSION : 2025-26
Experiment 4
SEM : 4th [EVEN]

Program to demonstrating overloaded constructor for calculating box


volume.
public class Exp04 {
public static void main(String args[]) {
Box a = new Box(10);
[Link]();
Box b = new Box(10, 20);
[Link]();
Box c = new Box(10, 20, 30);
[Link]();
}
}
class Box {
double l, b, h;

Box(double x) {
l = b = h = x;
}
Box(double x, double y) {
l = b = x;
h = y;
}
Box(double x, double y, double z) {
l = x;
b = y;
h = z;
}
void volume() {
[Link]("Volume of Box " + l * b * h);
}
}
Output:

Lab Manual AL-406 Java Lab Page 20


SESSION : 2025-26
Experiment 5
SEM : 4th [EVEN]

Program to show the detail of students using concept of inheritance.


public class Exp05 {
public static void main(String args[]) {
Student e = new Student("KRISH","RAM","VIZAG","9885098850",10,19, 'B', "IT");
e.display2();
}
}

class Person {
String name = new String();
String fname = new String();
String add = new String();
String phno = new String();

Person(String a, String b, String c, String d) {


name = a;
fname = b;
add = c;
phno = d;
}

void display() {
[Link]("Name is " + name);
[Link]("Address " + add);
[Link]("Father's Name is " + fname);
[Link]("Contact number " + phno);
}
}

class Student extends Person {


int roll, age;
char section;
String branch = new String();

Student(String a, String b, String c, String d, int e, int f, char g, String h) {


super(a, b, c, d);
roll = e;
age = f;
section = g;
branch = h;
}

void display2() {
[Link]();
[Link]("Roll Number=" + roll);

Lab Manual AL-406 Java Lab Page 21


[Link]("AGE=" + age);
[Link]("SECTION=" + section);
[Link]("Branch is " + branch);

Output:

Lab Manual AL-406 Java Lab Page 22


SESSION : 2025-26
Experiment 6
SEM : 4th [EVEN]

Program to demonstrate package concept.

package p; // Creation of package


import [Link]; // import specific class from a package
import [Link].*; // import all classes from package
class Exp06 {
public static void main(String args[]){
// Third way to import class from package.
[Link] str = "Example of Package";
[Link](str);
}
}

Output:

Lab Manual AL-406 Java Lab Page 23


SESSION : 2025-26
Experiment 7
SEM : 4th [EVEN]

Program to demonstrate implementation of an interface which contains two


methods declaration square and cube.

public class Exp07 {


public static void main(String args[]) {
Shapes shapes = new Shapes();
[Link](12);
[Link](12);
}
}

interface Shape {
void square(int x);
void cube(int x);
}

class Shapes implements Shape {


public void square(int x) {
[Link]("Area of Square is : "+ x*x);
}

public void cube(int x) {


[Link]("Area of Cube is : "+ 6*x*x);
}
}

Output:

Lab Manual AL-406 Java Lab Page 24


SESSION : 2025-26
Experiment 8
SEM : 4th [EVEN]

Program to demonstrate exception handling in case of division by zero error.

import [Link];
class Exp08 {
public static void main(String args[]) {
Scanner input = new Scanner([Link]);
int x,y,z;
try{
[Link]("Enter first number : ");
x = [Link]();
[Link]("Enter second number : ");
y = [Link]();

z = x / y ;
[Link](x + " / " + y + " = " + z);
}catch(ArithmeticException ae){
[Link]("Can't Divide by Zero");
}
}
}

Output:

Lab Manual AL-406 Java Lab Page 25


SESSION : 2025-26
Experiment 9
SEM : 4th [EVEN]

Program to demonstrate multithreading.

class Exp09{
public static void main(String args[]){
Simple1 t1=new Simple1();
Simple2 t2=new Simple2();

[Link]();
[Link]();
}
}
class Simple1 extends Thread{
public void run(){
[Link]("Performing task one");
}
}

class Simple2 extends Thread{


public void run(){
[Link]("Performing task two");
}
}

Output:

Lab Manual AL-406 Java Lab Page 26


SESSION : 2025-26
Experiment 10
SEM : 4th [EVEN]

Program to demonstrate JDBC concept using GUI based application for


student information.

Prerequisite :

Kindly see Appendix – 1 in Additional Resources Section at the End of this


Document.

Program:
import [Link].*;
public class Exp10 {
public static void main(String[] args) {
final String DB_URL = "jdbc:mysql://localhost/MyDB";
final String USER = "root"; // default username for MySQL DB
final String PASS = ""; // Default Password for MySQL DB
String QUERY="SELECT * FROM Emp";
// Open a connection
try {
Connection conn;
conn = [Link](DB_URL, USER, PASS);
Statement stmt = [Link]();
ResultSet rs = [Link](QUERY);
// Extract data from result set
[Link]("ID FirstName LastName Age ");
while ([Link]()) {
// Retrieve by column name
[Link]("%2d ", [Link]("id") );
[Link]("%-15s", [Link]("first_name"));
[Link]( "%-15s",[Link]("last_name"));
[Link]( "%3d",[Link]("age"));
[Link]();
}
} catch (SQLException e) {
[Link]();
}
}
}

Lab Manual AL-406 Java Lab Page 27


Output:

Lab Manual AL-406 Java Lab Page 28


SESSION : 2025-26
Experiment 11
SEM : 4th [EVEN]

Program to display “Hello World” in web browser using applet.

Prerequisite:

1. Create an HTML File. With <Applet> Tag. Say [Link]


2. Create Java Applet with Java. Say [Link]

Solution :

Program 1 :

<!--HTML File : [Link]-->


<head>
<title>Hello World Applet</title>
</head>
<body>
<applet code="[Link]" width="200" height="200">
</applet>
</body>
</html>

Program 2.

// Java File : [Link]


import [Link];
import [Link];
public class Exp10 extends Applet {
public void paint(Graphics g) {
[Link]("Hello World !", 10, 30);
}
}

Lab Manual AL-406 Java Lab Page 29


SESSION : 2025-26
Experiment 12
SEM : 4th [EVEN]

Program to add user controls to applets.

// Java File : [Link]


import [Link].*;
import [Link];
import [Link];
public class Exp12 extends Applet {
public void init(){
add(new Button("Click Me"));
add(new TextField("Hello",15));
add(new CheckBox("Check1"));
}
public void paint(Graphics g) {
[Link]("Hello World !", 10, 30);
}
}

Lab Manual AL-406 Java Lab Page 30


SESSION : 2025-26
Experiment 11
SEM : 4th [EVEN]

Write a program to create an application using concept of swing.


import [Link].*;
public class Exp11 {
public static void main(String[] args) {
JFrame f = new JFrame();// creating instance of JFrame

JButton b = new JButton("click");// creating instance of JButton


[Link](130, 100, 100, 40);// x axis, y axis, width, height

[Link](b);// adding button in JFrame

[Link](400, 300);// 400 width and 500 height


[Link](null);// using no layout managers
[Link](true);// making the frame visible
}
}

Output:

Lab Manual AL-406 Java Lab Page 31


SESSION : 2025-26
Experiment 14
SEM : 4th [EVEN]

Program to demonstrate student registration functionality using servlets


with session management.

Prerequisite :

Kindly see Appendix – 2 in Additional Resources Section at the End of this


Document.

Program:

// Import required java libraries


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

// Extend HttpServlet class


public class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException {


// Do required initialization
message = "Hello World";
}

public void doGet(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {

// Set response content type


[Link]("text/html");

// Actual logic goes here.


PrintWriter out = [Link]();
[Link]("<h1>" + message + "</h1>");
}

public void destroy() {

Lab Manual AL-406 Java Lab Page 32


// do nothing.
}
}

Appendix – 1
Step 1 : Install XAMPP Server + MySQL and Create Database

1. Install XAMPP Server for using MySQL Database.


2. Open XAMPP Control panel and start both Apache Server and MySQL
Database.

Click on start button for both Apache and MySQL to start.

3. Click on admin button of MySQL to open phpMyAdmin web window.

Lab Manual AL-406 Java Lab Page 33


4. Click on new button on left panel to Create Database named MyDB

On clicking create, it will show new window to create table.

Lab Manual AL-406 Java Lab Page 34


Type Emp in Table name and set 4 as number of columns. On clicking create,
it will show new window to add columns in the table.

Type details as shown in above pic and set id as primary key by Ticking A_I
checkbox. And click on Save to create it.
5. Now click on Query option on top to Insert Sample Records and run
following Query.

Lab Manual AL-406 Java Lab Page 35


After typing above query, click on Go Button to insert records.
6. You will get success message if you typed everything correctly.
7. Now you click on Brows on top panel to see records.

8. Congratulations!!!! You have successfully completed Database Creation.

Note:

Default username is root and password for MySQL is “” (blank).

Step 2 : Add JDBC Library (JDBC Connector) to your Code

1. In VS Code, press Ctrl + Shift + P, searchbar will open then Type


java configure classpath in serchbar and select it.

Lab Manual AL-406 Java Lab Page 36


2. Select Libraries tab from (Sources, JDK Runtime, Libraries)

Click on add Library and select [Link]


from your downloaded location.
3. Click on Apply Settings and you are done adding Library.
4. Now close this Project Settings Tab and return to your program.

Lab Manual AL-406 Java Lab Page 37


Appendix – 2

Lab Manual AL-406 Java Lab Page 38

You might also like