Table of Content
[Link] Topic [Link] Remarks
1 Write a program to print a message: Welcome to java 6
2 Write a program to find square root of a number. 7
3 Write a program to use command line in java 8-9
4 Write a program to find area of rectangle using multiple classes 10-11
5 Write a program to use constructor in java. 12-13
6 Write a program using constructor overloading 14-15
7 Write a program to use static member function 16
8 Write a program to use nested function 17-18
9 Write a program to use super function in java 19-20
10 Write a program to use arithmetic operations 21
11 Write a program to use relational operators 22-23
12 Write a program to use increment operators 24
13 Write a program to use decrement operators 25
14 Write a program to use nested function 26
15 Write a program to use break statement 27
16 Write a program to use continue statement 28
17 Write a program to use method overriding 29-30
18 Write a program to use while statement 31
19 Write a program to print even numbers 32
20 Write a program to print matrix using do while loop 33-34
21 Write a program to show compile time error 35
22 Write a program to show use of applets 36-37
23 Write a program to show runtime error 38
24 Write a program to use try and catch block 39
25 Write a program to use multiple try and catch blocks 40-41
1|Page
26 Write a program to use finally statement 42-43
27 Write a program to show sorting of strings 44-45
28 Write a program to use two dimensional array 46
29 Write a program to show use of interfaces 47-48
30 Write a program to show use of packages 49
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 [Link] 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[])
{
[Link]("Welcome to Java");
}
}
**********Output**********
6|Page
/*2 - Write a program to find square root of a number*/
import [Link];
class squareroot
{
public static void main(String args[])
{
double x = 25;
double y;
y = [Link](x);
[Link](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 = [Link]();
int volume1 = [Link]();
[Link]("Area = " + area1);
[Link]("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();
[Link] = 15;
[Link] = 10;
area1 = [Link] * [Link];[Link](20,12);
area2 = [Link]();
[Link]("Area 1 = " + area1);
[Link]("Area 2 = " + area2);}}
**********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 = [Link]();
[Link]("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 = [Link]();
room room2 = new room(20);
int area2 = [Link]();
[Link]("Area 1 = "+ area1);
[Link]("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 = [Link](4,5);
int b = [Link](a,2);
[Link]("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(); [Link]("Largest
value = " + large);
}
}
class nestingTest
{
public static void main(String args[])
{
nesting nest = new nesting(50,40);
[Link]();
}
}
*********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 = [Link]();
int volume1 = [Link]();
[Link]("Area = " + area1);
[Link]("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;
[Link]("a = " + a);
[Link]("b = " + b);
[Link]("a+b = " + (a+b));
[Link]("a-b = " + (a-b));
[Link]("a*b = " + (a*b));
[Link]("a/b = " + (a/b));
[Link]("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;
[Link]("a="+a); [Link]("b="+b);
[Link]("c="+c);
[Link]("a<b"+(a<b));
[Link]("a>b"+(a>b));
[Link]("a<=b"+(a<=b));
[Link]("a==b"+(a==b));
[Link]("a>=b"+(a>=b));
[Link]("a!=b"+(a!=b));
[Link]("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;
[Link]("m ="+m);
[Link]("n ="+n);
[Link]("++m ="+ ++m);
[Link]("n++ ="+ n++);
[Link]("m ="+m);
[Link]("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;
[Link]("m ="+m);
[Link]("n ="+n);
[Link]("--m ="+ --m);
[Link]("n-- ="+ n--);
[Link]("m ="+m);
[Link]("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){
[Link]("You are eligible to donateblood");
} else{
[Link]("You are not eligible to donate
blood");
} } else{
[Link]("Age must be greater than 18");
}}}
*********Output*********
/*15-Write a program to use break statement*/
class bbb{
public static void main(String args[])
{
[Link]("show importance of breakstatement:");
{
for (int i=1;i<=10;i++)
{
[Link]("="+i);if
(i==5)
{
[Link]("bye");
break;}}}
*******output*******
27 | P a g e
/*16-Write a program to use continue statement*/
class ccc{
public static void main(String args[])
{
[Link]("show importance of continue
statement:");
{
for (int m=1;m<=3;m++)
{
for(int n=1;n<=2;n++)
{
if (m==n)
continue;
[Link]("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 = [Link]();
int volume1 = [Link]();
[Link]("Area = " + area1);
[Link]("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;
[Link]("Even numbers brtween 1 and 15are:");
while(count<=15)
{[Link](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;
[Link]("Even numbers brtween 1 and 15are:");
while(count<=15)
{[Link](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;
[Link]("Multiplication Table \n");
row=1;
do
{
column=1;
do
{
y=row*column;
[Link](" "+y);
column=column+1;
}
33 | P a g e
while(column<=3);
[Link]("\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[])
{
[Link]("hello")}
}
*********Output**********
35 | P a g e
/*22-Write a program to use Applets*/
//Path C/java/bin/[Link]
import [Link].*;
import [Link].*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
[Link]("Hello java",10,100);
}}
//Path C/java/bin/[Link]
<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=[Link]
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);
[Link]("x="+x);
int y=a/(b+c);
[Link]("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)
{ [Link]("Division by zero"); }
y=a/(b+c);
[Link]("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)
{
[Link]("Division by zero");
}
catch(ArrayIndexOfBoundsException e)
{
[Link]("Array index error");
40 | P a g e
}
catch(ArrayStoreException e)
{
[Link]("wrong data type");
}
int y=a[1]/a[0];
[Link]("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)
{
[Link]("Division by zero");
}
catch(ArrayStoreException e)
{
42 | P a g e
[Link]("wrong data type");
}
finally
{
int y=a[1]/a[0];
[Link]("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=[Link];
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++)
{
[Link](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;
[Link]("MULTIPLICATION TABLE");
[Link](" ");
int i,j;
for(i=1;i<ROWS;i++)
{ for (j=1;j<COLUMNS;j++)
{ product [i] [j] = i*j; [Link]("
" +product[i][j]);
} [Link](" ");
} }}
********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;
[Link]("area of
rectangle="+[Link](10,20));
area=cir;
[Link]("area of
circle="+[Link](10,0));} }
**********Output**********
48 | P a g e
/*30-Write a program to create a package*/
package package1;
public class A
{
public void displayA()
{
[Link]("classA");} }
//Program to access a package
import package1.A;
class packageTest1
{
public static void main(String args[])
{
A objectA=new A();
[Link](); } }
**********Output**********
49 | P a g e