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

Java Lab Manual

The document is a lab manual for Java programming experiments conducted at Malwa Institute of Science and Technology. It includes various experiments focusing on Java concepts such as class, type casting, exception handling, inheritance, polymorphism, and database connectivity. Each experiment outlines objectives, required software, theoretical background, procedures, and sample programs.

Uploaded by

jeevanrajput850
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)
3 views25 pages

Java Lab Manual

The document is a lab manual for Java programming experiments conducted at Malwa Institute of Science and Technology. It includes various experiments focusing on Java concepts such as class, type casting, exception handling, inheritance, polymorphism, and database connectivity. Each experiment outlines objectives, required software, theoretical background, procedures, and sample programs.

Uploaded by

jeevanrajput850
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

LAB

LABMANUAL
MANUAL
Java Programming

Submitted To:
Submitted Submitted
SubmittedBy:
By:
Ms. Sonali
Sonali Goswami
Goswami Name:

Assistant Professor
Assistant Professor Roll No. :
Department of
Department of CS/IT
CS/IT Semester/Year

DEPARTMENT
DEPARTMENT
OF
COMPUTER
COMPUTERSCIENCE
SCIENCE&&
ENGINEERING
ENGINEERING
MALWA INSTITUTE
MALWA INSTITUTEOFOF
SCIENCE AND
SCIENCE TECHNOLOGY,
AND INDORE
TECHNOLOGY, INDORE
VILLAGE LIMBODAGARI,
VILLAGE SAWER
LIMBODAGARI, ROAD
SAWER INDORE
ROAD INDORE
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Experiment
Objective of Experiments Page No.
No

1 Installation of J2SDK

2 Write a program to show Concept of CLASS in JAVA

3 Write a program to show Type Casting in JAVA

4 Write a program to show How Exception Handling is in JAVA

5 Write a program to show Inheritance

6 Write a program to show Polymorphism

7 Write a program to show Interfacing between two classes

8 Write a program to Add a Class to a Package

9 Write a program to demonstrate AWT.

10 Write a program to Hide a Class

11 Write a Program to show Data Base Connectivity Using JAVA

12 Write a Program to show “HELLO JAVA ” in Explorer using Applet

13 Write a Program to show Connectivity using JDBC

14 Write a program to demonstrate multithreading using Java.


MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Experiment No. 01

Date of
Conduction
Date of
Submission
Objective Installation of J2SDK

Software Used 1. Internet Explorer


2. Internet Connection

Theory Introduction to JAVA


Java is one of the computer programming languages that can be used to
create both web based and desktop based application/program/software.
It is called as 99 % object oriented Language.
Java history:
Java was conceived by James Gosling, Patrick Naughton, Chirs Warth,
Ed Frank and Mike Sheridian at Sun Microsystems in [Link] language
was initially called as “Oak”. It was renamed as java in 1995.
The trouble with C & C++ is that they are designed to be compiled for a
specific target. Although it is possible to compile C++ program on any
type of CPU, it requires full C++ compiler on targeted for the particular
CPU. The compilers are expensive and time consuming to develop. An
easier & cost efficient solution was required.
Sun Microsoft officially describes java with the following features:
1. Simple: no use of pointers, preprocessor
2. Compiled & interpreted.
3. Platform interdependent.
4. Object Oriented.
5. Reliable and secure.
6. Distribution.
7. Multithreading.
8. High performance and Dynamic.

Procedure Open the website URL:-


[Link]
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Click on the JDK download button.

Click on Radio Button Accept the license agreement and then click on the
download link from the table depending on the operating system and
architecture of your PC (windows x86)

The File Download dialog box appears prompting you to run or save the
download file

Click Save to download the file to your local system.


Tip: Save the file to a known location on your computer, for example, to
your desktop.
Close all applications including the browser.
Double-click on the saved file to start the installation process.
The installation process starts. Click the Install button to accept the
license terms and to continue with the installation.
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Oracle has partnered with companies that offer various products. The
installer may present you with option to install these programs when you
install Java. After ensuring that the desired programs are selected, click
the Next button to continue the installation.

