You will need to use these tools to facilitate the development of your programs.
StructureBuilder (SB) is a tool to create UML class diagrams and automatically generate Java "stub codes". SB can also reverse engineer Java source/byte code to produce the corresponding UML diagrams. StructureBuilder (SB) is available on Owlnet. If you have run the registration script (see lab #1), you should have it in your PATH.
In this lab, we will use SB to design a Pizza program. Perform the following steps to create the UML diagrams shown below. SB will generate code that reflects the class structure, but not the specific actions on any objects. For that, after using SB, you'll edit the resulting stub code to add the rest of the code, filling in what each method should do.
First, you need to initialize your account to use SB. From your home directory (type cd to return to your home, note, it is important that you are in your home directory, otherwise it won't work), type
propagate.shThis script is part of SB and sets you up with a license. You won't need to ever run this again.
NOTE: there is a bug in the above script preventing SB from saving the user's preferences. To fix it, enter the following command after running the above script.
cd
mkdir sb
mv sb.ini sb/
When you want to start SB, you need to be in your home directory (once again, if you're not, it won't work, and instead of giving you the fully licensed and working version, you get the evaluation copy running). Once you're in your home, just type
StructureBuilderSB should start. There will be an initial window -- leave the fields blank, and click on the "Standard Mode" button.
Prepare to start a completely new diagram:
In the File menu, choose Close All, to start your design from scratch.
In the Tools menu, choose Preferences, and set the Default Source Directory to your lab directory. This is where all the generated files will be saved.
In the File menu, choose New/Class Diagram, or equivalently, click on the "New Class Diagram" button. In the resulting dialog box, enter a name like "Pizza" for this diagram. Also, check the "Display Variables and Method Types" and the "Show Method Parameters" boxes. Then click on "OK".
You should see a new "Pizza" tab. Click on it.
In the File menu, choose New/Class, or equivalently, click on the
"New Class" button. In the left pane, a new class box should appear.
In the right pane, some Java code to declare the new class should appear.
Both will use a default class name (e.g., Class_0
).
We need to edit this new class. Double-click on the class diagram, or equivalently, right-click and choose Properties, and a Properties Dialog should appear.
Circle
. Make sure the Directory text box displays
the correct directory.
int
newVariable
. Select (highlight) the int
text and
type over double
. Select the newVariable
text, and type over _radius
. Make sure the "private"
radio button is selected.
Repeat the same process to create a class called Rectangle
with width and height fields of type double.
You may want to keep the Properties dialog open. Note that you can switch classes and create new classes from its interface also.
Create an interface called IShape
.
void newMethodName
. Change
this to double getArea
. Check the "abstract" check
box and the "public" radio button. NOTE: Though interface methods
are by definition public and abstract, we want to explicitly declare
them public and abstract in order for SB to display them correctly
The UML class diagram for IShape should display the getArea()
method in italic. In UML, all abstract methods are italicized.
Make Circle and Rectangle implement IShape by dragging the inheritance arrow (the middle one) from each subclass to IShape. You may need to drag the classes around for a more readable placement.
Generate method stubs for Circle and Rectangle by right-clicking each inheritance arrow, and selecting Generate Method Stubs. Circle and Rectangle should now each have a default stub code for the getArea() method.
Add a class called Pizza.
To make Pizza reference an IShape, select class Pizza. In the lower far-right corner of the Pizza class diagram is an "association" symbol. Click on it and drag it to the IShape class diagram.
A field (i.e. variable) called iShape is automatically generated. Rename the field to _shape.
Create a Get Routine for _shape. Rename get_shape to getShape.
Double-click on the link arrow, and check the aggregation check box. Aggregation simply means "has-a". You can add the text "has-a" to the link label and "_shape" to the Role A text box. These texts are like comments and have no effect on the code.
Constructors are special pieces of code used to initialize an instance of a class when it comes into existence.
To add a new method to a class:
void newMethodName
To add a parameter to a method:
select the desired method.
in the Parameters tab, click the New button. A default int newParam is created. Rename the type and the name accordingly.
Add a class callled PizzaClient which will make use of Pizza, PizzaDeal, Circle, and Rectangle to figure out the better deal. This class should have a special method with signature
public static void main (String[] args)
Add "dependencies" between different classes by dragging the dependency arrow icon (just left of the association icon) to another class. In this example, PizzaClient "depends" on Pizza, PizzaDeal, Circle, and Rectangle. You can enter a label for a dependency arrow by double-clicking on it and, filling out the pop-up dialog box. Dependency arrows have no effect on code generation.
Feel free to make modifications on your diagram. You can drag your class diagrams around and bend the arrows in many different ways. You just have to experiment with the tool.
In the File menu, select Save All, and use the file name Pizza. This should create the files PizzaClient.java, PizzaDeal.java, Pizza.java, IShape.java, Circle.java, and Rectangle.java in the specified directory.
Now define what this program actually does, i.e., define the methods described by your diagram. Use Emacs to add the methods to the corresponding class .java file. See week 1 lecture 3 class notes for the code you need. Remember that the IShape getArea method is abstract and so has no code body. Make sure you add comments to the code as shown in the sample code. The comments are written in "javadoc" style. You will learn more about javadoc in next week's lab.
If you set up SB properly, you should be able to generate javadoc and
UML diagrams for your code in one click of a button: go to the menu Tools/Generate
Documentation; select private access type to generate javadoc for all fields
and methods, select the class diagrams you want to javadoc, and click the
Generate button! You can submit the generated UML diagrams and javadoc
html files as the official javdoc documentation of your work. You
will not need to export the diagrams to any files at all.
More information on running StructureBuilder can be found here.
DrJava provided a way to edit and save java code with key words highlighting, curly brace matching, and an interactive environment to manipulate objects and test code without having to write the main method.
DrJava is already installed for Comp 212. All you have to do is execute the command drjava in an xterm window. You should see a GUI development environment that looks very similar to DrScheme. Here are the functions of the four subwindows in the GUI.
The top half of the GUI constitutes the Definitions window. You type in all the class definitions here. After you type in some code, you need to click on the save button before you can compile your code. All the classes in the Definitions window will be saved in a single file. There should only be one public class in the Definitions window, and the saved file should have the same name as that of the public class with the extension .java.
The bottom half of the GUI comprises three tabbed windows.
Lecture note #3 contains a syntax summary of the most common Java constructs.
Download the following Java file: TestFruit.java. This file contains the code for an interface and several related classes with syntax "bugs".
In Java, one source file can contain at most one public class/interface. Note that an interface is always public whether or not it is declared as public. Though it is a common Java practice to have one file per class/interface, we put everything in this one file to simplify the editing.
Open TestFruit.java in DrJava. Compile and fix all errors.