Bits & Bytes

Posts Tagged ‘computer’

UML Class Diagrams – part 5

Continued . . .

For this section, we pull together the material for modeling operations and attributes to give a specification of the full form and then discuss modeling for the appropriate level of detail. For attributes and operations, the name is always used, but the rest of the elements are optional. By convention, the class name is in the top box, followed by attributes and operations, respectively. This is the common convention that we have used and it is highly recommended, but UML does allow for any ordering.

For attributes, we have full form shown below. The name is mandatory, and the rest of the optional elements are shown in C++ template-style brackets ( < > ) to indicate that they are optional. First, we have the visibility, which is shown as +, #, , or ~ for public, protected, private, or package level visibility, respectively. Then we have the name. The attribute could contain multiple instances of its type; the allowed range of multiplicity can be specified in bracket notation like this [0..1], [n], or [0..*], which correspond to {0, 1}, {n}, or {0, 1, 2, … } in set notation, respectively. After this, we have a colon followed by the type. Then an “=” and an initial value. Finally, we have a the properties list, each of which may have a value setting.

Attributes

<visibility> name <Multiplicity> <: Type> <= InitialValue> <{Properties}> 
Properties
Property1 <= Value1>, ... 

For operations, the full form is shown below. Again, the optional elements are show in C++ template-style brackets. Just as with attributes, we have the visibility followed by the name. Inside the parentheses, we have the arguments, which can have a direction, name, type, and default value, as shown under the Arguments heading. After the arguments, we have a colon and the return type. Finally, we have the properties in braces, just as with the attributes.

Operations

<Visibility> Name(Arguments) <: ReturnType> <{Properties}>
Arguments
<Direction1> <ArgName1> <: ArgType1> <= DefaultValue1> ; ...
Properties
Property1 <= Value1> , ...

Below, we have a class for creating cubic polynomials. These are polynomials of the form

F(x) = c0x0 + c1x1 + c2x2 + c3x3

= c0 + c1x + c2x2 + c3x3,

where ci is a constant coefficient of the appropriate degree. The function F() is used to evaluate the polynomial at a particular value of x. For this class, we will assume that a constructor exists (not shown) that initializes the cubic to be the zero polynomial (all zero coefficients).

class CCubic
{
public:
    double F(double dX = 0.0) const;
private:
    double mdaC[4];
};

This class can be diagramed in UML, as shown below, with in the fullest form for attributes and operations. Here, we use the assumption that the cubic is initialized in a constructor somewhere to be the zero polynomial; of course, that make the polynomial a constant and not a cubic. The coefficients can be changed, so we indicate that with the “changeable” property. Also, the function F() is “const” because does not change the values of the coefficients. Moreover, it is statically bound. Therefore, we have modeled it with the property specifications “isQuery” and “leaf” to indicate these properties.

This an elaborate specification, but is reasonable for modeling a simple class like this. However, for a diagram with many attributes and operations, this level of detail can get messy and difficult to read. Moveover, many of the details may not be relevant in a given context. For example, when designing this class it is probably not very important what the initial values of the coefficients are or the default value of dX in the function F(). So, we might use the cleaner version below.

Likewise, the fact that the coefficients are “changeable” is probably obvious as are the properties of the function F(). Furthermore, the use of doubles is probably clear and may not even be material to the design. So, we can throw those out as well as the obvious fact that dX is an input parameter. Our greatly simplified diagram now looks like this.

Finally, in a larger context, we may only be concerned that a cubic class exists and we can throw out the attributes and operations entirely, as we demostrate below. Notice that our diagram has three compartments, even though two ar empty. In this case, we could use simply one compartment with the class name or we could use three like this to indicate that it is important to keep in mind that this class has attributes and operations.

UML Class Diagrams – part 4

Continued . . .

In this section, we give the final details of the attributes and operations that can be modeled with UML. With the central elements covered, we are going to add some examples of attributes with initial values and operations with default argument values. Additionally, we will give some examples of attributes and operations with properties, and we will talk about some of the commonly used properties. Finally, we will show how to of model scope and operation binding.