A few brief dialogs confirm the last steps of the installation process;
click Close on the last dialog. This will complete Java installation process.

Viva Question 1. What is programming language?


2. Difference between java and C language?
3. How to install java software
4. What is Integrated Development Environment
Experiment No. 02
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Date of
Conduction
Date of
Submission
Objective Write a program to show Concept of CLASS in JAVA

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory Objects and Classes:


Object: Everything in this world is an object, from a nail to a hammer,
from lever to an airplane. As per human perspective object is nothing but
a visible thing or something by which action takes place or something that
exists in time and space.
In OOP we are interested in those objects that simulate to reality i.e. real
world objects.
Definition: “An object represents an individual, identifiable item, unit, or
entity, either real or abstract, with a well defined role in the problem
domain”.
Class: The object contains data and code to manipulate that data. The
entire set of data and code of an object can be made a user defined data
type using the concept of the class. A class may be thought of as a ‘data
type’ and an object as a variable of that data type.
Once a class is defined, we can create any number of objects belonging to
that class. Classes are user defined data type and behave like built-in types
of a programming language. A class is thus a collection of objects of
similar type.

Example: mango, apple and orange are the members of the class fruit.
The basic form of a class definition is:
class <Class name>
{
Variables ----------;
Methods -----------;
}
Attributes and Behavior:
Attributes(variables): The properties of the class in terms of the nature
and behavior are called the attributes of that specific class. Each class has
its own unique attributes. In programming these attributes are created as
variables.
Behavior(methods): A class with only data fields not works, so as to
make the class in working condition, methods are declared inside the body
of the class but immediately after the declaration of instance variables.
The general form of the method is
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

returntype methodname (parameter- list )


{ Method-body;
}

Program class First


{
int x,y
void addition()
{
int z = x+y;
[Link]( "The addition is "+z );
}
}
class Second
{
public static void main( String args[ ] )
{
First f1 = new First();
f1.x=10;
f1.y=20;
[Link]();
}
}

Output

Viva Question 1. What is a class in JAVA?


2. What a class contains in itself?
3. How to create a object of a class in java?
4. How a data member of a class is been called from another class?
Experiment No. 03

Date of
Conduction
Date of
Submission
Objective Write a program to show Type Casting in JAVA
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory Typecasting:
The process of converting the value from one data type to another is called
as typecasting. In other words it can be understand as the process in which
the source variable data type is different to that of destination data type is
called as typecasting.

Types of typecasting:
1. Implicit typecasting
2. Explicit typecasting

Implicit typecasting: It is also called as widening. The process in which


the value is been assigned from the smaller data type to larger data type is
called as implicit typecasting. This process is done by java compiler itself
that’s why the name is given implicit.
E.g. byte x = 89;
int y = x ;
here the value of x is been assigned to y where x is byte(8) and y is int(32)

Explicit typecasting: It is also called as narrowing. The process in which


the value is been assigned from the larger data type to smaller data type is
called as explicit typecasting. This process java complier cannot do itself
because there is the risk of loss of precision (loss in actual data). Therefore
programmer has to do this task explicitly.
E.g. byte x ;
int y = 104 ;
x = (byte) y ;
here the value of y is been assigned to x where x is byte(8) and y is int(32)

Program class Second


{
public static void main(String args[])
{
int x = 125;
[Link]("The value of x is "+x);
byte y;
y = (byte)x;
[Link]("The value of y is "+y);
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

int p=99;
[Link]("The value of p is "+p);
char ch;
ch = (char)p;
[Link]("The value of ch is "+ch);

char as = 'f';
[Link]("The value of as is "+as);
int q;
q = (int)as;
[Link]("The value of q is "+q);
}
}

Output

Viva Question 1. Why typecasting is done?


2. What are data types in java?

Experiment No. 04

Date of
Conduction
Date of
Submission
Objective Write a program to show How Exception Handling is in JAVA

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory Exception handling:


It is very rare that a program works correctly first time. It may have an
error or an unexpected event. This is known as bugs.
Types of Errors (Bugs):
 Logical error
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

 Syntactic error
