Bits & Bytes

Posts Tagged ‘tutorial’

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.

UML Class Diagrams – part 1

UML stands for the Unified Modeling Language and it is a system for communicating the design and development of a software system. Object-oriented programming (OOP) has developed as the major tool to manage program complexity, and UML is the primary method of diagramming object-oriented programming structures and interactions. UML diagrams are used to create designs, communicate between developers, and document code so that it can be understood quickly. The most common type of UML diagram is a class diagram, which is sometimes called a static diagram.

A class diagram models classes and the relationships between them. The central element of class diagrams is a rectangular box that is used to represent a class. Typically, the box is separated into three sections that hold the class name, attributes, and operations. Attributes and operations are generic object-oriented design (OOD) terms for variables and functions. The layout of these items is shown here:

A simple example class should help to make these abstractions concrete. The point class header shown below has two attributes that given by the x and y double variables that represent the coordinates of the point in the plane. The class also has two operations: one to display the point and one to find the distance to another point. The point class has four members in all, all of which are public.

class CPoint
{
public:
    double mdX;
    double mdY;

    void Display();
    double Distance(CPoint qP);
};

With a basic illustration, this class can be represent by the simple UML diagram below. Notice that the member variables are in the attributes section and the member functions are in the operations section. That’s pretty straightforward, so far. However, this is just a simple diagram, and there are many other elements that we could have shown in it.

Eventually, we will explain how to diagram this class completely and much more, but it is important to remember that diagrams do not need to display all of the details of the code. UML diagrams are for communication and understanding; they should be used to emphasize the elements that are important to the context. As we will demonstrate in our upcoming examples, extraneous details will often be omitted to highlight important points.

To be continued . . .

 

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