COMP 212 Lab 1: Unix, Emacs, StructureBuilder, JDK

This tutorial covers the basics of using the main software tools of this and later courses:

These instructions do not necessarily apply to systems other than Owlnet Sun workstations. While you may do your coursework on other computers, software is not guaranteed to behave identically.

Labbies: Don't spend too much time on the UNIX and Emacs sections, because you'll need plenty of time for the StructureBuilder and JDK sections.


UNIX

Do the following, to get some practice for some of the most useful UNIX commands:

  1. register comp212 will make a few changes to your account, including creating a COMP 212 subdirectory. You should keep all your COMP 212 work in this directory. In it, further create a labs/01 subdirectory, will will contain your work for for this tutorial.

  2. man is a program for viewing online manuals about UNIX commands and features. xman is a GUI version of the same program.

    For example, use man ls to read about a file's permissions, and answer the following questions: If you own a file with permissions -rw-r--r--, can your friends read it? Can they write to it? Who can get to your comp212 directory?

    Read about the flags (such as -l, -A, -F, -t) for using the features of ls. What are the permissions for your newly-created personal ~/comp212/ directory?

  3. chmod changes the permission modes of files. Use it to make your COMP 212 directory accessible by yourself only.

  4. alias makes abbreviations for common commands. How can you use it to make ll (or something else of your choice) be shorthand for ls -FlAt (or whatever is your favorite combination of flags).

  5. cp copies files. Copy this file, index.html in the directory ~comp212/public_html/01-spring/labs/01/ to your lab directory.

  6. ln creates "links" to files. Make a "symbolic" link (ln -s) to this lab directory in your own lab directory. What's the main reason for using (symbolic) links? What's an annoying result of using symbolic links together with cd?

  7. rm removes files. Remove the symbolic link and index.html to get them out of the way for the rest of this tutorial.

  8. chsh will change your "shell", i.e., the program in which you enter UNIX commands. By Owlnet default, you use csh, the C shell. We recommend that you use a shell with more features, either tcsh or bash. The latter may be most familiar to Linux users.

    To use chsh, you need the full path of the new shell. Look at the text file /etc/shells for the full paths of all the available shells. Alternatively, use which tcsh to try to find the path for tcsh (or any other UNIX program).

    Go ahead and use chsh. This is a one-time thing -- it will change your shell for all future logins, or at least until the next chsh.

    In your new shell, what key do you press to repeat previous commands? In your new shell, what key do you press for filename completion?

  9. An unfortunate fact of life is that programs hang or crash. In Unix, you can close programs that hang, without having to reboot, using kill. (This is the equivalent to the Close Window Dialog in Windows.) To use this very powerful command, the computer needs to know exactly what it is you want dead. In UNIX, any running program has a PID (process identification) number. You can find out PIDs using the program top or ps. Once you have the PID of the program you want stopped, you just type kill PID with the appopriate PID.

  10. Read about I/O "redirection" and "piping" in the "About Unix Commands" chapter of Owlnet's UNIX handout or in the online manual for your shell (e.g., man tcsh).

    Use > to send the output of cal 1752 to a file. (See man cal to see why the year 1752's calendar is strange).

  11. Do you know how to copy and paste in UNIX? Well, in case you don't, try highlighting some text by using the left mouse button. What you just did is the same as the Copy function in Windoze. Now, go to where you want to paste the text, and click the middle mouse button. It should automatically paste what was highlighted.

If much of this material is new to you, don't worry. This tutorial covers much more than you need to know for the course. Of course, if you don't understand something, it is up to you to come to us for help. For assistance, you can


Emacs

Emacs is an editor program. Actually, that's an understatement in a couple ways. First, it is pretty much the UNIX editor program. (The default Emacs on Owlnet is GNU Emacs, but there is a competing XEmacs.) Sure, there are others, but not many people use them. Second, it is so much more than just an editor. On some systems Emacs' icon was a kitchen sink, and on old systems a running joke was that its name meant "Emacs Makes A Computer Slow". It is an amazingly configurable program with lots of options. E.g., you can read email and newsgroup postings and more using just emacs. This part of the tutorial will just cover a few basic things. If you want to learn more, check out the Owlnet handout on Emacs.

  1. Now, the most confusing thing for emacs newbies is understanding the notation used for commands. If you check out the Emacs reference sheet, you will see commands like

    What this notation means is, respectively,

    Some other good keyboard commands include:

  2. Another cool thing is that for commands, there is also tab-completion. Try opening a file using C-x C-f. If you just type the first couple letters of a file you want and press tab, emacs will try to complete the name of your file. This is good because as computer scientists, we are lazy.

  3. Like any decent editor, Emacs periodically auto-saves your work. If you notice some files that show up in your directory starting with a #, it's an Emacs auto-save file. In the event of a crash or any other situation where you were forced to exit Emacs without saving your work, you can recover the last auto-saved version via M-x recover-file.

    There are a tremendous number of commands that can follow M-x. If you forget exactly the one you want, don't forget the tab completion to help you figure out which one you're looking for. Or you can try M-x apropos to search for appropriate commands.

  4. You can also change the type and size of fonts and much more. Try holding down the Ctrl key and pressing the left mouse button while Emacs has the mouse focus. Now, try holding down Shift and the left button. I'll leave it to you to play around with it.


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.

pizza.png (7975 bytes)

  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.


JDK: Compiling and Running a Java program

In an xterm, go to your directory containing the Java program you just created.

Compiling from the command line

First compile the file PizzaClient.java with the command

     javac PizzaClient.java
Then run the program with the command
     java PizzaClient
You'll see an output that says something about which pizza is a better deal.

Compiling from inside Emacs

There is another more convenient way to run the edit-compile-debug cycle while using Emacs.

Load the file PizzaClient.java into an Emacs buffer by the command:

     C-x C-f PizzaClient.java
Then compile from with Emacs with
     M-x compile
or, equivalently,
     ESC c

In the minibuffer, Emacs will suggest to you the compile command to use. This should be javac PizzaClient.java, as above. Simply press Return after this. The compilation will begin and the details will be shown in a special Emacs buffer called *compilation*. If there are any errors, they will appear in this buffer. As there are no errors, the compilation should finish successfully and you should see a message like:

Compilation finished at .....
To run the program, now go to the Unix prompt and as before,
     java PizzaClient
and you'll again see the same output as before.

Warning: if you rename a file, ESC c may suggest the wrong file name. In that case you can edit the file name on the command line before using Return.