0% found this document useful (0 votes)
15 views103 pages

Java OOP Lab Record for B.Tech Students

The document is a lab record for Object Oriented Programming through Java at Godavari Institute of Engineering & Technology. It includes a list of experiments, each with specific programming tasks such as displaying default values of data types, solving quadratic equations, and implementing search algorithms. Additionally, it provides an introduction to Java, its history, features, and various programming examples.

Uploaded by

paridalarakesh
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)
15 views103 pages

Java OOP Lab Record for B.Tech Students

The document is a lab record for Object Oriented Programming through Java at Godavari Institute of Engineering & Technology. It includes a list of experiments, each with specific programming tasks such as displaying default values of data types, solving quadratic equations, and implementing search algorithms. Additionally, it provides an introduction to Java, its history, features, and various programming examples.

Uploaded by

paridalarakesh
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

GODAVARI INSTITUTE OF ENGINEERING & TECHNOLOGY

(Autonomous)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING (AIML & CS)

II [Link] I Sem GRBT-20

Object Oriented Programming


Through JAVA

LAB RECORD
GODAVARI INSTITUTE OF ENGINEERING & TECHNOLOGY (A)

Department of COMPUTER SCIENCE & ENGINEEERING (AIML&CS)

Name: Pin No:

CERTIFICATE

Certified that this is the bonafide record of practical work done by

Mr./Ms.
A Student of with Pin No:
in the Laboratory during the Academic year

No. of Experiments Conducted: No. of Experiments attended:

Faculty In-charge Head of the Department

Submitted for Practical Examination

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];

public class LabTask2

public static void main(String args[])

Scanner input=new Scanner([Link]);

[Link]("Enter value of a:");

double a=[Link]();

[Link]("Enter value of b:");

double b=[Link]();

[Link]("Enter value of c:");

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);

[Link]("THE ROOTS ARE "+r1+" AND "+r2);

else if (d==0.0)

PIN: 7
double r1=-b/(2.0*a);

[Link]("ROOTS ARE REAL AND EQUAL");

[Link]("The root is "+r1);

else

[Link]("IMAGINARY ROOTS");

PIN: 8
PIN: 9
EXPERIMENT: - 3
AIM: Write a JAVA program to display the Fibonacci sequence.
PROGRAM:
import [Link];

public class LabTask3

public static void main(String args[])

int n1=0,n2=1,n3;

Scanner sc=new Scanner([Link]);

[Link]("Enter 'k' value:");

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:

public class LabTask4

public static void main(String args[])

int x=[Link](args[0]);

int y=[Link](args[1]);

int sum=x+y;

[Link]("Sum of two numbers is:"+sum);

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];

public class biggest{

private int num1;

private int num2;

public biggest SetValues()

Scanner sc= new Scanner([Link]);

[Link]("Enter 'num1' value: ");

int num1=[Link]();

[Link]("Enter 'num2' value: ");

int num2=[Link]();

this.num1=num1;

this.num2=num2;

return this;

public void display()

if(num1>num2)

[Link]("num1 is bigger");

else

PIN: 14
[Link]("num2 is bigger");

public static void main(String args[])

biggest obj = new biggest();

obj=[Link]();

[Link]();

PIN: 15
PIN: 16
EXPERIMENT: - 6
AIM: Write a JAVA program to demonstrate static variables, methods, and blocks.

PROGRAM:

public class LabTask6{

static String s="HITLER";

static int x=245;

static int y;

static void fun(int z)

[Link]("s: "+s);

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

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

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

static

[Link]("static block is invoked");

y=x-98;

public static void main(String args[])

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];

public class LabTask7

public static void main(String args[])

int arr[],n,search,i;

Scanner sc=new Scanner([Link]);

[Link]("Enter number of elements: ");

n=[Link]();

arr=new int[n];

[Link]("Enter "+n+" elements: ");

for(i=0;i<n;i++)

arr[i]=[Link]();

[Link]("Enter the value to find: ");

search=[Link]();

for(i=0;i<n;i++)

if (arr[i]==search)

[Link](search+" is found at location "+(i+1));

PIN: 19
break;

if(i==n)

[Link](search+" isn't found in array");

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

static int a[],l,h,n,i,key;

static void bsearch(int a[],int l,int h,int key)

int mid = (l+h)/2;

while( l<=h )

if (a[mid]<key )

l=mid+1;

else if ( a[mid] == key )

[Link]("Element is found at location : " + (mid+1));

break;

else

h=mid-1;

PIN: 22
}

mid=(l+h)/2;

if (l>h)

[Link]("Element is not found");

public static void main(String[] args)

Scanner sc=new Scanner([Link]);

[Link]("Enter n");

n=[Link]();

a=new int[n];