Logical error:
The logical error encounters due to poor understanding and implementing
procedure of the task to be achieved.
Syntactic error:
The syntactic error encounters due to poor understanding of the language
itself.
Problems other than logical and syntactic errors are known as exceptions.
It is an error or an unexpected event. These are unusual condition that may
occur at the time of program execution. Exception handling is one of the
most recent features of object oriented programming.
Java exception
A java exception is an object that describes an exceptional (error)
condition that has occurred in a piece of code. When an exceptional
condition arises, an object representing that exception is created and
thrown in the method that caused the error. That method may choose to
handle the exception itself, or pass it on.

Program class MyExceptionHandle


{
public static void main(String a[])
{
try{
for(int i=5;i>=0;i--){
[Link](10/i);
}
} catch(Exception ex){
[Link]("Exception Message: "+[Link]());
[Link]();
}
[Link]("After for loop...");
}
}
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Output

Viva Question 1. Define Error and exception.


2. How many ways available to handle exception in Java?
3. Difference between checked and unchecked exceptions?
Experiment No. 05

Date of
Conduction
Date of
Submission
Objective Write a Program to show Inheritance

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory Inheritance:
It is the process by which the one object acquires the properties of another
object.
In JAVA the mechanism of deriving a new class from the old class is
called inheritance.
For example:

Super class or base or parent class: Class that is inherited is called the
super class or base class.

Sub class or derived or child class: The class that does the inheriting is
called sub class. It inherits all of the instance variables and methods
defined by the super class and add its own unique elements.

Here all the class members of class One except that are been declared as
private will be in inherited to class Two and apart from the inherited class
Two can have its own others members.
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

To inherit a class you simply use a keyword “extends” during declaration


of child class.

Syntax: class <subclass name> extends <super class name>


Example: class MotorCycle extends Cycle { }

Program class First


{
int a,b,c;
void add()
{
c=a+b;
[Link]("Addition is "+c);
}
}
class Second extends First
{
void mul()
{
c=a*b;
[Link]("Multiplication is"+c);
}

}
class Third
{
public static void main(String args[])
{
Second s1 = new Second();
s1.a=2;
s1.b=5;
[Link]();
[Link]();
}
}
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Output

Viva Question 1. How many types of inheritance occur in OOP?


2. Which keyword is for inheritance?
3. Define base class and derived class?

Experiment No. 06

Date of
Conduction
Date of
Submission
Objective Write a program to show Polymorphism

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory Polymorphism: Polymorphism means that one name in different forms.


According to object oriented programming the object behaving in more
than one form is called as polymorphism.

In programming this can be achieved by two ways. We can use same


method name in different forms

There are two strategies in methods for same name different uses

1) Method overloading. { Compile time polymorphism }

2) Method overriding. { Run time polymorphism }

Method overloading: It is possible to define two or more methods within


the same class the same name. As long as their parameter declarations are
different when this is the case the methods are said to be overloaded & a
process is referred to as method overloading.

Method overloading is one of the ways that java uses the type and a
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

number of arguments as its guide to determine which version of


overloaded method to actually called.

Method overloading: It is possible to define two or more methods within


the same class the same name. As long as their parameter declarations are
different when this is the case the methods are said to be overloaded & a
process is referred to as method overloading.

Method overloading is one of the ways that java uses the type and a
number of arguments as its guide to determine which version of
overloaded method to actually called.

Program class First


{
void area(int a)
{
int z = a*a*a;
[Link]("Area is "+z);
}
void area(int a,int b)
{
int z = a*b;
[Link]("Area is "+z);
}
void area(int a,int b,int c)
{
int z = a*b*c;
[Link]("Area is "+z);
}
}

class Second
{
public static void main(String args[])
{
First f1 = new First();
[Link](4);
[Link](10,30);
[Link](10,20,30);
}
}
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Output

Viva Question 1. What is polymorphism?


2. Difference between method overloading and overriding?

Experiment No. 07

Date of
Conduction
Date of
Submission
Objective Write a program to show Interfacing between two classes

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory Interfaces:
Interfaces are syntactically similar to abstract classes, but they lack
instance variables, and their methods are declared without any body.

