Wednesday, 26 January 2022

A) Define an Interface Shape with abstract method area(). Write a java program to calculate an area of Circle and Sphere.(use final keyword)

 Slip 8 Q 1

Save code as             "InterfaceCircleSphere.java"


import java.util.Scanner;

interface Shape{
    void area();
}
class Circle implements Shape{
    final float PI=3.14f;
    float areacircle,radius;
    Scanner s=new Scanner(System.in);
    void accept(){
        System.out.print("Enter the Radius of circle : ");
        radius=s.nextFloat();
    }
    public void area(){
    areacircle=PI*radius*radius;
    }
    public void show()
    {
    System.out.println("Area of circle is : "+areacircle);
    }
}

class Sphere implements Shape{
    final float PI=3.14f;
    float areasphere,radius;
    Scanner s=new Scanner(System.in);
    void accept(){
        System.out.print("Enter the Radius of sphere : ");
        radius=s.nextFloat();
    }
    public void area(){
        areasphere=4*PI*radius*radius;
    }
    public void show(){
        System.out.println("Area of Sphere is : "+areasphere);
    }
}
class InterfaceCircleSphere
{
    public static void main(String args[]){
        Circle s1=new Circle();
        s1.accept();
        s1.area();
        s1.show();
        Sphere s2=new Sphere();
        s2.accept();
        s2.area();
        s2.show();
    }
}



OUTPUT:




No comments:

Post a Comment

Write a Java program to display given extension files from a specific directory on server machine.

 DOWNLOAD     SLIP14Q2 /** * STEPS TO RUN CODE * Step 01 compile the code * Step 02 run the code * Step 03 give a file directory locatio...