UML sequence diagrams illustrate the interactions between objects (that is, instances of classes). Before creating a UML sequence diagram, you must have a UML class diagram that illustrates the classes that comprise your application. See these instructions for creating UML class diagrams if you haven't yet performed this step. However, for this exercise, you'll be working with an EA project file that already contains some classes and a class diagram that correspond to this Java program:
package hello; import javax.swing.*; // here's where JOptionPane comes from... import java.util.*; // here's where Date comes from... import java.text.*; // here's where SimpleDateFormat comes from... /** * HelloWorldApp.java * @author: hornick * This program displays a welcome message in a dialog, and then * displays the current day/time in a second dialog. */ public class HelloWorldApp { /** * This is the main method of this program. * It delegates the real work to other methods */ public static void main(String args[]) { String text = "hello se1010" ; printWelcomeMessage( text ); printDate(); } /** * This method displays a welcome message in a JOptionPane dialog * It displays it twice: just as it came in, and also in uppercase * @param message Contains the text to be displayed in the dialog */ public static void printWelcomeMessage(String message ) { // take the incoming message and convert to uppercase String subtext = message.toUpperCase(); // print the message via a dialog box JOptionPane.showMessageDialog(null, "The message is: \n" + message + "\n or alternately: \n" + subtext ); } /** * This method displays the current day & time in a JOptionPane dialog */ public static void printDate() { Date today = new Date(); // creates a Date object with the current date and time when the code executes // We need a SimpleDateFormat object to convert the Date object to something printable SimpleDateFormat formatter = new SimpleDateFormat("EEEE HH:mm:ss"); // Note: the "EEEE HH:mm:ss" format displays e.g. "Thursday 10:02:34" // Now print the formatted date String dateString = formatter.format(today); JOptionPane.showMessageDialog(null, dateString ); } }
Follow this procedure to create your first sequence diagram:
Open a Project File.Open Project dialog, browse to the directory where you saved the downloaded example project file, and press the Open button.HelloWorld, should now appear in the Recent Projects list in the main EA window, and the project should be opened (HelloWorld - EA should now appear in the EA title bar.)Project Browser window (default location: upper right) is not visible , select Project Browser from the View menu. (If the project browser tends to disappear, you can lock it in place by clicking the “push pin” icon; to release the browser, click the icon again.)Logical View to expand it. You'll see an icon labeled Class diagram appear below Logical View.Class diagram and to open the class diagram. You'll see the classes that comprise this application within the class diagram, along with the relationship between the classes.Class Diagram on the tab at the bottom of the main window.Dynamic View to expand it. You'll see an icon labeled Sequence diagram appear below Dynamic View.Sequence diagram and to open the sequence diagram. The diagram will be empty.Sequence Diagram on the tab at the bottom of the main window.Toolbox window (default location: upper left) is not visible, select Toolbox from the View menu. (The “push pin” may be useful here too).Interaction Elements tools are not visible, click on More tools… and select UML → Interaction.Actor from the Toolbox and drag it onto upper left corner of the Sequence Diagram.Logical View. The HelloWorldApp class icon should appear. Left-click the HelloWorldApp class and drag the icon onto the Sequence Diagram.Paste Element dialog appears. Select As Simple Link and press the OK button. You select As Simple Link when you are working with a class that is not instantiated as an object. In this case the HelloWorldApp is never instantiated since it contains only class (static) methods.HelloWorldApp element appears in the Sequence diagram. Reposition it towards the left of the window by dragging it with the mouse.JOptionPane class, which is also a non-instantiated element. The JOptionPane class is in the javax.swing package.main() method from the HelloWorldApp class. We represent this call in the Sequence Diagram by drawing an arrow from the user's lifeline to the HelloWorldApp.main(String[]) from the “Message” drop-down menu and click OK.main() method invokes the printWelcomeMessage() method, followed by the printDate() method. To illustrate the method call from main() to printWelcomeMessage:HelloWorldApp element, and click the mouse on the dotted line.main() method activation box to the printWelcomeMessage() activation box.Message Properties dialog appears. From the Message drop-down list, select printWelcomeMessage() and type text in the “Parameter Values” field.showMessageDialog() from the drop-down menu and type null, “The message is…” in the “Parameter Values” field.main() method's activation box instead of the printWelcomeMessage() method's activation box, where it should be. To fix this, select the message arrow. A small arrow will appear. Click on this arrow to “Raise the activation level.”printDate() method from the main() method, repeat the above procedure to create a self-message for the printDate() method.printDate() method creates a Date object named today:java/util package under Logical View. The Date class icon should appear. Left-click the Date class and drag the icon onto the Sequence Diagram.Paste Element dialog appears. Select As an instance of element (object) and press the OK button. You select As an instance of element when you are working with a class that must be instantiated as an object.Date element appears in the Sequence diagram. Reposition it towards the left of the window by dragging it with the mouse.Date element, and in the subsequent dialog, enter the name today of the Date object.SimpleDateFormat class, which is found in the java/text package.today Date object within the printDate() method,printDate() method of the HelloWorldApp element, and click and hold the left mouse button.Date element and release the mouse button.Message Properties dialog appears. From the Message drop-down list, select Date() indicating the call to the Date constructor.today.printDate() method.Save from the Diagram menu. (Many changes will be saved automatically, but some will not.)Save Image to File from the Diagram menu. Alternatively, you can copy the class diagram to the clipboard by selecting Save Image to Clipboard from the Diagram menu.When you're done, you should have something that looks like this: