-
Notifications
You must be signed in to change notification settings - Fork 20.7k
Added surfaceAreaPyramid_v4 with improved comments and validation #6896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -192,4 +192,31 @@ public static double surfaceAreaCone(final double radius, final double height) { | |
| } | ||
| return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5)); | ||
| } | ||
| /** | ||
| * Calculates the surface area of a pyramid with a square base. | ||
| * This includes both the base and the four triangular faces. | ||
| * | ||
| * @param sideLength the length of one side of the square base | ||
| * @param slantHeight the slant height of the pyramid | ||
| * @return the total surface area of the pyramid | ||
| * @throws IllegalArgumentException if any parameter is non-positive | ||
| */ | ||
| public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) { | ||
| // Validation checks to ensure inputs are physically valid | ||
| if (sideLength <= 0) { | ||
| throw new IllegalArgumentException("Side length must be a positive number"); | ||
| } | ||
| if (slantHeight <= 0) { | ||
| throw new IllegalArgumentException("Slant height must be a positive number"); | ||
| } | ||
|
|
||
| // Base area (square) = side² | ||
| double baseArea = sideLength * sideLength; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simple and efficient calculation of the square base area. |
||
|
|
||
| // Lateral area = 2 × side × slant height | ||
| double lateralSurfaceArea = 2 * sideLength * slantHeight; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accurate computation for the combined area of four triangular sides. |
||
|
|
||
| // Combine base and lateral areas for total surface area | ||
| return baseArea + lateralSurfaceArea; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.