Skip to content

Commit f1ef64c

Browse files
authored
algorithm class: cone (TheAlgorithms#1253)
1 parent c5101e3 commit f1ef64c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Geometry/Cone.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* This class represents a circular cone and can calculate its volume and surface area
3+
* https://en.wikipedia.org/wiki/Cone
4+
* @constructor
5+
* @param {number} baseRadius - The radius of the base of the cone.
6+
* @param {number} height - The height of the cone
7+
*/
8+
export default class Cone {
9+
constructor (baseRadius, height) {
10+
this.baseRadius = baseRadius
11+
this.height = height
12+
}
13+
14+
baseArea = () => {
15+
return Math.pow(this.baseRadius, 2) * Math.PI
16+
}
17+
18+
volume = () => {
19+
return this.baseArea() * this.height * 1 / 3
20+
}
21+
22+
surfaceArea = () => {
23+
return this.baseArea() + Math.PI * this.baseRadius * Math.sqrt(Math.pow(this.baseRadius, 2) + Math.pow(this.height, 2))
24+
}
25+
}

Geometry/Test/Cone.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Cone from '../Cone'
2+
3+
const cone = new Cone(3, 5)
4+
5+
test('The Volume of a cone with base radius equal to 3 and height equal to 5', () => {
6+
expect(parseFloat(cone.volume().toFixed(2))).toEqual(47.12)
7+
})
8+
9+
test('The Surface Area of a cone with base radius equal to 3 and height equal to 5', () => {
10+
expect(parseFloat(cone.surfaceArea().toFixed(2))).toEqual(83.23)
11+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy