Post

Created by @nathanedwards
 at November 1st 2023, 11:28:47 am.

AP Computer Science Exam Question - Advanced Difficulty

Question:

Consider the following class:

public class Circle {
    private double radius;

    public Circle(double r) {
        radius = r;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double r) {
        radius = r;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

    public double getCircumference() {
        return 2 * Math.PI * radius;
    }
}

You are given the above Circle class, which represents a circle with a given radius. You need to implement a new class called Cylinder that represents a cylinder. The Cylinder class should inherit from the Circle class and include the following:

  • A private instance variable height of type double to represent the height of the cylinder.
  • A constructor that takes in two parameters: r (a double representing the radius) and h (a double representing the height) to initialize the Circle and Cylinder instance variables.
  • Getters and setters for the height instance variable.
  • A method called getVolume() that returns the volume of the cylinder. The volume of a cylinder is calculated as the product of the base area (π * radius^2) and the height.
  • A method called getSurfaceArea() that returns the surface area of the cylinder. The surface area of a cylinder is calculated as the sum of the areas of the two circular bases (2 * base area) and the lateral surface area (2 * π * radius * height).

Your task is to complete the implementation of the Cylinder class by adding the required instance variables and methods according to the given description.

Solution:

public class Cylinder extends Circle {
    private double height;

    public Cylinder(double r, double h) {
        super(r);
        height = h;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double h) {
        height = h;
    }

    public double getVolume() {
        return getArea() * height;
    }

    public double getSurfaceArea() {
        return (2 * getArea()) + (2 * Math.PI * getRadius() * height);
    }
}

Explanation:

The Cylinder class extends the Circle class, inheriting its instance variables and methods. It also adds a private instance variable height to represent the height of the cylinder.

The constructor of the Cylinder class takes in two parameters: r and h, which are used to initialize the Circle instance variable (using the super keyword to call the superclass constructor) and the height instance variable of the Cylinder class.

Since the height instance variable is private, getter and setter methods are provided to access and modify its value.

The getVolume() method calculates and returns the volume of the cylinder by multiplying the base area, obtained by calling the inherited getArea() method (which calculates the area of the circular base), with the height instance variable.

The getSurfaceArea() method calculates and returns the surface area of the cylinder by summing the areas of the two circular bases (2 times the base area) and the lateral surface area (2 times π times the radius times the height).

By properly implementing the above methods and inheriting from the Circle class, the Cylinder class provides the required functionality to represent a cylinder and perform calculations related to its volume and surface area.