COMP 212 Lab 2: DrJava, Java Syntax & StructureBuilder

This tutorial covers:

You will need to use these tools to facilitate the development of your programs.


DrJava

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.

Running DrJava

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.

  1. Interactions window: this is where you can type in any valid Java statement.  Usually, you would type in code to instantiate objects  of classes defined in the Definitions window, and call their methods to check whether or not they perform correctly.  Typing a valid Java expression terminated with a semi-colon and then pressing the Return (Enter) key, will cause DrJava to evaluate the expression but NOT printing the result.  If you want DrJava to print the value of the result in the Interactions window, you should press Return without terminating the expression with a semi-colon.  There is a menu item to reset the Interactions window, but it is buggy at this point.   The best way to clear the Interactions window is to force a re-compile by editing the Definitions window.
  2. Compiler Output window: Every time you compile your code, DrJava will display all compile error messages here.  Clicking on an error message will highlight the line where the error is suspected to take place in the Definitions window.  If there is no compile error, DrJava will declare success in this window.
  3. Console Output window: All console output will be printed in this window.

Java Syntax

The file JavaSyntax.html contains a syntax summary of the most common Java constructs.

Download the following Java file: TestFruit.java.  Open it in DrJava.  Compile and fix all errors.


StructureBuilder

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.

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.

  1. First, you need to initialize your account to use SB by running

         /usr/site/sb-4.0/propagate.sh
         
    You won't need to ever run this again.
  2. Now, start SB with

         /usr/site/sb-4.0/StructureBuilder
         
    There will be an initial window -- leave the fields blank, and click on the "Standard Mode" button.
  3. Prepare to start a completely new diagram:

  4. Now, make the diagram:
    1. 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 defaul 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.

      • Select the Classes tab. Change the class name (in the Name textbox) to Circle. Make sure the Directory text box displays the correct directory.
      • Select the Variables tab. Add the radius field by clicking on the New button Variables list box. By default, you will get 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.

    2. 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.

    3. Create a class called AShape.

      • Make the class abstract by checking the abstract check box in the Class tab of the class properties dialog.
      • To add an abstract method for computing the area, select the Methods tab. In the Methods list box, select New/User Method. By default you will get the method void newMethod. Change this to double getArea. Check the "abstract" and "public" check boxes.
      The UML class diagram of AShape should be displayed in italic font.
    4. Make Circle and Rectangle subclasses of AShape by dragging the inheritance arrow (the middle one) from each subclass to the superclass. You may need to drag the classes around for a more readable placement.

    5. 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.

    6. Add a class called Pizza.

      • Add a price field of type double. While that new field is selected, check the Create Get Routine box. That creates a method get_price returning that field.
      • Add a method, getArea returning a double. For consistency, rename get_price to getPrice.
    7. To make Pizza reference an AShape, 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 AShape class diagram. Double-click on the link arrow, and check the aggregation check box. Aggregation simply means "has-a". It has no effect on code generation.

    8. Constructors are special pieces of code used to initialize an instance of a class when it comes into existence.

      • To add a constructor for Pizza, select the Methods tab of its Properties, and select New/Constructor. In the Parameters area, add two new parameters: double price and AShape _shape.
      • Add a Circle constructor with parameter double radius.
      • Add a Rectangle constructor with parameters double width and double height.
      Of course, you can also create constructors when you originally create classes.
    9. Add a class callled PizzaClient which will make use of Pizza, Circle, and Rectangle to figure out the better deal. This class should have a special method with signature

           public static void main (String[] args)
                
    10. 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, 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. Dependecy arrows have no effect on code generation.

    11. 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.

    12. In the File menu, select Save All, and use the file name Pizza. This should create the files PizzaClient.java, Pizza.java, AShape.java, Circle.java, and Rectangle.java in the specified directory.

  5. 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 the class handout for the code you need. Remember that the AShape getArea method is abstract and so has no code body.

 

More information on running StructureBuilder can be found here.