Jump to content

Object-Oriented Programming/Polymorphism/JavaScript

From Wikiversity

index.js

[edit | edit source]
// This program demonstrates use of the Polygon, Triangle, Rectangle, and Square classes.

const Polygon = require("./polygon");
const Square = require("./square");
const Rectangle = require("./rectangle");
const Triangle = require("./triangle");

main();

function main() {
    let triangle = new Polygon([3, 4, 5]);
    console.log("triangle sides:", triangle.sides);
    console.log("triangle perimeter:", triangle.perimeter);

    let rectangle = new Polygon([4, 2, 4, 2]);
    console.log("rectangle sides:", rectangle.sides);
    console.log("rectangle perimeter:", rectangle.perimeter);

    let square = new Polygon([2, 2, 2, 2]);
    console.log("square sides:", square.sides);
    console.log("square perimeter:", square.perimeter);

    triangle = new Triangle(3, 4, 5);
    console.log("triangle sides:", triangle.sides);
    console.log("triangle perimeter:", triangle.perimeter);
    console.log("triangle area:", triangle.area);

    rectangle = new Rectangle(4, 2);
    console.log("rectangle sides:", rectangle.sides);
    console.log("rectangle perimeter:", rectangle.perimeter);
    console.log("rectangle area:", rectangle.area);

    square = new Square(2);
    console.log("square sides:", square.sides);
    console.log("square perimeter:", square.perimeter);
    console.log("square area:", square.area);
}

polygon.js

[edit | edit source]
// Supports polygon calculations for perimeter.

class Polygon {
    _sides = undefined;

    // Creates a polygon object.
    constructor(sides) {
        this.sides = sides;
    }

    get perimeter() {
        return this.sides.reduce((a, b) => a + b, 0);
    }
}

module.exports = Polygon;

rectangle.js

[edit | edit source]
// Supports rectangle calculations for perimeter and area.

const Polygon = require("./polygon");

class Rectangle extends Polygon {
    // Creates a rectangle object.
    constructor(length, width) {
        super(length, width, length, width);
    }

    // Gets area of the rectangle.
    get area() {
        let length = this.sides[0];
        let width = this.sides[1];
        if (length == width) {
            width = this.sides[2];
        }
        let area = length * width;
        return area;
    }
}

module.exports = Rectangle;

square.js

[edit | edit source]
// Supports square calculations for perimeter and area.

const Polygon = require("./polygon");

class Square extends Polygon {
    // Creates a square object.
    constructor(side) {
        super(side, side, side, side);
    }

    // Gets area of the square.
    get area() {
        let side = this.sides[0];
        let area = side * side
        return area;
    }
}

module.exports = Square;

triangle.js

[edit | edit source]
// Supports triangle calculations for perimeter and area.

const Polygon = require("./polygon");

class Triangle extends Polygon {
    // Creates a triangle object.
    constructor(side1, side2, side3) {
        super(side1, side2, side3);
    }

    // Gets area of the triangle.
    // References: 
    // https://www.mathsisfun.com/geometry/herons-formula.html 
    get area() {
        let s = this.perimeter / 2;
        let a = this.sides[0];
        let b = this.sides[1];
        let c = this.sides[2];
        let area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
        return area;
    }
}

module.exports = Triangle;

Try It

[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own JavaScript compiler / interpreter / IDE.

See Also

[edit | edit source]