//wap to show the concept of function overridding
class figure
{
int dim1;
int dim2;
figure(int d1,int d2)
{
dim1=d1;
dim2=d2;
}
int area()
{
[Link]("This is basic figure class");
return(0);
}
}
class rec extends figure
{
rec(int d1,int d2)
{
super(d1,d2);
}
int area()
{
[Link]("Area for rectangle is ");
return(dim1*dim2);
}
}
class tri extends figure
{
tri(int d1,int d2)
{
super(d1,d2);
}
int area()
{
[Link]("Area for triangle is ");
return((dim1*dim2)/2);
}
}
class basic
{
public static void main(String args[])
{
figure f=new figure(10,10);
rec r=new rec(5,5);
tri t=new tri(6,6);
figure ref;
ref=r;
[Link](" Area is : "+ [Link]());
ref=t;
[Link](" Area is : "+ [Link]());
ref=f;
[Link](" Area is : "+ [Link]());
}
}
*******OUTPUT******
Area for rectangle is
Area is : 25
Area for triangle is
Area is : 18
This is basic figure class
Area is : 0
//wap to find the parameter and area of circle
class per
{
float radius;
float pi=3.14F;
per(float r)
{
radius=r;
}
void area()
{
[Link]("Radius is : "+radius);
[Link]("Area of circle is : "+
(pi*radius*radius));
}
void par()
{
[Link]("parameter of circle is :
"+(2*pi*radius*radius));
}
}
class formula
{
public static void main(String args[])
{
per p = new per(10.2F);
[Link]();
[Link]();
}
}
******OUTPUT******
Area of circle is : 326.68558
Parameter of circle is : 653.37115
//wap to show the concept of constructor
overloading
class cons
{
int width;
int height;
cons()
{
width=10;
height=10;
}
cons(int h , int w)
{
height=h;
width=w;
}
cons(int h)
{
height=width=h;
}
void area()
{
int arr;
arr=height*width;
[Link]("Area is "+arr);
}
}
class over
{
public static void main(String args[])
{
cons a=new cons();
[Link]();
cons b=new cons(10,20);
[Link]();
cons c=new cons(10);
[Link]();
}
}
******OUTPUT******
Area is : 100
Area is : 200
Area is : 100
//wap to check wheather the num is prime or not
class prime
{
int n;
prime(int x)
{
n=x;
}
void cal()
{
int i=2;
int c=0;
while(i<=n-1)
{
if(n%i==0)
{
c++;
}
i++;
}
if(c==0)
[Link]("No is prime ");
else
[Link]("No is not prime ");
}
}
class prime1
{
public static void main(String args[])
{
prime obj=new prime(11);
[Link]();
}
}
******OUTPUT******
Given no is not prime
//wap to check wheather the string is palindrom or
not
class strpal
{
public static void main(String args[])
{
String str="madam";
String str1;
[Link]("String is "+str);
StringBuilder obj=new StringBuilder(str);
[Link]();
str1=[Link]();
if([Link](str1)==0)
[Link]("string is palindrom");
else
[Link]("string not palindrom");
}
}
******OUTPUT******
String is madam
String is palindrome
//wap to find the reverse of a string
class strrev
{
public static void main(String args[])
{
String str="shelja";
[Link]("String is "+str);
StringBuilder obj=new StringBuilder(str);
[Link]();
[Link]("reverse of string is "+obj);
}
}
******OUTPUT******
String is shelja
Reverse of string is ajlehs
//wap to find vowels in a given string
class strvol
{
public static void main(String args[])
{
String str="this is the string to find no of vowles";
int n=0,i,j;
j=[Link]();
char arr[]=new char[40];
for(i=0;i<j;i++)
{
arr[i]=[Link](i);
}
for(i=0;i<j;i++)
{
if(arr[i]=='a'||arr[i]=='e'||arr[i]=='i'||arr[i]=='o'||
arr[i]=='u')
{
n++;
}
}
[Link]("String is "+str);
[Link]("No of vowles in string : "+n);
}
}
******OUTPUT******
String is : this is the string to find no of vowles
Vowels in the string is : 10
//wap to check wheather the given year is leap or
not
class year
{
int yr;
year(int y)
{
yr=y;
}
void cal()
{
if(yr%100==0||yr%4==0)
{
[Link]("This is leap year");
}
else
{
[Link]("This is nt a leap year");
}
}
}
class year1
{
public static void main(String args[])
{
year yrr=new year(1987);
[Link]();
}
}
******OUTPUT******
This is nt a leap year ..
//wap to display an applet in java
import [Link].*;
import [Link].*;
public class hello extends Applet
{
String str;
public void init()
{
str=getParameter("string");
if(str==null)
str="java";
str="hello"+str;
}
public void paint(Graphics g)
{
[Link](str,10,100);
}
}
Corrosponding code of html
<html>
<head>
<title>
java aplet</title>
</head>
<body>
<applet code=[Link]
width="400"
height="200" >
<param name="string" value="ss">
</applet>
</body>
</html>
******OUTPUT******
javac [Link]
java hello
“main lang”
Appletviewer [Link]
//wap to show multiplaction of matrix
class mul
{
public static void main(String args[])
{
int a[][]=new int[2][2];
int b[][]=new int[2][2];
int c[][]=new int[2][2];
int i,j,k;
a[0][0]=2;
a[0][1]=3;
a[1][0]=4;
a[1][1]=3;
b[0][0]=4;
b[0][1]=6;
b[1][0]=2;
b[1][1]=7;
[Link]("Ist matrix is ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
[Link](a[i][j]+"\t");
}
[Link]("\n");
}
[Link]("2nd matrix is ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
[Link](b[i][j]+"\t");
}
[Link]("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
[Link]("After mul matrix is ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
[Link](c[i][j]+"\t");
}
[Link]("\n");
}
}
}
******OUTPUT******
Ist matrix is
2 3
4 3
2nd matrix is
4 6
2 7
3rd matrix is
14 22
33 45