Java OOP Lab Record for B.Tech Students
Java OOP Lab Record for B.Tech Students
(Autonomous)
LAB RECORD
GODAVARI INSTITUTE OF ENGINEERING & TECHNOLOGY (A)
CERTIFICATE
Mr./Ms.
A Student of with Pin No:
in the Laboratory during the Academic year
Examiner–1 Examiner–2
INDEX
Exp Date Name of the Experiment Page Signature Remarks
No: No of Faculty
1 Write a JAVA program to display default value of
all primi ve data types of JAVA
2 Write a JAVA program that displays the roots of a
quadra c equa on ax2+bx+c=0. Calculate the
discriminate D and basing on the value of D,
describe the nature of roots.
3 Write a JAVA program to display the Fibonacci
sequence
4 Write a JAVA program give example for command
line arguments.
5 Write a JAVA program to give the example for
‘this’ operator. And also use ‘this’ keyword as
return statement.
6 Write a JAVA program to demonstrate sta c
variables, methods, and blocks.
7 Write a JAVA program to search for an element in
a given list of elements (linear search).
8 Write a JAVA program to search for an element in
a given list of elements using binary search
mechanism.
9 Write a JAVA program to sort given list of
numbers.
10 Write a JAVA program to sort an array of strings
11 Write a JAVA program to check whether given
string is palindrome or not.
12 Write a JAVA program to determine the addi on
of two matrices.
13 Write a JAVA program to determine mul plica on
of two matrices.
14 Write a JAVA program for the following
a. Example for call by value.
b. Example for call by reference
15 Write a JAVA program that illustrates simple
inheritance.
16 Write a JAVA program that illustrates mul -level
inheritance
17 Write a JAVA program demonstra ng the
difference between method overloading and
method overriding.
18 Write a JAVA program demonstra ng the
difference between method overloading and
constructor overloading.
19 Write a JAVA program to give the example for
‘super’ keyword.
20 Write a JAVA program illustra ng mul ple
inheritance using interfaces.
PIN: 1
21 Write a JAVA program to illustrate the concept of
final keyword in the program.
22 . Write a JAVA program to create a package named
pl, and implement this package in ex1 class.
23 Write a JAVA program to create a package named
mypack and import it in circle class.
24 Write a JAVA program to give a simple example for
abstract class.
25 Write a JAVA program that describes excep on
handling mechanism
26 Write a JAVA program for example of try and catch
block. In this check whether the given array size is
nega ve or not.
27 Write a JAVA program to illustrate sub class
excep on precedence over base class
28 Write a JAVA program for handling of user defined
excep on by using throw.
29 . Write a JAVA program to illustrate the concept of
throws keyword
30 Write a JAVA program to illustrate crea on of
threads using runnable class.(start method start
each of the newly created thread. Inside the run
method there is sleep() for suspend the thread for
500 milliseconds).
31 Write a JAVA program to create a class MyThread
in this class a constructor, call the base class
constructor, using super and starts the thread.
The run method of the class starts a er this. It can
be observed that both main thread and created
child thread are executed concurrently
32 Write a JAVA program to illustrate the concept of
thread synchroniza on
33 Write Java program by implemen ng the
concepts of different collec ons as list, map and
set
34 Write a JAVA program that describes the life cycle
of an applet.
35 Write a JAVA program to design a laughing baby
face.
36 Write a JAVA program to create a simple
calculator
PIN: 2
INTRODUCTION TO JAVA:
JAVA was developed by James Gosling at Sun Microsystems Inc. in the year 1991, later acquired by Oracle
Corpora on. It is a simple programming language. Java makes wri ng, compiling, and debugging
programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementa on
dependencies as possible. A general-purpose programming language made for developers to write once run
anywhere that is compiled Java code can run on all pla orms that support Java. Java applica ons are
compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to C/C++.
HISTORY:
Java’s history is very interes ng. It is a programming language created in 1991. James Gosling,
Mike Sheridan, and Patrick Naught on, a team of Sun engineers known as the Green team ini ated the Java
language in 1991. Sun Microsystems released its first public implementa on in 1996 as Java 1.0. It provides
no-cost -run- mes on popular pla orms. Java1.0 compiler was re-wri en in Java by Arthur Van Hoff to
strictly comply with its specifica ons. With the arrival of Java 2, new versions had mul ple configura ons
built for different types of pla orms.
In 1997, Sun Microsystems approached the ISO standards body and later formalized Java, but it
soon withdrew from the process. At one me, Sun made most of its Java implementa ons available without
charge, despite their proprietary so ware status. Sun generated revenue from Java through the selling of
licenses for specialized products such as the Java Enterprise System.
On November 13, 2006, Sun released much of its Java virtual machine as free, open-source
so ware. On May 8, 2007, Sun finished the process, making all of its JVM’s core code available under open-
source distribu on terms.
The principles for crea ng java were simple, robust, secured, high performance, portable, mul -threaded,
interpreted, dynamic, etc. James Gosling in 1995 developed Java, who is known as the Father of Java.
Currently, Java is used in mobile devices, internet programming, games, e-business, etc.
FEATURES OF JAVA:
OBJECT ORIENTED
In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
PLATFORM INDEPENDENT
Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled
into pla orm specific machine, rather into pla orm-independent byte code. This byte code is distributed
over the web and interpreted by the Virtual Machine (JVM) on whichever pla orm it is being run on.
SIMPLE
Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to
master.
SECURE
With Java's secure feature it enables to develop virus-free, tamper-free systems. Authen ca on
techniques are based on public-key encryp on.
PIN: 3
ARCHITECTURE-NEUTRAL
Java compiler generates an architecture-neutral object file format, which makes the compiled
code executable on many processors, with the presence of Java run me system.
PORTABLE
Being architecture-neutral and having no implementa on dependent aspects of the specifica on makes
Java portable. The compiler in Java is wri en in ANSI C with a clean portability boundary, which is a POSIX
subset.
ROBUST
Java makes an effort to eliminate error-prone situa ons by emphasizing mainly on compile me error
checking and run me checking.
MULTITHREADED
With Java's mul threaded feature it is possible to write programs that can perform many tasks
simultaneously. This design feature allows the developers to construct interac ve applica ons that can
run smoothly.
INTERPRETED
Java byte code is translated on the fly to na ve machine instruc ons and is not stored anywhere. The
development process is more rapid and analy cal since the linking is an incremental and light-weight
process.
HIGH PERFORMANCE
With the use of Just-In-Time compilers, Java enables high performance.
DISTRIBUTED
Java is designed for the distributed environment of the internet.
DYNAMIC
Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving
environment. Java programs can carry an extensive amount of run- me informa on that can be used to
verify and resolve accesses to objects at run- me.
PIN: 4
EXPERIMENT: - 1
AIM: Write a JAVA program to display default value of all primitive data types of JAVA.
PROGRAM:
public class LabTask1
{
static boolean val1;
static double val2;
static float val3;
static int val4;
static long val5;
static String val6;
public static void main (String args[])
{
[Link]("Default values....");
[Link]("Boolean default is = " + val1);
[Link]("Double default is = " + val2);
[Link]("Float default is = " + val3);
[Link]("Int default is = " + val4);
[Link]("Long default is = " + val5);
[Link]("String default is = " + val6);
}
}
PIN: 5
PIN: 6
EXPERIMENT: - 2
AIM: Write a JAVA program that displays the roots of a quadratic equation ax2+bx+c=0.
Calculate the discriminate D and basing on the value of D, describe the nature of roots .
PROGRAM:
import java. [Link];
double a=[Link]();
double b=[Link]();
double c=[Link]();
double d=b*b-4.0*a*c;
if (d>0.0)
double r1=(-b+[Link](d,0.5))/(2.0*a);
double r2=(-[Link](d,0.5))/(2.0*a);
else if (d==0.0)
PIN: 7
double r1=-b/(2.0*a);
else
[Link]("IMAGINARY ROOTS");
PIN: 8
PIN: 9
EXPERIMENT: - 3
AIM: Write a JAVA program to display the Fibonacci sequence.
PROGRAM:
import [Link];
int n1=0,n2=1,n3;
int k=[Link]();
[Link](n1+" "+n2);
for(int i=2;i<k;i++)
n3=n1+n2;
[Link](" "+n3);
n1=n2;
n2=n3;
PIN: 10
PIN: 11
EXPERIMENT: - 4
AIM: Write a JAVA program give example for command line arguments.
PROGRAM:
int x=[Link](args[0]);
int y=[Link](args[1]);
int sum=x+y;
PIN: 12
PIN: 13
EXPERIMENT: - 5
AIM: Write a JAVA program to give the example for ‘this’ operator. And also use ‘this’
keyword as return statement.
PROGRAM:
import [Link];
int num1=[Link]();
int num2=[Link]();
this.num1=num1;
this.num2=num2;
return this;
if(num1>num2)
[Link]("num1 is bigger");
else
PIN: 14
[Link]("num2 is bigger");
obj=[Link]();
[Link]();
PIN: 15
PIN: 16
EXPERIMENT: - 6
AIM: Write a JAVA program to demonstrate static variables, methods, and blocks.
PROGRAM:
static int y;
[Link]("s: "+s);
[Link]("x= "+x);
[Link]("y= "+y);
[Link]("z= "+z);
static
y=x-98;
fun(10);
}}
PIN: 17
PIN: 18
EXPERIMENT: - 7
AIM: Write a JAVA program to search for an element in a given list of elements (linear
search).
PROGRAM:
import [Link];
int arr[],n,search,i;
n=[Link]();
arr=new int[n];
for(i=0;i<n;i++)
arr[i]=[Link]();
search=[Link]();
for(i=0;i<n;i++)
if (arr[i]==search)
PIN: 19
break;
if(i==n)
PIN: 20
PIN: 21
EXPERIMENT: - 8
AIM: To Write a JAVA program to search for an element in a given list of elements using
binary search mechanism.
PROGRAM:
import [Link].*;
class labtask8
while( l<=h )
if (a[mid]<key )
l=mid+1;
break;
else
h=mid-1;
PIN: 22
}
mid=(l+h)/2;
if (l>h)
[Link]("Enter n");
n=[Link]();
a=new int[n];
for(i=0;i<n;i++)
a[i]=[Link]();
[Link]("Enter key");
key=[Link]();
l=0;
h=n-1;
bsearch(a,l,h,key); }}
PIN: 23
PIN: 24
EXPERIMENT: - 9
AIM: To Write a JAVA program to sort given list of numbers.
PROGRAM:
import [Link].*;
class labtask9
[Link]("Enter n");
int n=[Link]();
for(i=0;i<n;i++)
a[i]=[Link]();
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if (a[i]>a[j])
PIN: 25
int t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<n;i++)
[Link](a[i]+" ");
PIN: 26
PIN: 27
EXPERIMENT: - 10
AIM: To Write a JAVA program to sort an array of strings.
PROGRAM:
import [Link].*;
class labtask10
[Link]("enter n");
int n=[Link]();
for(i=0;i<n;i++)
a[i]=[Link]();
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(a[i].compareTo(a[j])>0)
PIN: 28
{
String t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<n;i++)
[Link]([Link](a));
break;
PIN: 29
PIN: 30
EXPERIMENT: - 11
AIM: To Write a JAVA program to check whether given string is palindrome or not.
PROGRAM:
import [Link].*;
class LabTask11
if ([Link](rev))
{
[Link](str+" is a palindrome string.");
}
else
{
[Link](str+" is not a palindrome string.");
}
}
PIN: 31
PIN: 32
EXPERIMENT: -12
AIM: To Write a JAVA program to determine the addition on of two matrices.
PROGRAM:
import [Link].*;
class LabTask12
rows=[Link]();
cols=[Link]();
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
a[i][j]=[Link]();
PIN: 33
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
b[i][j]=[Link]();
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
sum[i][j]=a[i][j]+b[i][j];
for(i=0;i<rows;i++){
for(j=0;j<cols;j++)
[Link](sum[i][j]+"\t");
[Link]();
PIN: 34
PIN: 35
EXPERIMENT:-13
AIM: To Write a JAVA program to determine multiplication of two matrices.
PROGRAM:
import [Link].*;
class LabTask13
r1=[Link]();
c1=[Link]();
r2=[Link]();
c2=[Link]();
if(c1!=r1)
return ;
PIN: 36
[Link]("Enter first matrix elements:");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
a[i][j]=[Link]();
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
b[i][j]=[Link]();
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
mul[i][j]=0;
for(k=0;k<c1;k++){
mul[i][j]=a[i][k]*b[k][j];
[Link](mul[i][j]+" ");
[Link](); }}}
PIN: 37
PIN: 38
EXPERIMENT:-:14(A)
AIM: To write a JAVA program for Example for call by value.
PROGRAM:
class Lab14a
int x,y,temp;
temp=x;
x=y;
y=temp;
[Link]("a er swapping");
[Link]("x="+x);
[Link]("y="+y);
a.x=46;
a.y=17;
a.m1(46,17);
[Link]("before swapping");
[Link]("x="+a.x+"y="+a.y);
PIN: 39
}
PIN: 40
EXPERIMENT:-:14(B)
AIM: To write a JAVA program for example for call by Reference.
PROGRAM:
class Lab14b
int a,b;
x.a=17;
x.b=46;
[Link]("a="+x.a+",b="+x.b); change(x);
[Link]("A er changing...");
[Link]("a="+x.a+",b="+x.b);
A.a=46;
A.b=17;
PIN: 41
PIN: 42
EXPERIMENT:-15
AIM: TO Write a JAVA program that illustrates simple inheritance.
PROGRAM:
class LabTask15
int a=10;
void friend()
[Link]("my friends");
int b=20;
void bestu()
[Link](n1.a);
PIN: 43
Best n2=new Best();
[Link](n2.b);
[Link]();
}}
class A
int a=10;
void read()
class B extends A
int b=20;
void add()
[Link]("This is addition");
PIN: 44
PIN: 45
EXPERIMENT:-16
AIM: To Write a JAVA program that illustrates multi-level inheritance.
PROGRAM:
class Animal
void eat()
[Link]("ea ng...");
void bark()
[Link]("barking...");
void weep()
[Link]("weeping...");
PIN: 46
[Link]();
[Link]();
[Link]();
PIN: 47
PIN: 48
EXPERIMENT:-17
AIM: To Write a JAVA program demonstrating the difference between method overloading
and method overriding.
PROGRAM:
//Method Overloading
classMotor Bike
[Link] = method;
[Link]();
[Link]("Self");
PIN: 49
//Method Overriding
classMotor Bike
[Link]();
PIN: 50
PIN: 51
EXPERIMENT:-18
AIM: To Write a JAVA program demonstrating the difference between method overloading
and constructor overloading.
PROGRAM:
//Method Overloading
classMotorBike
[Link] = method;
[Link]();
[Link]("Self");
PIN: 52
LABTASK18B
//Constructor Overloading
class LabTask18b
String lang;
LabTask18b()
[Link]="Java";
LabTask18b(String lang)
[Link]=lang;
[Link]();
[Link]();
PIN: 53
PIN: 54
EXPERIMENT:-19
AIM: To Write a JAVA program to give the example for ‘super’ keyword.
PROGRAM:
class Sample
void name()
[Link]("I am Sai");
void name()
[Link]();
[Link]("This is Siva");
[Link]();
PIN: 55
PIN: 56
EXPERIMENT:-20
AIM: To Write a JAVA program illustrating multiple inheritance using interfaces.
PROGRAM:
interface A
int a=50;
interface B
int b=40;
void stmt()
[Link]();
[Link]("Addition is :"+(a+b));
PIN: 57
PIN: 58
EXPERIMENT: -21
AIM: To Write a JAVA program to illustrate the concept of final keyword in the program.
PROGRAM:
final class A
A x=new A();
x.a=46;
PIN: 59
PIN: 60
EXPERIMENT: - 22
AIM: To Write a JAVA program to create a package named pl, and implement this package
in ex1 class.
PROGRAM:
import pl.B;
class ex1
B x=new B();
x.m1();
package pl;
public class B
int a=17;
}}
PIN: 61
PIN: 62
EXPERIMENT:-23
AIM: To Write a JAVA program to create a package named mypack and import it in circle
class.
PROGRAM:
import Mypack.Lab23;
class Circle
x.m1(5);
package Mypack;
[Link]("area of circle="+area);
}}
PIN: 63
PIN: 64
EXPERIMENT:-24
AIM: To Write a JAVA program to give a simple example for abstract class.
PROGRAM:
abstract class A
int a=17,b=46;
void m2()
[Link]("sum="+(a+b));
class B extends A
void m1()
[Link]("sub="+(b-a));
B x=new B();
x.m2();
x.m1();
}}
PIN: 65
PIN: 66
EXPERIMENT:-25
AIM: To Write a JAVA program that describes exception handling mechanism.
PROGRAM:
class Lab25
try
int a=17/0;
catch(Exception x)
[Link](x);
PIN: 67
PIN: 68
EXPERIMENT:-26
AIM: To Write a JAVA program for example of try and catch block. In this check whether
the given array size is negative or not.
PROGRAM:
import [Link].*;
class Lab26
[Link]("enter size=");
int size=[Link]();
try
{
int a[]=new int[size];
}
catch(Exception e)
{
[Link](e);
[Link]("array size is negative");
[Link](0);
}
finally
{
[Link]("array size is positive");
}
}}
PIN: 69
PIN: 70
EXPERIMENT:-27
AIM: To Write a JAVA program to illustrate sub class exception precedence over base class.
PROGRAM:
class A
{
void m1()throws InterruptedException
{
[Link]("hello");
}
}
class B extends A
{
void m1()throws InterruptedException
{
for(int i=0;i<5;i++)
{
[Link](1000);
[Link]("Welcome");
B x=new B();
try
x.m1();
PIN: 71
PIN: 72
EXPERIMENT:-28
AIM: To Write a JAVA program for handling of user defined exception by using throw.
PROGRAM:
import [Link].*;
B(String a)
super(a);
int balance=1700;
[Link]("Enter amount=");
int amount=[Link]();
if(amount>balance)
{
throw new B("Invalid balance");
}
else
{
[Link]("Your Amount ="+amount);
}
}
PIN: 73
PIN: 74
EXPERIMENT:-29
AIM: To Write a JAVA program to illustrate the concept of throws keyword.
PROGRAM:
class A
for(int i=0;i<5;i++)
[Link](1000);
[Link]("Good Morning");
m1();
A x=new A();
x.m2();
PIN: 75
PIN: 76
EXPERIMENT:-30
AIM: Write a JAVA program to illustrate crea on of threads using runnable class.(start method
start each of the newly created thread. Inside the run method there issleep() for suspend the thread
for 500 milliseconds).
PROGRAM:
for(int i=0;i<5;i++)
try
[Link](500);
[Link]("Hello");
catch(Exception e){}
class B
A x=new A();
PIN: 77
Thread t1=new Thread(x);
[Link]();
PIN: 78
PIN: 79
EXPERIMENT:-31
AIM: To write a JAVA program to create a class MyThread in this class a constructor, call
the base class constructor, using super and starts the thread. The run method of the class starts
a er this. It can be observed that both main thread and created child thread are executed
concurrently.
PROGRAM:
Sample()
super();
start();
for(int i=0;i<3;i++)
try
[Link]([Link]().getName()+" - "+i);
[Link](1000);
catch(InterruptedException e)
[Link]("hii");
PIN: 80
}
class Main
for(int i=0;i<3;i++)
try
[Link]([Link]().getName()+" - "+i);
[Link](1000);
catch(InterruptedException e)
[Link]("hello");
PIN: 81
PIN: 82
EXPERIMENT:-32
AIM: To Write a JAVA program to illustrate the concept of thread synchronization.
PROGRAM:
class Display
for(int i=0;i<3;i++)
[Link]("Good morning");
try
[Link](1000);
catch(InterruptedException e)
{}
[Link](name);
Display d;
String name;
PIN: 83
My(Display d,String name)
this.d=d;
[Link]=name;
[Link](name);
[Link]();
PIN: 84
PIN: 85
EXPERIMENT:-33
AIM: To Write Java program by implementing the concepts of different collections as list,
map and set.
PROGRAM:
import [Link].*;
[Link]("India");
[Link]("America");
[Link]("Japan");
[Link]("China");
[Link]("America");
//add an Iterator to hs
while([Link]())
PIN: 86
{
String s=(String)[Link]();
[Link](s);
Stack<Integer>st=new Stack<Integer>();
[Link](new Integer(10));
[Link](new Integer(20));
[Link](new Integer(30));
[Link](new Integer(40));
[Link](st);
[Link](new Integer(101),"Naresh");
[Link](new Integer(102),"Rajesh");
[Link](new Integer(103),"Suresh");
[Link](new Integer(104),"Mahesh");
[Link](new Integer(105),"Ramesh");
set=[Link]();
PIN: 87
[Link](set);
PIN: 88
PIN: 89
EXPERIMENT:-34
AIM: Write a JAVA program that describes the life cycle of an applet.
PROGRAM:
import [Link].*;
import [Link].*;
</applet>*/
String str;
setBackground([Link]);
setForeground([Link]);
[Link](f);
PIN: 90
[Link](str,200,200);
PIN: 91
OUTPUT
PIN: 92
PIN: 93
EXPERIMENT:-35
AIM: Write a JAVA program to design a laughing baby face.
PROGRAM:
import [Link].*;
import [Link].*;
</applet>*/
setBackground([Link]);
setForeground([Link]);
[Link](f);
[Link](75,460,40,40);
[Link](75,460,40,40);
[Link](147,460,147,560);
[Link](175,460,40,40);
[Link](175,460,40,40);
[Link](20,400,260,260);
PIN: 94
[Link](80,535,135,80,180,180);
OUTPUT
PIN: 95
PIN: 96
EXPERIMENT:-36
AIM: Write a JAVA program to create a simple calculator
PROGRAM:
import [Link].*;
import [Link].*;
import [Link].*;
</applet>*/
String str="";
TextField t1,t2,t3;
Button b1,b2,b3,b4,b5;
Label l1,l2,l3;
add(l1);
add(l2);
add(l3);
t1=new TextField(10);
add(t1);
PIN: 97
t2=new TextField(10);
add(t2);
t3=new TextField(10);
add(t3);
b1=new Button("add");
b2=new Button("sub");
b3=new Button("mul");
b4=new Button("div");
b5=new Button("mod");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
[Link](50,100,100,20);
[Link](50,140,100,20);
[Link](50,180,100,20);
[Link](200,100,100,20);
[Link](200,140,100,20);
[Link](200,180,100,20);
[Link](50,250,50,20);
[Link](110,250,50,20);
[Link](170,250,50,20);
[Link](230,250,50,20);
PIN: 98
[Link](290,250,50,20);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
setLayout(null);
setVisible(true);
setSize(400,350);
setBackground([Link]);
setForeground([Link]);
str=[Link]();
double a=[Link]([Link]());
double b= [Link]([Link]());
if(str=="add")
double sum=a+b;
[Link](""+sum);
else if(str=="sub")
PIN: 99
{
double sub=a-b;
[Link](""+sub);
else if(str=="mul")
doublemul=a*b;
[Link](""+mul);
else if(str=="div")
double div=a/b;
[Link](""+div);
else if(str=="mod")
int x=[Link]([Link]());
int y=[Link]([Link]());
int mod=x%y;
[Link](""+mod);
repaint();
PIN: 100
OUTPUT:
PIN: 101