Tuesday 27 September 2011

INTERFACES


Exno:                                                   INTERFACES
Aim:
            To write a java program to implement the concept of  interfaces.
Algorithm:
Step1: Start.
Step2: Create a interface calculate and declare he method area().
Step3: Create the class rectangle and triangle which implements the interface calculate.
Step4: within these classes define the method area().
Step5: Create the objects for the rectangle and triangle and find the area.
Step6: Print the area of rectangle and triangle.
Step7: Stop.
Program:
import java.io.*;
interface calculat
{
public double area(double len,double hgt);
}
class rectangle implements calculat
{
public double area(double len,double hgt)
{
return len*hgt;
}
}
class triangle implements calculat
{
public double area(double len,double hgt)
{
return 0.5*len*hgt;
}
}
public class calculate
{
public static void main(String args[])
{
rectangle r=new rectangle();
triangle t=new triangle();
System.out.println(r.area(40,50));
System.out.println(t.area(20,30));
}
}

Output:
Z:\anitha>javac calculate.java
Z:\anitha>java calculate
2000.0
300.0

0 comments:

Post a Comment