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

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Polymorphism step by step in the jq 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 jq programming language

Source code in the jq programming language

def Point(x;y): {"type": "Point", "x": x, "y": y};
def Point(x): Point(x;0);
def Point: Point(0);

def Circle(x;y;r): {"type": "Circle", "x": x, "y": y, "r": r};
def Circle(x;y): Circle(x;y;0);
def Circle(x): Circle(x;0);
def Circle: Circle(0);

def print:
  if  .type == "Circle" then "\(.type)(\(.x); \(.y); \(.r))"
  elif .type == "Point" then "\(.type)(\(.x); \(.y))"
  else empty
  end;

# keyname should be (or evaluate to) a string
def set(keyname; value): 
  if type == "object" and .type and has(keyname) then .[keyname] = value 
  else error("set: invalid type: \(.)")
  end;

Circle(0;1;2) | .x = 1 | print


  

You may also check:How to resolve the algorithm Character codes step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Primorial numbers step by step in the Julia programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the Erlang programming language
You may also check:How to resolve the algorithm Monty Hall problem step by step in the REXX programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the AutoHotkey programming language