Post

Created by @nathanedwards
 at November 1st 2023, 10:33:14 am.

AP Computer Science Exam Question - Throwing Exceptions

Problem:

Write a Java program to implement a class called Circle that represents a circle with a given radius. The Circle class should have the following functionalities:

  • A constructor that takes a double value representing the radius of the circle.
  • A method called calculateArea() that calculates and returns the area of the circle.
  • A method called calculatePerimeter() that calculates and returns the perimeter of the circle.

The Circle class should throw appropriate exceptions if the radius is negative or zero.

Write the complete implementation of the Circle class, adhering to the given specifications.

Class Signature: public class Circle

Methods:

  • public Circle(double radius): Constructs a Circle object with the given radius.
  • public double calculateArea() throws IllegalArgumentException: Calculates and returns the area of the circle.
  • public double calculatePerimeter() throws IllegalArgumentException: Calculates and returns the perimeter of the circle.

You can use the following formulas for calculations:

  • Area of a circle = π * radius^2
  • Perimeter of a circle = 2 * π * radius

Note: Use Math.PI to represent the value of π.

Example:

Circle circle1 = new Circle(5.0);
System.out.println(circle1.calculateArea());       // Output: 78.53981633974483
System.out.println(circle1.calculatePerimeter());  // Output: 31.41592653589793

Circle circle2 = new Circle(-2.0);                  // Throws IllegalArgumentException
Circle circle3 = new Circle(0.0);                   // Throws IllegalArgumentException

Write the complete code for the Circle class including the constructor and the two methods mentioned above. Handle the exceptions appropriately and provide the requested functionalities.

Answer:

public class Circle {
    private double radius;

    public Circle(double radius) {
        if (radius <= 0) {
            throw new IllegalArgumentException("Invalid radius. Radius cannot be negative or zero.");
        }
        this.radius = radius;
    }

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

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

Explanation:

The Circle class has a private instance variable radius to store the radius of the circle. The constructor of the Circle class takes a double value representing the radius. Inside the constructor, we check if the radius is less than or equal to zero. If it is, we throw an IllegalArgumentException with an appropriate error message.

The calculateArea() method calculates the area of the circle using the formula Math.PI * radius * radius. It returns the calculated area.

The calculatePerimeter() method calculates the perimeter of the circle using the formula 2 * Math.PI * radius. It returns the calculated perimeter.

In the example given above, we create a Circle object circle1 with a radius of 5.0. We then call the calculateArea() and calculatePerimeter() methods on circle1 and print the respective outputs.

Next, we try to create Circle objects circle2 and circle3 with negative and zero radii respectively. As the constructor checks for invalid radii, both of these calls will throw IllegalArgumentException with appropriate error messages.