UML: Developing Applications
Contributed by Sherry Barkodar and maintained by Cindy Castillo and Bob May
November 2007 [Revision number: V1-4]
This publication is applicable to NetBeans IDE 6.0 and 6.1 releases.
In this tutorial, you learn how to use the UML features of the IDE to create a simple UML class diagram. You then use the code generation and reverse engineering features to develop a simple banking application, which you can test by executing a test class. This banking application is greatly simplified; a real model for an application of this type would require more detail. The purpose of this tutorial is to introduce some of the IDE's UML features, not to teach you about UML concepts or the Java programming language.
Expected duration: 25 minutes
Contents
Tutorial Requirements
Before you proceed, make sure you review the requirements in this section.
Prerequisites
To use this tutorial, the IDE must be installed on your system and you should be familiar with the basic parts of the IDE. You should also have a basic familiarity with the Java programming language and UML. For a basic understanding of the IDE, see the IDE Basics topics in the online help. A good resource for UML techniques and theory is the official UML resource page at http://www.uml.org/.
System Requirements
This tutorial assumes that your system meets the requirements specified in the Systems Requirements section of the NetBeans 6.1 Release Notes.
Software Needed for This Tutorial
To follow this tutorial, you need the software and resources listed in the following
table.
top
Creating the Java Project for the Application
In this section , you create a new Java project for the Java application you are going to develop in this tutorial.
- From the main menu, choose File > New Project and then do the following in the New Project wizard:
- Under Categories, select Java.
- Under Projects, select Java Application
- Click Next.
- In the Project Name field, type JavaPrj.
- For the Project Location field, click Browse, navigate to any directory on your computer. Click Open and type MyPrj.
- Clear the Set as Main Project and the Create Main Class checkboxes.
- Click Finish.
A progress dialog box appears.
When the new JavaPrj project is created, it appears in the Projects window.
View Demo
top
Creating the UML Project and the Class Diagram
In this section, you create the UML project (or UML project) and class diagram for the application. A UML project is the mechanism by which you store and manage a collection of files for a UML model. A UML model contains all of the model's diagrams, their associated elements, and metadata related to the UML model.
- To create the UML project, choose File > New Project and then do the following:
- Under Categories, select UML.
- Under Projects, select Java-Platform Model.
- Click Next.
The New Java-Platform Model dialog box opens.
- In the Project Name field, type UMLPrj.
Notice that when you type the Project Name, the IDE automatically suggests this name for the name of the Project Folder.
- Verify that the Project Location is MyPrj.
- Click Finish.
The IDE creates the UML project and the Create New Diagram dialog box appears.
- In the Diagram Type list, select Class Diagram.
- In the Diagram Name field, type BankClassDiagram.
- Leave UMLPrj in the Namespace field and click Finish.
The IDE does the following:
- Adds the UMLPrj project node in the Projects window.
- Creates a BankClassDiagram node under the Model node
- Displays the new diagram in the Diagram editor (the diagram is empty at this point)
- Opens the Modeling Palette
View Demo
top
Adding and Defining Class Elements
Now, add and define the class elements that will make up your Java application. You use the Class icon from the Modeling Palette to create the class elements.
- From the Basic section of the Modeling Palette, select the Class icon and click in the Diagram editor.
This action places a class element on the Diagram editor.
- Deselect the Class icon by right-clicking anywhere in the Diagram editor.
- Select the newly added class element, type BankAccount and press Enter.
The IDE does the following:
- Labels the element BankAccount
- Creates a public operation, BankAccount()
- Displays the properties of the BankAccount class in the Properties window
- With the BankAccount class element selected in the Diagram editor, right-click the word Attributes and choose Insert Attribute from the pop-up menu.
A one-line editor opens and displays the following information:
visibility type name[ranges]=initialValue{name=value}
- Type balance and press Enter.
A private attribute named balance of type int appears on the BankAccount class. The following operations are created on the class:
- public int getBalance()
- public void setBalance(int val)
- With the BankAccount class element still selected in the Diagram editor, right-click the word Operations and choose Insert Operation from the pop-up menu.
A one-line editor opens and displays the following information:
visibility returnType name(parameter) {properties...}
- Type withdraw, move your cursor (use the forward arrow on your keyboard) into the parentheses, type int amount and press Enter.
The IDE adds the new operation in the class element as follows:
public void withdraw(int amount)
View Demo
top
Adding More Elements to the Diagram
In this section, you use more UML icons from the Modeling Palette to add interfaces, packages, attributes, and operations to your application.
- From the Basic section of the Modeling Palette, select the Package icon .
Note: If necessary, scroll down to see the additional Modeling icons.
- Click in the Diagram editor to add a package element to the class diagram.
- Right-click anywhere in the Diagram editor to deselect the Package icon.
Note: As you add modeling elements to the diagram, you can select them and drag them to new locations in the Diagram editor to improve the appearance of the diagram. Be careful when you right-click, as in some positions, this action opens a pop-up menu for the Diagram editor. If this happens, just click again in the white space of the Diagram editor.
- With the package element selected, type bankpack and press Enter.
- From the Basic section of the Modeling Palette, select the Interface icon and click in the Diagram editor.
- Right-click anywhere in the Diagram editor to deselect the Interface icon.
- With the interface element selected, type Bank and press Enter.
- Add a deposit operation to the Bank interface.
You add operations to interfaces the same way you add them to classes (Step 6 in the preceding section). Define the operation as follows:
public void deposit(int amount)
- From the Modeling Palette, select the Class icon and click in the Diagram editor two times.
- Right-click anywhere in the Diagram editor to deselect the Class icon.
Note: If you draw too many class elements, deselect the Class icon, then right-click the class element you want to delete and select Edit > Delete.
- Name the class elements Checking and AccountTest and resize the elements if necessary.
View Demo
top
Identifying Associations Between Elements
In this section, you use the UML icons from the Modeling Palette to identify the association between class elements.
- From the Basic section of the Modeling Palette, select the Implementation icon and click inside the BankAccount class element.
- Click inside the Bank interface element and right-click anywhere in the Diagram editor to deselect the Implementation icon.
An implementation link appears between the class and the interface element. An implementation link denotes a relationship between a class and an interface.
- From the Basic section of the Modeling Palette, select the Generalization icon .
- Click inside the Checking class element (the subclass), then click inside the BankAccount class element (the superclass).
The Select Methods to Redefine dialog box appears.
- Select the withdraw method and click OK.
The IDE does the following:
- Closes the dialog box
- Adds the withdraw method to the Checking class
- Adds the Generalization link between the two related Class elements
A generalization link shows the relationship between a subclass and its superclass. Subclasses are refinements of the superclass, meaning they can inherit features (attributes and operations) from the superclass.
- Right-click in any white space in the Diagram editor to deselect the Generalization icon.
- From the Basic section of the Modeling Palette, select the Nested Link icon and click inside the BankAccount element and then inside the bankpack package element.
- Use the Nested Link icon as described in the prior step and connect the Checking, AccountTest, and Bank elements with the bankpack package.
- Deselect the Nested Link icon.
A nested link denotes how elements are organized into groups. In this case, you organized all the class elements into a group in the bankpack package.
- Press Ctrl-S anywhere in the Diagram editor to save the changes made to the model.
View Demo
top
Generating Java Source Code
In this section, using the Generate Code feature of UML you generate the Java source code for the UML model that you created in the previous sections.
- In the Projects window, right-click the UMLPrj node and choose Generate Code from the pop-up menu.
The Generate Code dialog box appears. and specifies the Target Project.
- Accept the default checkboxes in the Generate Code dialog box.
- Click OK.
The IDE generates the code and the output window displays the progress of the code generation process.
View Demo
top
Testing Your Work
- In the BankAccount class, add the following code to the deposit method:
setBalance(getBalance() + amount);
- Type (or copy and paste) the following code in the AccountTest class:
public static void main(String[] args) {
Checking myChecking = new Checking();
myChecking.deposit(100);
System.out.println("Checking Balance is: " +
myChecking.getBalance() );
}
This code creates a new Checking object and specifies a deposit of $100, then prints the results.
- In the Projects window, right-click the JavaPrj node and choose Build from the pop-up menu.
- In the Projects window, right-click the JavaPrj node and choose Run from the pop-up menu.
The Run Project dialog box appears.
- In the Run Project dialog box, click OK to select the
bankpack.AccountTest
as the main class.
The IDE executes the application and displays the following output in the Output window:
Checking Balance is: 100
You have now completed the application.
View Demo
top
Summary
In this tutorial, you designed a class diagram for a simple banking application. You learned how to perform the following tasks:
- Create a UML project
- Use the UML icons from the Modeling Palette to create classes, interfaces, packages, attributes, and operations
- Link the classes together with UML associations
- View the elements that you created in the Diagram editor in the UML project as represented in the Projects window
- Generate the source code for the elements created in the Diagram editor in the UML project and view the generated source in the Source Editor.
- Compile and execute classes from the Source Editor
top
Next Steps
-
To learn more about the UML feature, complete the Reverse Engineering tutorial.
To send comments and suggestions, obtain support, and stay informed of the latest
changes to the NetBeans IDE J2EE development features, join the nbusers@netbeans.org
mailing list.
top