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.
Do the following, to get some practice for some of the most useful UNIX commands:
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.
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?
chmod changes the permission modes of files. Use it to make your COMP 212 directory accessible by yourself only.
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).
cp copies files. Copy this file, index.html in the directory ~comp212/public_html/01-spring/labs/01/ to your lab directory.
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?
rm removes files. Remove the symbolic link and index.html to get them out of the way for the rest of this tutorial.
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?
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.
Use > to send the output of cal 1752 to a file. (See man cal to see why the year 1752's calendar is strange).
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 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.
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
Some other good keyboard commands include:
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.
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.
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 (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.
First, you need to initialize your account to use SB by running
/usr/site/sb-4.0/propagate.shYou won't need to ever run this again.
Now, start SB with
/usr/site/sb-4.0/StructureBuilderThere 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 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.
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 a class called AShape
.
void newMethod
.
Change this to double getArea.
Check the "abstract" and "public" check boxes.
AShape
should
be displayed in italic font.
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.
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
.
get_price returning that field.
- Add a method,
getArea
returning a double.
For consistency, rename get_price
to
getPrice
.
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.
Constructors are special pieces of code used to initialize an instance of a class when it comes into existence.
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
.
Circle
constructor
with parameter double radius
.
Rectangle
constructor
with parameters double width
and
double height
.
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)
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.
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,
Pizza.java, AShape.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 the class handout for the code you need.
Remember that the AShape
getArea
method is abstract and so has no code body.
In an xterm, go to your directory containing the Java program you just created.
First compile the file PizzaClient.java
with the command
javac PizzaClient.javaThen run the program with the command
java PizzaClientYou'll see an output that says something about which pizza is a better deal.
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.javaThen compile from with Emacs with
M-x compileor, 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 PizzaClientand 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.