diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js new file mode 100644 index 0000000000..c1f432bff9 --- /dev/null +++ b/Geometry/Rectangle.js @@ -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()}` + } +} diff --git a/Geometry/Test/Rectangle.test.js b/Geometry/Test/Rectangle.test.js new file mode 100644 index 0000000000..42c350654d --- /dev/null +++ b/Geometry/Test/Rectangle.test.js @@ -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' + ) + }) + }) +})