Calculate area & volume using method overriding

Q. Create an abstract class Shape. Derive three classes sphere, cone and cylinder from it. Calculate area and volume of all. (Use Method overriding)

Answer:

The Shape class is the abstract class which has two abstract method calcArea() and another is calcVolume().

Shape.java

abstract class Shape
{
     double r, h;
     Shape(double r, double h)
     {
          this.r = r;
          this.h = h;
     }
     abstract double calcArea();
     abstract double calcVolume();
}


Sphere.java

class Sphere extends Shape
{
     Sphere(double r)
     {
          super(r, 0);
     }
     double calcArea()
     {
          return 4*3.14*r*r;
     }
     double calcVolume()
     {
          return 4*3.14*Math.pow(r,3)/3;
     }
}


Cone.java

class Cone extends Shape
{
     Cone(double r, double h)
     {
          super(r, h);
     }
     double calcArea()
     {
          return 3.14*r*(r+Math.sqrt(r*r+h*h));
     }
     double calcVolume()
     {
          return 3.14*r*r*h/3;
     }
}


Cylinder.java

class Cylinder extends Shape
{
     Cylinder(double r, double h)
     {
          super(r, h);
     }
     double calcArea()
     {
          return 2*3.14*r*(h+r);
     }
     double calcVolume()
     {
          return 3.14*r*r*h;
     }
}


Calculate_Area_Volume .java

import java.io.*;
class Calculate_Area_Volume
{
     public static void main(String[] args) throws Exception
     {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          Shape shape = null;
          double r = 0;
          double h = 0;
          int ch;
          do
          {
               System.out.print("\n1.Sphere"+"\n2.Cone"+"\n3.Cylinder"+ "\n4.Exit "+"\nEnter your choice : ");
               ch = Integer.parseInt(br.readLine());
               switch (ch)
               {
                    case 1:
                         System.out.print("Enter radius of sphere:");
                         r = Double.parseDouble(br.readLine());
                         shape = new Sphere(r);
                         break;
                    case 2:
                         System.out.print("Enter radius of Cone:");
                         r = Double.parseDouble(br.readLine());
                         System.out.print("Enter height of cone:");
                         h = Double.parseDouble(br.readLine());
                         shape = new Cone(r,h);
                         break;
                    case 3:
                         System.out.print("Enter radius of cylinder:");
                         r = Double.parseDouble(br.readLine());
                         System.out.print("Enter height of cylinder:");
                         h = Double.parseDouble(br.readLine());
                         shape = new Cylinder(r,h);
                         break;
                    case 4:
                         System.exit(0);
               }
               System.out.println("Area = "+shape.calcArea()+"\nVolume = "+shape.calcVolume());
          }
          while (ch!=4);
     }
}


Output:

area volume