From b5ee1f6026888a8977a8aaaf6d004f7ff5ea011d Mon Sep 17 00:00:00 2001 From: saahil-mahato Date: Tue, 1 Oct 2024 10:43:19 +0545 Subject: [PATCH 1/3] feat: add geometry rectangle --- Geometry/Rectangle.js | 21 +++++++++++++++++++++ Geometry/Test/Rectangle.test.js | 11 +++++++++++ 2 files changed, 32 insertions(+) create mode 100644 Geometry/Rectangle.js create mode 100644 Geometry/Test/Rectangle.test.js diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js new file mode 100644 index 0000000000..9aa590130c --- /dev/null +++ b/Geometry/Rectangle.js @@ -0,0 +1,21 @@ +/** + * This class represents a rectangle and can calculate its perimeter and area + * https://en.wikipedia.org/wiki/Rectangle + * @constructor + * @param {number} length - The length of the rectangle. + * @param {number} breadth - The breadth of the rectangle. + */ +export default class Rectangle { + constructor(length, breadth) { + this.length = length + this.breadth = breadth + } + + perimeter = () => { + return 2 * (this.length + this.breadth) + } + + area = () => { + return this.length * this.breadth + } +} diff --git a/Geometry/Test/Rectangle.test.js b/Geometry/Test/Rectangle.test.js new file mode 100644 index 0000000000..16afd01fc8 --- /dev/null +++ b/Geometry/Test/Rectangle.test.js @@ -0,0 +1,11 @@ +import Rectangle from '../Rectangle' + +const rectangle = new Rectangle(3, 4) + +test('The perimeter of rectangle with length equal to 3 and breadth equal to 4', () => { + expect(parseFloat(rectangle.perimeter().toFixed(2))).toEqual(14.0) +}) + +test('The area of rectangle with length equal to 3 and breadth equal to 4', () => { + expect(parseFloat(rectangle.area().toFixed(2))).toEqual(12.0) +}) From 13d1dddb2e66d6a957f3ebedacf9187085a33e8c Mon Sep 17 00:00:00 2001 From: saahil-mahato Date: Tue, 1 Oct 2024 11:22:07 +0545 Subject: [PATCH 2/3] refactor: rewrite with latest standard --- Geometry/Rectangle.js | 86 ++++++++++++++++++++++++++++----- Geometry/Test/Rectangle.test.js | 82 ++++++++++++++++++++++++++++--- 2 files changed, 151 insertions(+), 17 deletions(-) diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js index 9aa590130c..a05d8dd814 100644 --- a/Geometry/Rectangle.js +++ b/Geometry/Rectangle.js @@ -1,21 +1,85 @@ /** - * This class represents a rectangle and can calculate its perimeter and area - * https://en.wikipedia.org/wiki/Rectangle - * @constructor - * @param {number} length - The length of the rectangle. - * @param {number} breadth - The breadth of the rectangle. + * Represents a rectangle and provides methods to calculate its perimeter and area. + * @see {@link https://en.wikipedia.org/wiki/Rectangle|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.length = length - this.breadth = 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 } - perimeter = () => { - return 2 * (this.length + this.breadth) + /** + * Gets the breadth of the rectangle. + * @public + * @returns {number} The breadth of the rectangle. + */ + get breadth() { + return this.#breadth } - area = () => { - return this.length * 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 index 16afd01fc8..42c350654d 100644 --- a/Geometry/Test/Rectangle.test.js +++ b/Geometry/Test/Rectangle.test.js @@ -1,11 +1,81 @@ import Rectangle from '../Rectangle' -const rectangle = new Rectangle(3, 4) +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('The perimeter of rectangle with length equal to 3 and breadth equal to 4', () => { - expect(parseFloat(rectangle.perimeter().toFixed(2))).toEqual(14.0) -}) + 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) + }) + }) -test('The area of rectangle with length equal to 3 and breadth equal to 4', () => { - expect(parseFloat(rectangle.area().toFixed(2))).toEqual(12.0) + 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' + ) + }) + }) }) From c1cdbd2f7453fb3684917bb1165a362f589dab4f Mon Sep 17 00:00:00 2001 From: saahil-mahato Date: Tue, 1 Oct 2024 12:52:13 +0545 Subject: [PATCH 3/3] fix: wikipedia link --- Geometry/Rectangle.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js index a05d8dd814..c1f432bff9 100644 --- a/Geometry/Rectangle.js +++ b/Geometry/Rectangle.js @@ -1,6 +1,6 @@ /** - * Represents a rectangle and provides methods to calculate its perimeter and area. - * @see {@link https://en.wikipedia.org/wiki/Rectangle|Rectangle} + * 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 */