[Link]("Enter "+n+" elements ");

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

static int a[],n,i,j,t;

public static void main(String[] args)

Scanner sc=new Scanner([Link]);

[Link]("Enter n");

int n=[Link]();

int a[]=new int[n];

[Link]("Enter "+n+" elements ");

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

static String a[],t;

static int n,i,j;

public static void main(String[] args)

Scanner sc=new Scanner([Link]);

[Link]("enter n");

int n=[Link]();

String a[]=new String[n];

[Link]("enter "+n+" strings");

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

public static void main(String args[])

String str, rev = "";


Scanner sc = new Scanner([Link]);
[Link]("Enter the string:");
str= [Link]();
int len= [Link]();
for (int i = len-1;i>=0;i--)

rev= rev+ [Link](i);

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

static int i,j,rows,cols;

public static void main(String... args)

Scanner sc=new Scanner([Link]);

[Link]("Enter the [Link] rows: ");

rows=[Link]();

[Link]("Enter the [Link] columns: ");

cols=[Link]();

int sum[][]=new int[rows][cols];

[Link]("Enter first matrix elements:");

int a[][]=new int[rows][cols];

for(i=0;i<rows;i++){

for(j=0;j<cols;j++){

a[i][j]=[Link]();

[Link]("Enter second matrix elements:");

int b[][]=new int[rows][cols];

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];

[Link]("The resultant matrix is:");

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

static int i,j,r1,c1,r2,c2,k;

public static void main(String... args)

Scanner sc=new Scanner([Link]);

[Link]("Enter the [Link] rows of 1st matrix: ");

r1=[Link]();

[Link]("Enter the [Link] columns of 1st matrix: ");

c1=[Link]();

[Link]("Enter the [Link] rows of 2nd matrix: ");

r2=[Link]();

[Link]("Enter the [Link] columns of 2nd matrix: ");

c2=[Link]();

if(c1!=r1)

[Link]("matrix multiplication is not possible");

return ;

int mul[][]=new int[r1][c2];

PIN: 36
[Link]("Enter first matrix elements:");

int a[][]=new int[r1][c1];

for(i=0;i<r1;i++){

for(j=0;j<c1;j++){

a[i][j]=[Link]();

[Link]("Enter second matrix elements:");

int b[][]=new int[r2][c2];

for(i=0;i<r2;i++){

for(j=0;j<c2;j++){

b[i][j]=[Link]();

[Link]("Resultant matrix is:");

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;

void m1(int x,int y)

temp=x;

x=y;

y=temp;

[Link]("a er swapping");

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

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

public sta c void main(String... args)

Lab14a a=new Lab14a();

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;

public static void main(String args[])

Lab14b x=new Lab14b();

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);

public static void change(Lab14b A)

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");

class Best extends LabTask15

int b=20;

void bestu()

[Link]("My Bestie1 is Siva.");

[Link]("My Bestie2 is Sai.");

public static void main(String[] args)

LabTask15 n1=new LabTask15();

[Link](n1.a);

PIN: 43
Best n2=new Best();

[Link](n2.b);

[Link]();

}}

class A

int a=10;

void read()

[Link]("This is read method");

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...");

class Dog extends Animal

void bark()

[Link]("barking...");

Class BabyDog extends Dog

void weep()

[Link]("weeping...");

public class LabTask16

public sta c void main(String args[])

BabyDog d=new BabyDog();

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

private String startMethod = "Kick";

public void start()

[Link](startMethod+" star ng...");

public void start(String method)

[Link] = method;

[Link](startMethod+" star ng...");

public class LabTask17a

public sta c void main(String args[])

MotorBike b=new MotorBike();

[Link]();

[Link]("Self");

PIN: 49
//Method Overriding

classMotor Bike

public void start()

[Link]("Using kick paddle to start...");

classSelfStartMotorBike extends MotorBike

public void start()

[Link]("Using self start bu on to start...");

public class LabTask17b

public sta c void main(String args[])

SelfStartMotorBike b=new SelfStartMotorBike();

[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

private String startMethod = "Kick";

public void start()

[Link](startMethod+" star ng...");

public void start(String method)

[Link] = method;

[Link](startMethod+" star ng...");

public class LabTask18a

public sta c void main(String args[])

MotorBike b=new MotorBike();

[Link]();

[Link]("Self");

PIN: 52
LABTASK18B

//Constructor Overloading

class LabTask18b

String lang;

LabTask18b()

[Link]="Java";

LabTask18b(String lang)

[Link]=lang;

public void getLang()

[Link]("Programming Langauage: "+[Link]);

public sta c void main(String[] args)

LabTask18b obj1=new LabTask18b();

LabTask18b obj2=new LabTask18b("C++");

[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");

class Task19 extends Sample

void name()

[Link]();

[Link]("This is Siva");

public static void main(String[] args)

Task19 t=new Task19();

[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;

class Task20 implements A,B

void stmt()

[Link]("I have Rights on both A & B");

public static void main(String[] args)

Task20 x=new Task20();

[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

final int a=17;

public static void main(String... args )

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

public static void main(String... Y)

B x=new B();

x.m1();

package pl;

public class B

public void m1()

int a=17;

[Link]("a value is="+a);

}}

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

public static void main(String... Y)

Lab23 x=new Lab23();

x.m1(5);

package Mypack;

public class Lab23

public void m1(int r)

double pi=3.14,area; area=pi*r*r;

[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));

public static void main (String args[])

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

public static void main(String[] args)

try

int a=17/0;

catch(Exception x)

[Link](x);

[Link]("rest of the code");

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

public static void main(String args[])

Scanner x=new Scanner([Link]);

[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");

public static void main(String args[])


{

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].*;

class B extends RuntimeException

B(String a)

super(a);

public static void main(String... args)

int balance=1700;

Scanner x=new Scanner([Link]);

[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

void m1()throws InterruptedException

for(int i=0;i<5;i++)

[Link](1000);

[Link]("Good Morning");

void m2()throws InterruptedException

m1();

public static void main(String args[])throws InterruptedException

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:

class A implements Runnable

public void run()

for(int i=0;i<5;i++)

try

[Link](500);

[Link]("Hello");

catch(Exception e){}

class B

public static void main(String... args)

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:

class Sample extends Thread

Sample()

super();

start();

public void run()

for(int i=0;i<3;i++)

try

[Link]([Link]().getName()+" - "+i);

[Link](1000);

catch(InterruptedException e)

[Link]("hii");

PIN: 80
}

class Main

public static void main(String[] args)

Sample t1=new Sample();

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

synchronized public void wish(String name)

for(int i=0;i<3;i++)

[Link]("Good morning");

try

[Link](1000);

catch(InterruptedException e)

{}

[Link](name);

class My extends Thread

Display d;

String name;

PIN: 83
My(Display d,String name)

this.d=d;

[Link]=name;

public void run()

[Link](name);

class B{ public static void main(String... args)

Display d=new Display();

My t=new My(d,"Sai ");

[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].*;

public class LabTask33

public static void main(String args[])

//create a HashSet to store Strings

HashSet<String> hs=new HashSet<String>();

//Store some String elements

[Link]("India");

[Link]("America");

[Link]("Japan");

[Link]("China");

[Link]("America");

//view the HashSet

[Link] ("HashSet = " + hs);

//add an Iterator to hs

Iterator it = [Link] ();

//display element by element using Iterator

[Link]("Elements Using Iterator: ");

while([Link]())

PIN: 86
{

String s=(String)[Link]();

[Link](s);

//create an empty stack to contain Integer objects

Stack<Integer>st=new Stack<Integer>();

[Link](new Integer(10));

[Link](new Integer(20));

[Link](new Integer(30));

[Link](new Integer(40));

[Link] (new Integer(50));

[Link](st);

[Link]("Element at top of the stack is: "+[Link]());

[Link]("Removing element at the TOP of the stack: "+[Link]());

[Link]("The new stack is: "+st);

HashMap<Integer,String> hm=new HashMap<Integer,String>();

[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<Integer> set=new HashSet<Integer>();

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 code="[Link]" width="500" height="500">

</applet>*/

public class LabTask34 extends Applet

String str;

public void init()

str="Welcome to Java Applet";

[Link]("Inside init method");

setBackground([Link]);

setForeground([Link]);

public void start()

[Link]("Inside start method");

public void paint(Graphics g)

Font f=new Font("Arial",3,27);

[Link](f);

PIN: 90
[Link](str,200,200);

[Link]("Inside paint method");

public void stop()

[Link]("Inside stop method");

public void destroy()

[Link]("Inside destroy method");

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 code="[Link]" width="600" height="800">

</applet>*/

public class LabTask35 extends Applet

public void init()

setBackground([Link]);

setForeground([Link]);

public void paint(Graphics g)

Font f=new Font("Arial",1,28);

[Link](f);

[Link]("Folks! I'm a Laughing Baby Face",340,550);

[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 code="[Link]" width="700" height="200">

</applet>*/

public class LabTask36 extends Applet implements ActionListener

String str="";

TextField t1,t2,t3;

Button b1,b2,b3,b4,b5;

Label l1,l2,l3;

public void init()

l1=new Label("Enter 1st value:");

add(l1);

l2=new Label("Enter 2nd value:");

add(l2);

l3=new Label("Result: ");

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]);

public void paint(){}

public void actionPerformed(ActionEvent e)

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

You might also like