An interface is like a class with some restrictions.

1. All its methods must be abstract instance methods, no static


methods allowed. However you don’t declare the methods abstract.
That is implicit.
2. All the variables defined in an interface must be static final, i.e.
constants.
3. No static initialize blocks. You must do your initializations in one
line per variable. And, of course, no static initialize helper methods
defined in the interface. If you need them, they must be defined
outside the interface.
4. The throws clauses about Exceptions in the interface’s methods
must exactly match the throws clauses of the implementing
methods.
5. An interface may extend any other interface but after that the class
which uses interface must override the methods of both interfaces.
6. Variables declared in interface can be accessed by interface name
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

and dot operator i.e. [Link]

Extend or implement?
1. A class extends another class.
2. A class implements one or more interfaces.
3. An interface extends one or more interfaces.
Instantiating:-
Interface methods are always instance methods. To use them, there must
be some associated Object that implements the interface. You can’t
instantiate an interface directly, but you can instantiate a class that
implements an interface. References to an Object can by via the class
name, via one of its superclass names, or one of its interface names.
Program interface First
{
public void add(int x,int y);
}

class Second implements First


{

public void add(int x,int y)


{
int z = x+y;
[Link](z);
}

public static void main(String args[])


{
Second s1 = new Second();
[Link](12,34);
}

Output

Viva Question 1. Which keyword is used for interfacing?


2. What is the use of interface in java?
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Experiment No. 08

Date of
Conduction
Date of
Submission
Objective Write a program to Add a Class to a Package

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory Packages:
Packages are collection of classes. In other words it’s also defined as
collection of classes kept under a single folder is called as package.
In java we can create our own packages and can also call the classes inside
those packages in other classes or programs.

For creating a package the syntax is: - package packagename ;


And for calling a package the syntax is: - import [Link]
;
{Wildcard (*) can be used to import all the classes of that particular
package}

While creating a program for a package following steps are to be done:


1. Explicitly create a folder in any drive of your hard disk. Simply
just by right click and named it as mypack.
2. Now create a new java file as done below in left column and it
should have first line of package mypack.
3. Save this file inside the mypack folder we created in step1
4. Compile this file
5. Now create a new file having main method and using the package
6. Save this file one level above i.e. just nearby mypack folder.
7. Complie this file and run it.

Program package amit; import amit.*;


MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

public class First class Second


{ {
public void factorial(int x) public static void main(String
{ args[])
int fact = 1; {
for(int i=x;i>=1;i--)
{ First f1 = new First();
fact = fact*i; [Link](5);
} }
[Link]("The factorial is
"+fact); }
}
}

Output

Viva Question 1. What are packages?


2. Which keywords are used to create and call the package
3. List any two predefined packages available in JAVA?

Experiment No. 09

Date of
Conduction
Date of
Submission
Objective Write a program to demonstrate AWT.

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or


window-based application in java.
Java AWT components are platform-dependent i.e. components are
displayed according to the view of operating system. AWT is heavyweight
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

i.e. its components uses the resources of system.


The [Link] package provides classes for AWT api such as TextField,
Label, TextArea, RadioButton, CheckBox, Choice, List etc.

Container
The Container is a component in AWT that can contain another
component like buttons, textfields, labels etc. The class that extends
Container class is known as container such as Frame, Dialog and Panel.

Window
The window is the container that has no borders and menu bars. You must
use frame, dialog or another window for creating a window.

Panel
The Panel is the container that doesn't contain title bar and menu bars. It
can have other components like button, textfield etc.

Frame
The Frame is the container that contain title bar and can have menu bars. It
can have other components like button, textfield etc.

Program import [Link].*;

class First extends Frame


{
First()
{
setSize(150,150);
setVisible(true);
setLayout(new FlowLayout());
Label l1 = new Label("Enter Your Name");
TextField t1 = new TextField(20);
Button b1 = new Button("Click Here");
add(l1);
add(t1);
add(b1);
}
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

}
class Second
{
public static void main(String args[])
{
First f1 = new First();
}
}

Output

Viva Question 1. What is desktop application?


2. What are windows and panels in GUI application?
Experiment No. 10

Date of
Conduction
Date of
Submission
Objective Write a program to Hide a Class

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory Java inner class or nested class is a class i.e. declared inside the class or
interface.
We use inner classes to logically group classes and interfaces in one place
so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including private
data members and methods.

Advantage of java inner classes

There are basically three advantages of inner classes in java. They are as
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

follows:
1) Nested classes represent a special type of relationship that is it can
access all the members (data members and methods) of outer
class including private.

2) Nested classes are used to develop more readable and maintainable


code because it logically group classes and interfaces in one place only.

3) Code Optimization: It requires less code to write.

