Tuesday, October 7, 2008

DSA INTERFACE

import java.io.*;

interface Area //interface defined
{
double pi=3.14F;
double compute(double x, double y);
}

class Rectangle implements Area
{
public double compute (double x, double y)
{
return(x*y);
}


}

class Triangle implements Area
{
public double compute(double x, double y)
{
return(0.5*x*y);
}
}
class InterfaceTest1
{
public static void main(String args[])
{
int f,e,k=0;
try {
DataInputStream x=new DataInputStream(System.in);
while(k<3)
{
System.out.println("Please enter ur choice:");
System.out.println("1.FIND AREA OF Triangle :");
System.out.println("2.FIND AREA OF RECTANGLE: ");
System.out.println("3.EXIT");
k=Integer.parseInt(x.readLine());
switch(k)
{
case 1:
System.out.println("ENTER THE BASE : ");
e=Integer.parseInt(x.readLine());
System.out.println("ENTER THE HEIGHT : ");
f=Integer.parseInt(x.readLine());
Triangle tri=new Triangle();
System.out.println("Area of Triangle : "+tri.compute(e,f));
break;
case 2:
System.out.println("ENTER THE LENGTH : ");
e=Integer.parseInt(x.readLine());
System.out.println("ENTER THE BREADTH : ");
f=Integer.parseInt(x.readLine());
Rectangle rect=new Rectangle();
System.out.println("Area of rectangle : "+rect.compute(e,f));
break;

case 3:
System.out.println("THANK U: ");
break;

}
}
}
catch(Exception w) {
System.out.println("EXCEPTION!!! ");
}

}
}
/*
Hi...this is first program based on interface
Area of rectangle : 50.0
*/

No comments:

Post a Comment

JTSEARCH