Saturday 22 October 2016

A Program to find area of rectangle and square

//*********a program to display area of square and rectangle using interface******************//
import java.util.*;
interface Area
{
public  void area(int side);
public void display();
}
class Shapes implements Area
{ int area1;int i;
int side;
Shapes(int c)
{
i=c;
}
public  void area(int side)
{ this.side=side;
if(i==1)//for square
{
area1=side*side;
}
else if(i==2) //for rectangle
{
System.out.println("enter width of rectangle");
Scanner sc=new Scanner(System.in);
int b=sc.nextInt();
area1=side*b;
}
else
return ;

}

public void display()
{
System.out.println("area="+area1);
}
}
class Demo
{
public static void main(String args[])
{
do{ Scanner sc=new Scanner(System.in);
System.out.println("enter 1:square\n2:rectangle\n");
int n=sc.nextInt();
Shapes s=new Shapes(n);
System.out.println("enter length of side");
s.area(sc.nextInt());
s.display();
}while(true);
}
}
output:





No comments:

Post a Comment