Wednesday, June 2, 2021

Define an interface using JAVA that contains a method to calculate the perimeter of an object. Define two classes-circle and Rectangle with suitable fields and methods. Implement the interface “perimeter” in these classes. Write the appropriate main() method to create object of each class and test all the methods.

PROGRAM :

package com.company;

import java.util.Scanner;

interface perimeter

{

    public void compPerimeter();

}

class Circle implements perimeter

{

    public void compPerimeter()

    {

        System.out.println("Enter the radius of the circle");

        Scanner s=new Scanner(System.in);

        float r=s.nextFloat();

        double Circleperimeter=2*3.14*r;

        System.out.println("Perimeter of the circle is "+Circleperimeter);

    }

}

 

class Rectangle implements perimeter

{

    public void compPerimeter()

    {

        System.out.println("Enter the Length and Bredth of the recatngle");

        Scanner s=new Scanner(System.in);

        float l=s.nextFloat();

        float w=s.nextFloat();

        double Rectperimeter=2*(l+w);

        System.out.println("Perimeter of the Rectangle is "+Rectperimeter);

    }

}

    class Interfacedemo

    {

        public static void main(String[] args)

        {

            Circle ob1=new Circle();

            ob1.compPerimeter();

            Rectangle ob2=new Rectangle();

            ob2.compPerimeter();

        }

    }

OUTPUT :



Labels: