How to resolve the algorithm Polymorphism step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Polymorphism step by step in the JavaScript programming language

Table of Contents

Problem Statement

Create two classes   Point(x,y)   and   Circle(x,y,r)   with a polymorphic function print, accessors for (x,y,r), copy constructor, assignment and destructor and every possible default constructors

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Polymorphism step by step in the JavaScript programming language

The provided JavaScript code defines two classes, Point and Circle, using constructor functions and prototypes to create and manipulate geometric objects. Here's a detailed explanation of the code:

  1. Point Class:

    • The Point class represents a point in a two-dimensional Cartesian coordinate system.
    • The constructor function Point() takes two optional arguments:
      • x: The x-coordinate of the point (default: 0).
      • y: The y-coordinate of the point (default: 0).
    • If the first argument is an instance of Point, it copies the x and y coordinates from that point.
    • Otherwise, it uses the provided values or defaults for x and y.
    • The Point class has setter functions set_x and set_y to modify the x and y coordinates, respectively.
  2. Circle Class:

    • The Circle class represents a circle defined by its center (a Point) and a radius.
    • The constructor function Circle() takes three optional arguments:
      • x: The x-coordinate of the circle's center (default: 0).
      • y: The y-coordinate of the circle's center (default: 0).
      • r: The radius of the circle (default: 0).
    • If the first argument is an instance of Circle, it copies the x, y, and r values from that circle.
    • If the first argument is an instance of Point, it uses that point as the center and the second argument (if provided) as the radius.
    • Otherwise, it uses the provided values or defaults for x, y, and r.
    • The Circle class has setter functions set_x, set_y, and set_r to modify the x-coordinate of the center, the y-coordinate of the center, and the radius, respectively.
  3. Prototype Methods:

    • Both Point and Circle classes have a print method defined in their prototypes. This method prints the coordinates or properties of the object in the specified format.

In summary, the provided code creates classes for geometric objects and provides methods for constructing, setting, and printing these objects. These classes allow you to easily create and modify points and circles in your JavaScript applications.

Source code in the javascript programming language

/* create new Point in one of these ways:
 *    var p = new Point(x,y);
 *    var p = new Point(a_point);
 * default value for x,y is 0
 */
function Point() {
    var arg1 = arguments[0];
    var arg2 = arguments[1];

    if (arg1 instanceof Point) {
        this.x = arg1.x;
        this.y = arg1.y;
    }
    else { 
        this.x = arg1 == null ? 0 : arg1;
        this.y = arg2 == null ? 0 : arg1;
    } 

    this.set_x = function(_x) {this.x = _x;}
    this.set_y = function(_y) {this.y = _y;}
}

Point.prototype.print = function() {
    var out = "Point(" + this.x + "," + this.y + ")";
    print(out);
}

/* create new Circle in one of these ways:
 *    var c = new Circle(x,y,r);
 *    var c = new Circle(a_circle);
 *    var c = new Circle(a_point,r);
 * default value for x,y,r is 0
 */
function Circle() {
    var arg1 = arguments[0];
    var arg2 = arguments[1];
    var arg3 = arguments[2];

    if (arg1 instanceof Circle) {
        this.x = arg1.x;
        this.y = arg1.y;
        this.r = arg1.r;
    }
    else if (arg1 instanceof Point) {
        this.x = arg1.x;
        this.y = arg1.y;
        this.r = arg2 == null ? 0 : arg2;
    }
    else { 
        this.x = arg1 == null ? 0 : arg1;
        this.y = arg2 == null ? 0 : arg2;
        this.r = arg3 == null ? 0 : arg3;
    } 

    this.set_x = function(_x) {this.x = _x;}
    this.set_y = function(_y) {this.y = _y;}
    this.set_r = function(_r) {this.r = _r;}
}

Circle.prototype.print = function() {
    var out = "Circle(" + this.x + "," + this.y + "," + this.r + ")";
    print(out);
}


  

You may also check:How to resolve the algorithm Long multiplication step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Brownian tree step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Legendre prime counting function step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Vigenère cipher step by step in the JavaScript programming language