Hi,
i was trying one program on interface, i don't know where i am getting wrong, but when i complied the program at some other system some time back, it complied successfully and gave me the output, but when i tried it today i was getting errors.
Well one more thing, actually i was doing this to practise interface, but i think the logic or code which i am writing is wrong, so please see to it and suggest me,
Here's the code:
/* Program on Interface:
1. Create an interface Area with two methods:
a)Area(int side1, int side2).
b)Perimeter(int side1, int side2)
2. Create class Square that implements an interface Area, and defines above two methods to calculate area and perimeter of square.
3. Create class Rectangle that implements an interface and defines above two methods to calculate area and perimeter of rectangle.
4.Create class DemoInterface that uses class Square and Rectangle to calculate the area and perimeter of Square and Rectangle.*/
/********* This inteface i saved in same folder by name Area1.java *******8*/
interface Area1{
AreaCalculate(int length, int breadth);
Perimeter(int side1, int side2);
}
/******************************************************************/
class Square implements Area1
{
//Area1 a1=new Area1();
//Area1 a2=new Area1();
public int AreaCalculate(int l, int b)
{
int AreaSquare=0;
AreaSquare=l*b;
return AreaSquare;
}
public int Perimeter(int s1, int s2)
{
int perimeterSquare=0;
perimeterSquare=2*(s1+s2);
return perimeterSquare;
}
}
class Rectangle1 implements Area1
{
//Area1 a3=new Area1();
//Area1 a4=new Area1();
public int AreaCalculate(int l, int b)
{
int AreaRect=0;
AreaRect=l*b;
return AreaRect;
}
a4.Perimeter(int s1, int s2)
{
int perimeterRect=0;
perimeterRect=2*(s1+s2);
return perimeterRect;
}
}
public class DemoInterface
{
public static void main(
String[] args)
{
int AreaOfSquare=0, AreaOfRect=0, PerimeterOfSquare=0, PerimeterOfRectangle=0;
Square sq1=new Square();
AreaOfSquare=sq1.AreaCalculate(10,10);
System.out.println("Area of Square is ="+AreaOfSquare);
Square sq2=new Square();
PerimeterOfSquare=sq2.Perimeter(10,10);
System.out.println("Perimeter of Square is ="+PerimeterOfSquare);
Rectangle1 rect1=new Rectangle1();
Rectangle1 rect2= new Rectangle1();
AreaOfRect=rect1.AreaCalculate(4,4);
System.out.println("Area of Rectangle is ="+AreaOfRect);
PerimeterOfRectangle=rect2.Perimeter(4,4);
System.out.println("Perimeter of Rectangle is ="+PerimeterOfRectangle);
}
}