0% found this document useful (0 votes)
51 views

wap To Show The Concept of Function Overridding Class Figure

The document discusses Java programs that demonstrate various object-oriented programming concepts like function overriding, constructors, inheritance, matrices, strings, and applets. It contains the code and output for programs that calculate areas of shapes by overriding methods, check for prime numbers, find vowels in strings, reverse strings, and multiply matrices by iterating through nested for loops.

Uploaded by

Ankush Verma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

wap To Show The Concept of Function Overridding Class Figure

The document discusses Java programs that demonstrate various object-oriented programming concepts like function overriding, constructors, inheritance, matrices, strings, and applets. It contains the code and output for programs that calculate areas of shapes by overriding methods, check for prime numbers, find vowels in strings, reverse strings, and multiply matrices by iterating through nested for loops.

Uploaded by

Ankush Verma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

//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()
{
System.out.println("This is basic figure class");
return(0);
}
}

class rec extends figure


{
rec(int d1,int d2)
{
super(d1,d2);
}
int area()
{
System.out.println("Area for rectangle is ");
return(dim1*dim2);
}
}

class tri extends figure


{
tri(int d1,int d2)
{
super(d1,d2);
}
int area()
{
System.out.println("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;
System.out.println(" Area is : "+ ref.area());
ref=t;
System.out.println(" Area is : "+ ref.area());
ref=f;
System.out.println(" Area is : "+ ref.area());
}
}

*******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()
{
System.out.println("Radius is : "+radius);
System.out.println("Area of circle is : "+
(pi*radius*radius));
}
void par()
{
System.out.println("parameter of circle is :
"+(2*pi*radius*radius));
}
}

class formula
{
public static void main(String args[])
{
per p = new per(10.2F);
p.area();
p.par();
}
}

******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;
System.out.println("Area is "+arr);
}
}
class over
{
public static void main(String args[])
{
cons a=new cons();
a.area();
cons b=new cons(10,20);
b.area();
cons c=new cons(10);
c.area();
}
}

******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)
System.out.println("No is prime ");
else
System.out.println("No is not prime ");
}
}
class prime1
{
public static void main(String args[])
{
prime obj=new prime(11);
obj.cal();
}
}

******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;
System.out.println("String is "+str);
StringBuilder obj=new StringBuilder(str);
obj.reverse();
str1=obj.toString();
if(str.compareTo(str1)==0)
System.out.println("string is palindrom");
else
System.out.println("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";
System.out.println("String is "+str);
StringBuilder obj=new StringBuilder(str);
obj.reverse();
System.out.println("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=str.length();
char arr[]=new char[40];
for(i=0;i<j;i++)
{
arr[i]=str.charAt(i);
}
for(i=0;i<j;i++)
{
if(arr[i]=='a'||arr[i]=='e'||arr[i]=='i'||arr[i]=='o'||
arr[i]=='u')
{
n++;
}
}
System.out.println("String is "+str);
System.out.println("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)
{
System.out.println("This is leap year");
}
else
{
System.out.println("This is nt a leap year");
}
}
}

class year1
{
public static void main(String args[])
{
year yrr=new year(1987);
yrr.cal();
}
}

******OUTPUT******

This is nt a leap year ..


//wap to display an applet in java

import java.awt.*;
import java.applet.*;
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)
{
g.drawString(str,10,100);
}
}

Corrosponding code of html

<html>
<head>
<title>
java aplet</title>
</head>
<body>
<applet code=hello.class
width="400"
height="200" >
<param name="string" value="ss">
</applet>
</body>
</html>

******OUTPUT******
javac hello.java
java hello
“main lang”
Appletviewer applet.html
//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;

System.out.println("Ist matrix is ");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.print("\n");
}

System.out.println("2nd matrix is ");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.print("\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];
}
}
}

System.out.println("After mul matrix is ");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.print("\n");
}
}
}

******OUTPUT******

Ist matrix is
2 3
4 3

2nd matrix is
4 6
2 7

3rd matrix is
14 22
33 45

You might also like