Post

Created by @nathanedwards
 at October 31st 2023, 9:02:55 pm.

AP Computer Science Exam Question

public class Triangle {
    private double sideA;
    private double sideB;
    private double sideC;
    
    /**
     * Constructs a Triangle object with three sides.
     * The sides must form a valid triangle, otherwise an IllegalArgumentException is thrown.
     * 
     * @param a the length of side A
     * @param b the length of side B
     * @param c the length of side C
     */
    public Triangle(double a, double b, double c) {
        if (!isValidTriangle(a, b, c))
            throw new IllegalArgumentException("Invalid triangle sides");
            
        sideA = a;
        sideB = b;
        sideC = c;
    }
    
    /**
     * Checks if given sides form a valid triangle.
     * A valid triangle should satisfy the triangle inequality theorem.
     * 
     * @param a length of side A
     * @param b length of side B
     * @param c length of side C
     * @return true if the sides form a valid triangle, false otherwise
     */
    private boolean isValidTriangle(double a, double b, double c) {
        return (a + b > c) && (b + c > a) && (a + c > b);
    }
    
    /**
     * Calculates the perimeter of this triangle.
     * 
     * @return the perimeter of the triangle
     */
    public double getPerimeter() {
        return sideA + sideB + sideC;
    }
    
    /**
     * Calculates the area of this triangle using Heron's formula.
     * 
     * @return the area of the triangle
     */
    public double getArea() {
        double s = getPerimeter() / 2;
        return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
    }
}

Explain the purpose of the Triangle class and provide step-by-step explanations for each method:

  • Triangle(double a, double b, double c)
  • private boolean isValidTriangle(double a, double b, double c)
  • double getPerimeter()
  • double getArea()

Answer

Purpose of the Triangle class:

The Triangle class represents a triangle object with three sides. It provides methods to check if the given sides form a valid triangle, calculate the perimeter, and calculate the area of the triangle.

Triangle(double a, double b, double c) constructor:

This constructor creates a Triangle object with three sides: a, b, and c. It first calls the isValidTriangle(a, b, c) method to check if the sides form a valid triangle. If the sides do not satisfy the triangle inequality theorem, an IllegalArgumentException is thrown. Otherwise, the sides are assigned to instance variables sideA, sideB, and sideC.

private boolean isValidTriangle(double a, double b, double c) method:

This method checks if the given sides a, b, and c form a valid triangle. It applies the triangle inequality theorem, which states that the sum of the lengths of any two sides of a triangle must be greater than the length of the remaining side. The method returns true if the given sides satisfy this condition, otherwise false.

double getPerimeter() method:

This method calculates and returns the perimeter of the triangle. It adds the lengths of the three sides (sideA, sideB, and sideC) and returns the result.

double getArea() method:

This method calculates and returns the area of the triangle using Heron's formula. It first calculates the semi-perimeter (s) by dividing the perimeter by 2. Then it uses the formula Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC)) to calculate the area. The method returns the calculated area.