Skip to content

feat: add geometry kite #1697

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions Geometry/Kite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* This class represents a Kite and provides methods to calculate its area and perimeter.
* @see {@link https://en.wikipedia.org/wiki/Kite_(geometry)}
* @class
*/
export default class Kite {
/** @private */
#sideA

/** @private */
#sideB

/** @private */
#diagonal1

/** @private */
#diagonal2

/**
* Creates a kite instance.
* @constructor
* @param {number} sideA - The length of one pair of equal sides.
* @param {number} sideB - The length of the other pair of equal sides.
* @param {number} diagonal1 - The length of the first diagonal.
* @param {number} diagonal2 - The length of the second diagonal.
* @throws {Error} Will throw an error if any dimension is invalid.
*/
constructor(sideA, sideB, diagonal1, diagonal2) {
this.#validateDimension(sideA, 'sideA')
this.#validateDimension(sideB, 'sideB')
this.#validateDimension(diagonal1, 'diagonal1')
this.#validateDimension(diagonal2, 'diagonal2')

this.#sideA = sideA
this.#sideB = sideB
this.#diagonal1 = diagonal1
this.#diagonal2 = diagonal2
}

/**
* Validates that a dimension is a positive number.
* @private
* @param {number} value - The value to validate.
* @param {string} name - The name of the dimension (for error reporting).
* @throws {Error} Will throw an error if the value is not a positive number.
*/
#validateDimension(value, name) {
if (typeof value !== 'number' || isNaN(value) || value <= 0) {
throw new Error(`${name} must be a positive number.`)
}
}

/**
* Calculates the area of the kite.
* @public
* @returns {number} The area of the kite.
*/
area() {
return (this.#diagonal1 * this.#diagonal2) / 2
}

/**
* Calculates the perimeter of the kite.
* @public
* @returns {number} The perimeter of the kite.
*/
perimeter() {
return 2 * (this.#sideA + this.#sideB)
}

/**
* Returns a string representation of the kite.
* @public
* @returns {string} A string describing the kite's dimensions and area/perimeter.
*/
toString() {
return (
`Kite: sideA = ${this.#sideA}, sideB = ${this.#sideB}, ` +
`diagonal1 = ${this.#diagonal1}, diagonal2 = ${this.#diagonal2}, ` +
`area = ${this.area()}, perimeter = ${this.perimeter()}`
)
}

/**
* Gets the length of side A of the kite.
* @public
* @returns {number} The length of side A.
*/
get sideA() {
return this.#sideA
}

/**
* Gets the length of side B of the kite.
* @public
* @returns {number} The length of side B.
*/
get sideB() {
return this.#sideB
}

/**
* Gets the length of diagonal 1 of the kite.
* @public
* @returns {number} The length of diagonal 1.
*/
get diagonal1() {
return this.#diagonal1
}

/**
* Gets the length of diagonal 2 of the kite.
* @public
* @returns {number} The length of diagonal 2.
*/
get diagonal2() {
return this.#diagonal2
}
}
74 changes: 74 additions & 0 deletions Geometry/Test/Kite.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Kite from '../Kite'

describe('Kite', () => {
describe('Constructor', () => {
test('creates a kite with valid dimensions', () => {
const kite = new Kite(5, 7, 10, 12)
expect(kite).toBeInstanceOf(Kite)
expect(kite.sideA).toBe(5)
expect(kite.sideB).toBe(7)
expect(kite.diagonal1).toBe(10)
expect(kite.diagonal2).toBe(12)
})

test('throws an error if any dimension is invalid', () => {
expect(() => new Kite(-5, 7, 10, 12)).toThrow(
'sideA must be a positive number.'
)
expect(() => new Kite(5, -7, -10, null)).toThrow(
'sideB must be a positive number.'
)
expect(() => new Kite(5, 7, null, null)).toThrow(
'diagonal1 must be a positive number.'
)
expect(() => new Kite(5, 7, 10, undefined)).toThrow(
'diagonal2 must be a positive number.'
)
})
})

describe('Area Calculation', () => {
test('calculates area correctly', () => {
const kite = new Kite(5, 7, 10, 12)
expect(kite.area()).toBe(60) // Area = (10 * 12) / 2
})
})

describe('Perimeter Calculation', () => {
test('calculates perimeter correctly', () => {
const kite = new Kite(5, 7, 10, 12)
expect(kite.perimeter()).toBe(24) // Perimeter = 2 * (5 + 7)
})
})

describe('Getters', () => {
test('sideA getter returns correct value', () => {
const kite = new Kite(5, 7, 10, 12)
expect(kite.sideA).toBe(5)
})

test('sideB getter returns correct value', () => {
const kite = new Kite(5, 7, 10, 12)
expect(kite.sideB).toBe(7)
})

test('diagonal1 getter returns correct value', () => {
const kite = new Kite(5, 7, 10, 12)
expect(kite.diagonal1).toBe(10)
})

test('diagonal2 getter returns correct value', () => {
const kite = new Kite(5, 7, 10, 12)
expect(kite.diagonal2).toBe(12)
})
})

describe('String Representation', () => {
test('returns correct string representation', () => {
const kite = new Kite(5, 7, 10, 12)
expect(kite.toString()).toBe(
'Kite: sideA = 5, sideB = 7, diagonal1 = 10, diagonal2 = 12, area = 60, perimeter = 24'
)
})
})
})