I have recently begun studying UML. All is going fine so far until I saw the following:

Class Point2D

This is a class Called Point2D It has 2 attributes which are x, type float and y, type float. It has 3 methods

  1. __init __(takes parameters 'x' and 'y', both of which are float) returns nothing
  2. distance (takes parameter 'target' which has the type class ???) returns a float
  3. display() returns nothing

I have no problem of reading these but my issue arose with the method 'distance', especially on how to interpret the parameter 'target' being Point2D

How should this be interpreted?

Python reconstruction:

class Point2D:
x = float
y = float

def display(self):
    print('coordinate x ' + str(self.x) + ', coordinate y ' + str(self.y))

def distance(self, target):
    gap = sqrt(abs(target.x-self.x)**2 + abs(target.y-self.y)**2)
    # target is still an enigma but the code above should describe what 
    # distance is supposed to do

def __init__(self, x, y):
    self.y = float(y)
    self.x = float(x)
  • What do you think this could mean? Perhaps you could write some pseudocode to show how that method might be used or how it could be implemented. – amon Feb 17 at 13:05
  • 1
    The UML shows the Point2D used as an object that holds 2 properties (x,y), I am not a Python developer, but the code does not reflect the UML. Note that in UML you can decide on the level of detail you want to use, in many cases, the UML is not used to produce code, rather to show the overall structure. – NoChance Feb 17 at 13:11
  • 1
    Distance is usually a function with two parameters, a x and a y, say. Now, in OO, instead of writing d = distance(x,y) we would do something like d= x.distance(y) (though I personally hate that formulation. Pure math functions are really ugly in pure OO). So in UML, what would the specification of distance look like? – Kristian H Feb 17 at 13:15
  • 5
    Your thinking is inconsistent. Can you explain precisely and step-by-step, why you think that the types of the parameters of __init__ are float and float? Now, look at the steps you took and apply exactly the same steps to distance. What is the type of the target parameter? – Jörg W Mittag Feb 17 at 14:27
  • 1
    Your code example was not showing what distance is supposed to do, I took the freedom to fix that. And the fact you have problems of understanding this is most probably not a programming or softwareengineering or UML problem. "distance" here means the so-called Euclidean distance between the point "self" and the point "target". If the Wikipedia entry is too formal for you, google for that term, I am sure you will find a simpler explanation somewhere. – Doc Brown Feb 17 at 16:54

Yes, a type of parameter, attribute etc. can be a class. Simply speaking you need to pass an object of the right type, in your case an object being instance of class Point2D.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.