Wednesday, June 2, 2021

Create a Java Class “Shape” with constructor to initialize the one parameter “dimension”. Now create three sub classes of Shape with following methods (i) “Circle” with methods to calculate the area and circumference of the circle with dimension as radius. (ii) “Square” with methods to calculate the area and length of diagonal of the square with dimension as length of one side. (asuming length of each side of the square is same). (iii) “Sphere” with methods to calculate the volume and surface area of the sphere with dimension as radius of the sphere. Write appropriate main method to create object of each class and test every method.

PROGRAM :

package com.company;

import java.util.Scanner;

class shape

{

    double dimention;

    shape(double dimention)

    {

        this.dimention=dimention;

    }

}

class Circle extends shape

{

    double area, circum;

    Circle(double d)

    {

        super(d);

    }

    void area()

    {

        area=3.14*dimention*dimention;

        System.out.println("Area ofa Circle="+area);

    }

    void circumference()

    {

        circum=2*3.14*dimention;

        System.out.println("Circumference of a Circle="+circum);

    }

}

class Square extends shape

{

    double area, LenofDia;

    Square(double d)

    {

        super(d);

    }

    void area()

    {

        System.out.println("Area ofa Square="+(dimention*dimention));

    }

    void diagonal()

    {

        LenofDia=Math.sqrt(2)*dimention;

        System.out.println("Length of Diagonal of the Square="+LenofDia);

    }

}

class Sphere extends shape

{

    double volume , surfacearea;

    Sphere(double d)

    {

        super(d);

    }

    void volume()

    {

        System.out.println("Volum of a Sphere="+((4/3)*3.14*dimention*dimention*dimention));

    }

    void surfacearea()

    {

        surfacearea=4*3.14*dimention*dimention;

        System.out.println("Surface area of a Sphere="+surfacearea);

    }

}

public class test

{

    public static void main(String args[])

    {

 

        Scanner s=new Scanner(System.in);

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

        int r=s.nextInt();

        Circle ob1=new Circle(r);

        ob1.area();

        ob1.circumference();

        System.out.println("Enter the Length of the square");

        int l=s.nextInt();

        Square ob2=new Square(l);

        ob2.area();

        ob2.diagonal();

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

        int b=s.nextInt();

        Sphere ob3=new Sphere(b);

        ob3.volume();

        ob3.surfacearea();

    }

}

OUTPUT :



Labels:

1 Comments:

At December 25, 2022 at 6:41 AM , Blogger TECHNO NEPAL said...

thank you bro

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home