Our arc class below, called CAArc, should serve to illustrate the remaining elements of UML that can be modeled for operations and attributes. This class contains three operations and three attributes. Of the operations, two are instance operations and the third is a class operation. The word static is used in C++ to indicate a class member, rather than an instance member. The word virtual before GetLength() indicates that that function has dynamic binding, while GetOrientation() is statically, or compile-time, bound. Also, the ” = 0″ is used to indicate that GetLength() is not implemented for this class, but rather is a pure virtual function or an abstract operation.

Of the classes attributes, two are instance members and one, the constant skdPi, is a class member. The constant is initialized at compile-time to the value 3.14159. The other member variables, dStartAngle and dEndAngle, have no initial value that is apparent here and are non-constant, so they can be changed at anytime.

class CAArc
{
public:
    virtual double GetLength() = 0;
    int GetOrientation();
    static double GradToDeg(double dA = 0.0);
private:
    static const double skdPi = 3.14159;
    double dStartAngle;
    double dEndAngle;
};

The arc class above can be modeled in UML using the diagram below. Since the GetLength() is an abstract operation, we italicize it to show this. We use the property “leaf” to indicate that the operation GetOrientation() is statically bound and can not be overridden. The operation is GradToDeg() has an argument with a default argument value, as shown. Also, we have underlined GradToDeg() to show that it is a class operation. Likewise, we underline skdPi, since it is a constant with class scope. The initial value for skdPi is shown along with the qualifier “frozen” to indicate that this is a constant. The instance members dStartAngle and dEndAngle are just ordinary member variables.

As far as properties, notice that the constant skiPi has the property “frozen” after it and the function GetOrientation() has the property “leaf” after it. However, we also have the property “changeable” on our instance variables. This can be used with attributes to specify that the variable’s value can be changed. We can also use “addOnly” on attributes with multiplicity to indicate that the values can only be added, say to an array, for instance. For operations, we can use the property “isQuery” to indicate that the operation does not change values; this is the C++ equivalent of a function that is constant. Additionally, we can specify operations having the properties “sequential”, “guarded”, or “concurrent” in the context of processes and threads.

To be continued . . .

PHP: How to Find a Substring in a String

How do you find a substring within a string? In PHP, the answer is not quite as straightforward as it should be. But, it’s still easy to do–you just need to be aware of a couple things.

Suppose we have the string “ABCDEFG”. There are two cases we want to look at: when we want to find “DEF” in the middle of the string, and when we want to find “ABC” at the beginning.

The function to find a substring is called strpos(). It can take up to three parameters:

strpos($haystack, $needle, $offset);

  1. $haystack: (required) the full string we want to search within
  2. $needle: (required) the substring we are searching for
  3. $offset: (optional) if we want the function to start looking for the substring at a position which is not at the beginning of the $haystack

When we call strpos() on the string, it will return the position of the substring as an integer (starting from 0), if it finds it. If it doesn’t find the substring, it will return false (which is 0 in PHP). So, when we search for “DEF”, the function will return 3 which is the position of the “D” (base 0). However, when we search for “ABC”, strpos() will return 0 because it found the substring at position 0.

So, we have to construct our code in this way to check the return value from the function:

$ret = strpos("ABCDEFG", "ABC");

if($ret === false) {
    echo("We did not find the substring.");
} else {
    echo("We found the substring!");
}

Here are some sample cases:

strpos(“ABCDEFG”, “ABC”); –> returns integer 0, substring found

strpos(“ABCDEFG”, “DEF”); –> returns integer 3, substring found

strpos(“ABCDEFG”, “XYZ”); –> returns a 0 = to PHP’s false keyword. substring not found

In the above code, the operator === checks whether two values are identical, which means whether the two values are of the same PHP type. The strpos() function will return a 0 that is equivalent to the false keyword in PHP if it does not find the substring. It will return an integer 0 if it finds the substring starting at position 0.  So, we use the operator which checks whether the two values are identical, and if a 0 was returned, whether the function meant that 0 to be false or to be the position of the substring it found.

 

© 2007–2024 XoaX.net LLC. All rights reserved.