Difference between nested class and inner class in Java


Inner class is a part of nested class. Non-static nested classes are known as
inner classes.
Types of Nested classes
There are two types of nested classes non-static and static nested
[Link] non-static nested classes are also known as inner classes.

1. Non-static nested class(inner class)


o a)Member inner class
o b)Annomynous inner class
o c)Local inner class
2. Static nested class

Program class Outer


{
private class Inner
{
void msg()
{
[Link]("Hello World");
}
}
void call()
{
Inner i1 = new Inner();
[Link]();
}
}
class Second
{
public static void main(String args[])
{
Outer obj=new Outer();
[Link]();
}
}
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Output

Viva Question 1. What are the inner classes?


2. How many types of nested classes are in JAVA?
3. List the advantages of inner classes?
Experiment No. 11

Date of
Conduction
Date of
Submission
Objective Write a Program to show Data Base Connectivity Using JAVA

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory
Program
Output
Viva Question
Experiment No. 12

Date of
Conduction
Date of
Submission
Objective Write a Program to show “HELLO JAVA ” in Explorer using Applet

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory
Program import [Link].*; <html>
import [Link].*; <applet code="[Link]"
width="300"
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

public class First extends Applet height="300"


{ </html>
public void paint(Graphics g)
{
[Link]("HELLO
JAVA",50,150);
}

}
Output
Viva Question
Experiment No. 13

Date of
Conduction
Date of
Submission
Objective Write a Program to show Connectivity using JDBC

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory
Program
Output
Viva Question
Experiment No. 14

Date of
Conduction
Date of
Submission
Objective Write a program to demonstrate multithreading using Java.

Software Used 1. Java jdk 1.8


2. Notepad
3. Command Prompt

Theory Multithreading:
A multithreaded program contains two or more parts that can run
concurrently. Each part of such a program is called a thread, and each
thread defines a separate path of execution. A thread is a piece of code that
runs in concurrent with other threads has a statically ordered sequence of
instructions. Threads are being extensively used express concurrency on
both single and multiprocessors machines. The Java Virtual Machine
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

allows an application to have multiple threads of execution running


concurrently. Programming a task having multiple threads of control is
known as multithreading or multithreaded programming.

A single multithreaded program can perform two or more tasks


simultaneously. For instance, a text editor can format text at the same time
that it is printing, as long as these two actions are being performed by two
separate threads. Threads are lightweight. They share the same address
space and cooperatively share the same heavyweight process. Interthread
communication is inexpensive, and context switching from one thread to
the next is low cost.
When a Java Virtual Machine starts up, there is usually a single thread
which typically calls the method named main. The Java Virtual Machine
continues to execute threads until either of the following occurs:
 The exit method of class Runtime has been called and the security
manager has permitted the exit operation to take place.
 All threads that are not daemon threads have died, either by
returning from the call to the run method or by throwing an
exception that propagate beyond the run method.

Program class First extends Thread


{
public void run()
{
for(int i=1;i<=150;i++)
[Link]("Thread 1");

}
class Second extends Thread
{
public void run()
{
for(int i=1;i<=50;i++)
[Link]("Hello World");

}
class Third
{
public static void main(String args[])
{
First f1 = new First();
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Second s1 = new Second();


[Link]();
[Link]();
}
}
Output

Viva Question 1. What is a thread?


2. What is multithreading?
3. List methods of creating threads?

You might also like