
Example: Java program for
demonstrating interface for calculating area’s of circle and square based on
choice


//Program name MainClass.java
//Java program for demonstrating interface for
calculating area’s of circle and square based on choice
import java.util.*;
public class MainClass implements Area{
Scanner scan=new Scanner(System.in);
public static void main(String[] args)
{
Scanner scan1=new Scanner(System.in);
MainClass M=new MainClass();
int choice;
System.out.println("Enter 1 to find
area of Circle");
System.out.println("Enter 2 to find
area of Square");
System.out.print("Your choice :
");
choice =scan1.nextInt();
if(choice==1)
{
M.AreaofCircle();
}
else if(choice==2)
{
M.AreaofSquare();
}
else
{
System.out.println("Wrong
Choice..");
}
}
public void AreaofCircle()
{
int r;
double pi=3.14;
double area;
System.out.print("Enter radius of
circle to find the area :");
r=scan.nextInt();
area=pi*r*r;
System.out.println("Area of Circle
is : "+area);
}
public void AreaofSquare()
{
int side;
double area;
System.out.print("Enter side of
square to find the area :");
side=scan.nextInt();
area=side * side;
System.out.println("Area of Square
is : "+area);
}
}

Output

Enter 1 to find area of Circle
Enter 2 to find area of Square
Your choice : 1
Enter radius of circle to find the area :10
Area of Circle is : 314.0
Enter 1 to find area of Circle
Enter 2 to find area of Square
Your choice : 2
Enter side of square to find the area :10
Area of Square is : 100.0
Enter 1 to find area of Circle
Enter 2 to find area of Square
Your choice : 3
Wrong Choice..
