Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Plane to Circle/Cylinder

Updated 21 Oct 2026

A cylinder has a circle as its base, so it is natural to write a base class Plane for the circle (its radius and area) and a derived class Cylinder that adds a height and uses the inherited radius and area to find the volume and the surface areas. This is the standard form of the program: default constructors, an accept() and a display() in each class, and the derived class calling the base class methods with super.

Definition

Inheritance lets a derived class (class Cylinder extends Plane) use the data members and methods of the base class and add its own. Data members marked protected can be used directly in the derived class. A method written again in the derived class with the same name and parameters is redefined (overridden), and super.method() calls the base class version.

For a circle of radius $r$ and a cylinder of height $h$:

  • area of the circle $= \pi r^2$
  • volume $= \pi r^2 h$ (base area $\times$ height)
  • curved surface area $= 2 \pi r h$
  • total surface area $= 2 \pi r h + 2 \pi r^2$ (curved area plus two circular ends)

The program uses $\pi = 3.14159$ as a constant.

For example, $r = 3$ and $h = 5$: base area $= 28.27431$, volume $= 141.37155$, curved area $= 94.2477$, total area $= 94.2477 + 2 \times 28.27431 = 150.79632$.

Both radius and height must be greater than 0; otherwise the program prints INVALID INPUT.

Algorithm

  1. main (in the driver class Test) creates an object ob of the derived class Cylinder. The constructor Cylinder() sets height to 0, after Plane() has set radius to 0.
  2. main calls ob.accept(), the accept() of Cylinder:
    • it first calls super.accept(), the accept() of Plane, which reads radius and returns false if it is 0 or less;
    • if that returned false, Cylinder's accept() returns false at once;
    • otherwise it reads height and returns false if it is 0 or less, else true.
  3. If accept() returned false, main prints INVALID INPUT and stops.
  4. Otherwise main calls ob.display(), the display() of Cylinder. It calls super.display(), which prints the radius and the base area from area() (PI * radius * radius), then prints:
    • the height;
    • volume(), which returns area() * height, reusing the inherited method;
    • curvedArea(), which returns 2 * PI * radius * height;
    • totalArea(), which returns curvedArea() + 2 * area().
  5. Every result is printed to 2 decimal places with printf("%.2f").

Program

Plane holds the radius and the circle's area, Cylinder adds the height and the three cylinder results, and Test only holds main. The Scanner is a static member of Plane, so both classes read from the same one.

import java.util.Scanner;

/* Base class: a plane figure, the circle, described by its radius. */
class Plane
{
    protected double radius;                     // radius of the circle
    protected final double PI = 3.14159;         // value of pi used in all formulae
    static Scanner sc = new Scanner(System.in);  // one Scanner shared by Plane and Cylinder

    /* Default constructor: radius starts at 0. */
    Plane()
    {
        radius = 0.0;
    }

    /* Reads the radius. Returns true if it is greater than 0, otherwise false. */
    boolean accept()
    {
        System.out.print("Enter radius: ");
        radius = sc.nextDouble();
        if (radius <= 0)
        {
            return false;
        }
        return true;
    }

    /* Returns the area of the circle: PI * r * r. */
    double area()
    {
        return PI * radius * radius;
    }

    /* Prints the radius and the area of the circle. */
    void display()
    {
        System.out.printf("Radius             : %.2f%n", radius);
        System.out.printf("Area of the base   : %.2f%n", area());
    }
}

/* Derived class: a cylinder, a Plane (its base) with a height. */
class Cylinder extends Plane
{
    double height;               // height of the cylinder

    /* Default constructor: height starts at 0. */
    Cylinder()
    {
        height = 0.0;
    }

    /* Reads the radius through the base class and then the height.
       Returns true only if both are greater than 0. */
    boolean accept()
    {
        if (!super.accept())     // stop at once if the radius was invalid
        {
            return false;
        }
        System.out.print("Enter height: ");
        height = sc.nextDouble();
        if (height <= 0)
        {
            return false;
        }
        return true;
    }

    /* Returns the volume: base area * height (area() is inherited). */
    double volume()
    {
        return area() * height;
    }

    /* Returns the curved surface area: 2 * PI * r * h. */
    double curvedArea()
    {
        return 2 * PI * radius * height;
    }

    /* Returns the total surface area: curved area + two circular ends. */
    double totalArea()
    {
        return curvedArea() + 2 * area();
    }

    /* Prints the base details through the base class, then the cylinder's results. */
    void display()
    {
        super.display();
        System.out.printf("Height             : %.2f%n", height);
        System.out.printf("Volume             : %.2f%n", volume());
        System.out.printf("Curved surface area: %.2f%n", curvedArea());
        System.out.printf("Total surface area : %.2f%n", totalArea());
    }
}

/* Driver class: holds main. */
public class Test
{
    /* Creates one Cylinder object and runs accept and display. */
    public static void main(String args[])
    {
        Cylinder ob = new Cylinder();    // object of the derived class
        if (ob.accept())
        {
            ob.display();
        }
        else
        {
            System.out.println("INVALID INPUT");
        }
    }
}

Variable description table

VariableData TypePurpose
radiusdoubleProtected data member of Plane: radius of the circle and of the cylinder's base
PIdoubleProtected constant of Plane: value of pi (3.14159)
scScannerStatic data member of Plane: reads all the input
heightdoubleData member of Cylinder: height of the cylinder
obCylinderObject of the derived class created in main
argsString[]Command-line arguments of main (not used)

Sample input and output

Sample 1

Enter radius: 3
Enter height: 5
Radius             : 3.00
Area of the base   : 28.27
Height             : 5.00
Volume             : 141.37
Curved surface area: 94.25
Total surface area : 150.80

Sample 2

Enter radius: 0.1
Enter height: 0.2
Radius             : 0.10
Area of the base   : 0.03
Height             : 0.20
Volume             : 0.01
Curved surface area: 0.13
Total surface area : 0.19

Sample 3

Enter radius: 2.5
Enter height: 10
Radius             : 2.50
Area of the base   : 19.63
Height             : 10.00
Volume             : 196.35
Curved surface area: 157.08
Total surface area : 196.35

Sample 4

Enter radius: 4
Enter height: -2
INVALID INPUT

One value per line

Dry run

Take the first sample: radius $= 3$, height $= 5$. After accept(), radius is 3.0 and height is 5.0. display() works out:

StepStatementValue
1area(): 3.14159 * 3 * 328.27431
2volume(): area() * 5141.37155
3curvedArea(): 2 * 3.14159 * 3 * 594.2477
4totalArea(): 94.2477 + 2 * 28.27431150.79632

Printed to 2 decimal places these are 28.27, 141.37, 94.25 and 150.80, so the last line is Total surface area : 150.80, as in the first sample run.

Variations

  1. Cone "Write a base class for a circle and a derived class for a cone that finds its volume, curved surface area and total surface area."
  2. Sphere "Derive a class for a sphere from the circle class and find its volume and surface area."
  3. Different base "Write a class for a rectangle and a derived class for a cuboid that finds its volume and total surface area."