Skip to content

feat: add geometry rectangle #1694

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 3 commits 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
85 changes: 85 additions & 0 deletions Geometry/Rectangle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* This class represents a rectangle and provides methods to calculate its perimeter and area.
* @see {@link https://en.wikipedia.org/wiki/Rectangle}
*/
export default class Rectangle {
/** @private */
#length

/** @private */
#breadth

/**
* Creates a new Rectangle instance.
* @constructor
* @param {number} length - The length of the rectangle.
* @param {number} breadth - The breadth of the rectangle.
* @throws {Error} Will throw an error if length or breadth is not a positive number.
*/
constructor(length, breadth) {
this.#validateDimension(length, 'length')
this.#validateDimension(breadth, 'breadth')
this.#length = length
this.#breadth = breadth
}

/**
* 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 perimeter of the rectangle.
* @public
* @returns {number} The perimeter of the rectangle.
*/
perimeter() {
return 2 * (this.#length + this.#breadth)
}

/**
* Calculates the area of the rectangle.
* @public
* @returns {number} The area of the rectangle.
*/
area() {
return this.#length * this.#breadth
}

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

/**
* Gets the breadth of the rectangle.
* @public
* @returns {number} The breadth of the rectangle.
*/
get breadth() {
return this.#breadth
}

/**
* Returns a string representation of the rectangle.
* @public
* @returns {string} A string describing the rectangle.
*/
toString() {
return `Rectangle: length = ${this.#length}, breadth = ${
this.#breadth
}, area = ${this.area()}, perimeter = ${this.perimeter()}`
}
}
81 changes: 81 additions & 0 deletions Geometry/Test/Rectangle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Rectangle from '../Rectangle'

describe('Rectangle', () => {
describe('Constructor', () => {
test('creates a rectangle with valid dimensions', () => {
const rectangle = new Rectangle(5, 3)
expect(rectangle).toBeInstanceOf(Rectangle)
expect(rectangle.length).toBe(5)
expect(rectangle.breadth).toBe(3)
})

test('throws an error if length is not a positive number', () => {
expect(() => new Rectangle(-1, 3)).toThrow(
'length must be a positive number.'
)
expect(() => new Rectangle(0, 3)).toThrow(
'length must be a positive number.'
)
expect(() => new Rectangle('5', 3)).toThrow(
'length must be a positive number.'
)
})

test('throws an error if breadth is not a positive number', () => {
expect(() => new Rectangle(5, -1)).toThrow(
'breadth must be a positive number.'
)
expect(() => new Rectangle(5, 0)).toThrow(
'breadth must be a positive number.'
)
expect(() => new Rectangle(5, '3')).toThrow(
'breadth must be a positive number.'
)
})
})

describe('Perimeter Calculation', () => {
test('calculates perimeter correctly', () => {
const rectangle = new Rectangle(5, 3)
expect(rectangle.perimeter()).toBe(16)
})

test('calculates perimeter correctly for a square', () => {
const square = new Rectangle(4, 4)
expect(square.perimeter()).toBe(16)
})
})

describe('Area Calculation', () => {
test('calculates area correctly', () => {
const rectangle = new Rectangle(5, 3)
expect(rectangle.area()).toBe(15)
})

test('calculates area correctly for a square', () => {
const square = new Rectangle(4, 4)
expect(square.area()).toBe(16)
})
})

describe('Getters', () => {
test('length getter returns correct value', () => {
const rectangle = new Rectangle(5, 3)
expect(rectangle.length).toBe(5)
})

test('breadth getter returns correct value', () => {
const rectangle = new Rectangle(5, 3)
expect(rectangle.breadth).toBe(3)
})
})

describe('toString Method', () => {
test('returns correct string representation', () => {
const rectangle = new Rectangle(5, 3)
expect(rectangle.toString()).toBe(
'Rectangle: length = 5, breadth = 3, area = 15, perimeter = 16'
)
})
})
})