Java (Lab)
Java (Lab)
1|Page
26 Write a program to use finally statement 42-43
2|Page
Introduction to OOPs
Object-Oriented Programming (OOP) is a programming paradigm that uses
objects, which are instances of classes, to model and organize data and
behavior. In OOP:
1. **Objects**: These are instances of classes and encapsulate both data
(attributes) and behavior (methods).
2. **Classes**: These are blueprints or templates for creating objects. They
define the structure and behavior of objects.
3. **Encapsulation**: Data is encapsulated within objects, and access to it is
controlled by methods, providing data security and modularity.
4. **Inheritance**: Classes can inherit properties and methods from other
classes, promoting code reuse and hierarchy.
5. **Polymorphism**: Objects of different classes can be treated as objects of a
common superclass. This allows for flexibility and dynamic method invocation.
OOP is widely used for designing and building software due to its ability to
model real-world entities, promote code organization, and support code reuse.
3|Page
Features of OOPs
4|Page
Introduction To java
Java is a general-purpose, object-oriented programming language .it is a
platform –neutral language.java is the first programming language that is not
tied to any particular hardware or operating system programs developed in java
can be executed anywhere on any system.
Developed by: Sun Microsystems of USA in 1991.
Original Name: Oak
Introduced by: James Gosling
5|Page
/*1 - Write a program to print a message : Welcome to
Java*/
class Welcome
{
public static void main(String args[])
{
System.out.print("Welcome to Java");
}
}
**********Output**********
6|Page
/*2 - Write a program to find square root of a number*/
import java.lang.Math;
class squareroot
{
public static void main(String args[])
{
double x = 25;
double y;
y = Math.sqrt(x);
System.out.println(y);}
}
**********Output**********
7|Page
/*3 - Write a program to use super function*/
class room
{
int length;
int breadth;
room(int x, int y)
{
length = x;
breadth = y;
}
int area()
{
return length * breadth; } }
class bedroom extends room
{
int height;
bedroom(int x, int y, int z)
{ super(x,y);
height = z; }
8|Page
int volume()
{
return length * breadth * height;}
}
class inherit
{ public static void main (String args[])
{ bedroom room1 = new bedroom(14, 12, 10);
int area1 = room1.area();
int volume1 = room1.volume();
System.out.println("Area = " + area1);
System.out.println("Volume = " + volume1);}
}
**********Output***********
9|Page
/*4 - Write a program to find area of rectangle using
multiple classes*/
class Rectangle
{
int length, width;
void getdata(int x, int y)
{
length = x;
width = y;
}
int rectarea()
{
int area = length * width;
return area;
}
}
class rectarea
{
10 | P a g e
public static void main(String args[])
{
int area1, area2;
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
rect1.length = 15;
rect1.width = 10;
**********Output**********
11 | P a g e
/*5 - Write a program to use Constructor
*/
class rectangle
{
int length;
int breadth;
rectangle(int x, int y)
{
length = x;
breadth = y;
}
int area()
{
return length * breadth;
} }
class constructor
{
public static void main(String args[])
{
12 | P a g e
rectangle room = new rectangle(25,15);
int area = room.area();
System.out.println("Area = " + area);
}
}
*********Output********
13 | P a g e
/* 6 - Write a program using Constructor Overloading*/
class room
{
int length;
int breadth;
room(int x, int y)
{
length = x;
breadth = y;
}
room(int x)
{
length = breadth = x;
}
int area()
{
return length * breadth;
} }
14 | P a g e
class constructorOverloading
{
public static void main(String args[])
{
room room1 = new room(25,15);
int area1 = room1.area();
room room2 = new room(20);
int area2 = room2.area();
System.out.println("Area 1 = "+ area1);
System.out.println("Area 2 = "+ area2);}}
*********Output********
15 | P a g e
/* 7 - Write a program to use static member functions*/
class calculate
{ static int mul(int x, int y)
{ return x*y; }
static int div(int x, int y)
{ return x/y;} }
class calc
{
public static void main(String args[])
{ int a = calculate.mul(4,5);
int b = calculate.div(a,2);
System.out.println("B = "+ b);
} }
**********Output*********
16 | P a g e
/* 8 - Write a program to use nested function*/
class nesting
{
int m, n;
nesting(int x, int y)
{
m=x;
n=y;
}
int largest()
{
if (m>n)
return m;
else
return n;
}
void display()
{
17 | P a g e
int large = largest(); System.out.println("Largest
value = " + large);
}
}
class nestingTest
{
public static void main(String args[])
{
nesting nest = new nesting(50,40);
nest.display();
}
}
*********Output*********
18 | P a g e
/* 9 - Write a program to use super function*/
class room
{ int length;
int breadth;
room(int x, int y)
{
length = x;
breadth = y;
}
int area()
{
return length * breadth;}}
class bedroom extends room
{
int height;
bedroom(int x, int y, int z)
{
super(x,y);
19 | P a g e
height = z; }
int volume()
{
return length * breadth * height; } }
class inherit
{
public static void main (String args[])
{
bedroom room1 = new bedroom(14, 12, 10);int
area1 = room1.area();
int volume1 = room1.volume();
System.out.println("Area = " + area1);
System.out.println("Volume = " + volume1);}}
**********Output***********
20 | P a g e
/*10 - Write a program to use Arithmetic Operators*/
class floatpoint
{
public static void main (String args[])
{
float a = 20.5F, b = 6.4F;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("a+b = " + (a+b));
System.out.println("a-b = " + (a-b));
System.out.println("a*b = " + (a*b));
System.out.println("a/b = " + (a/b));
System.out.println("a%b = " + (a%b));} }
**********Output*********
21 | P a g e
/*11-Write a program to use relational operators*/
class relational
{
public static void main(String args[])
{
float a = 9.6F;
float b = 6.5F;
float c = 5.5F;
System.out.println("a="+a); System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("a<b"+(a<b));
System.out.println("a>b"+(a>b));
System.out.println("a<=b"+(a<=b));
System.out.println("a==b"+(a==b));
System.out.println("a>=b"+(a>=b));
System.out.println("a!=b"+(a!=b));
System.out.println("b==a+b is"+(b==a+b));
22 | P a g e
}
}
*********Output*********
23 | P a g e
/*12-Write a program to use increment operators*/
class increment
{ public static void main(String args[])
{
int m =10;
int n =20;
System.out.println("m ="+m);
System.out.println("n ="+n);
System.out.println("++m ="+ ++m);
System.out.println("n++ ="+ n++);
System.out.println("m ="+m);
System.out.println("n ="+n);} }
*********Output*********
24 | P a g e
/*13-Write a program to use decrement operator*/
class decrement
{ public static void main(String args[])
{
int m =10;
int n =20;
System.out.println("m ="+m);
System.out.println("n ="+n);
System.out.println("--m ="+ --m);
System.out.println("n-- ="+ n--);
System.out.println("m ="+m);
System.out.println("n ="+n); }}
*********Output*********
25 | P a g e
/*14-Write a program to use nested if else statement*/
class NestedIf {
public static void main(String[] args) { int age=25;
int weight=48;
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donateblood");
} else{
System.out.println("You are not eligible to donate
blood");
} } else{
System.out.println("Age must be greater than 18");
}}}
*********Output*********
/*15-Write a program to use break statement*/
class bbb{
public static void main(String args[])
{
System.out.println("show importance of breakstatement:");
{
for (int i=1;i<=10;i++)
{
system.out.println("="+i);if
(i==5)
{
system.out.println("bye");
break;}}}
*******output*******
27 | P a g e
/*16-Write a program to use continue statement*/
class ccc{
public static void main(String args[])
{
System.out.println("show importance of continue
statement:");
{
for (int m=1;m<=3;m++)
{
for(int n=1;n<=2;n++)
{
if (m==n)
continue;
system.out.println("m="+m+"n="+n);}}}}}
*******output******
28 | P a g e
/*17-Write a program to use method overriding
*/
class room
{
int length;
int breadth;
room(int x, int y)
{
length = x;
breadth = y;
}
int area()
{ return length * breadth;} }
class bedroom extends room
{
int height;
bedroom(int x, int y, int z)
{
super(x,y);
29 | P a g e
height = z; }
int volume()
{
return length * breadth * height;
}}
class inherit
{
public static void main (String args[])
{
bedroom room1 = new bedroom(14, 12, 10);int
area1 = room1.area();
int volume1 = room1.volume();
System.out.println("Area = " + area1);
System.out.println("Volume = " + volume1);
}}
**********Output***********
30 | P a g e
/*18-Write a program to use while statement*/
class even{
public static void main(String args[])
{
int count=2;
System.out.println("Even numbers brtween 1 and 15are:");
while(count<=15)
{System.out.println(count+"\n");
count+=2; } }
********output********
31 | P a g e
/*19-Write a program to print even numbers*/
class even{
public static void main(String args[])
{
int count=2;
System.out.println("Even numbers brtween 1 and 15are:");
while(count<=15)
{System.out.println(count+"\n");
count+=2; } }
********output********
32 | P a g e
/*20-Write a program to print matrix using do while loop*/
class dowhiletest
{
public static void main(String args[])
{
int row;
int column;
int y;
System.out.println("Multiplication Table \n");
row=1;
do
{
column=1;
do
{
y=row*column;
System.out.print(" "+y);
column=column+1;
}
33 | P a g e
while(column<=3);
System.out.print("\n");
row=row+1;
}
while(row<=3);
}
}
***********Output***********
34 | P a g e
/*21-Write a program to show compile time error*/
class error1
{public static void main(String args[])
{
System.out.println("hello")}
}
*********Output**********
35 | P a g e
/*22-Write a program to use Applets*/
//Path C/java/bin/HelloJava.java
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello java",10,100);
}}
//Path C/java/bin/HelloJava.html
<html>
<head>
<title>
Welcome to Java Applets
</title></head>
<body>
<center>
<H1>welcome to the world of applets</H1>
36 | P a g e
</center> <applet
code=HelloJava.class
width=400
height=200>
</applet>
</center>
</body>
</html>
*********Output*********
How to implement
37 | P a g e
/*23-Write a program to show run time error*/
class error2
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x=a/(b-c);
System.out.println("x="+x);
int y=a/(b+c);
System.out.println("y="+y);
}}
***********Output***********
38 | P a g e
/*24-Write a program to use try and catch block*/
class error3
{ public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x,y;
try
{x=a/(b-c);} catch(ArithmeticException
e)
{ System.out.println("Division by zero"); }
y=a/(b+c);
System.out.println("y="+y);}}
***********Output***********
39 | P a g e
/*25-Write a program to use multiple try and catch
blocks*/
class error4
{
public static void main(String args[])
{
int a[]={5,10};
int b=5;
try
{
int x=a[2]/b-a[1];
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(ArrayIndexOfBoundsException e)
{
System.out.println("Array index error");
40 | P a g e
}
catch(ArrayStoreException e)
{
System.out.println("wrong data type");
}
int y=a[1]/a[0];
System.out.println("y="+y);
}
}
**********Output**********
41 | P a g e
/*26-Write a program to use finally statement*/
class error5
{
public static void main(String args[])
{
int a[]={5,10};
int b=5;
try
{
int x=a[2]/b-a[1];
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(ArrayStoreException e)
{
42 | P a g e
System.out.println("wrong data type");
}
finally
{
int y=a[1]/a[0];
System.out.println("y="+y);
}
}
}
*********Output**********
43 | P a g e
/*27-Write a program to sort a string
*/
class StringOrdering
{
static String
name[]={"Madras","Delhi","Ahemdabad","Calcutta","B
ombay"};
public static void main(String args[])
{
int size=name.length;
String temp=null;
for(int i=0; i<size; i++)
{
for(int j=i+1; j<size; j++)
{
if(name[j].compareTo(name[i])<0)
{
temp=name[i];
44 | P a g e
name[i]=name[j];
name[j]=temp;
} } }
for(int i=0; i<size; i++)
{
System.out.println(name[i]);
} } }
**********Output**********
45 | P a g e
/*28-Write a program to use two dimensional array*/
class multable
{ final static int ROWS=5;
final static int COLUMNS=5;
public static void main(String args[ ] )
{ int product[][]=new int [ROWS][COLUMNS];
int row,column;
System.out.println("MULTIPLICATION TABLE");
System.out.println(" ");
int i,j;
for(i=1;i<ROWS;i++)
{ for (j=1;j<COLUMNS;j++)
{ product [i] [j] = i*j; System.out.print("
" +product[i][j]);
} System.out.println(" ");
} }}
********Output*********
46 | P a g e
/*29-Write a program to use interfaces*/
interface Area
{
final static float pi=3.14F;
float compute(float x,float y);
}
class rectangle implements Area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class circle implements Area
{
public float compute(float x,float y)
{
return(pi*x*x);
}
47 | P a g e
}
class interfacetest
{
public static void main(String args[])
{
rectangle rect=new rectangle();
circle cir=new circle();
Area area;
area=rect;
System.out.println("area of
rectangle="+area.compute(10,20));
area=cir;
System.out.println("area of
circle="+area.compute(10,0));} }
**********Output**********
48 | P a g e
/*30-Write a program to create a package*/
package package1;
public class A
{
public void displayA()
{
System.out.println("classA");} }
//Program to access a package
import package1.A;
class packageTest1
{
public static void main(String args[])
{
A objectA=new A();
objectA.displayA(); } }
**********Output**********
49 | P a g e