Homework #2 CMSC 131
Due February 11th, 2003
(was Feb 10th)
Object-Oriented Programming I
  Spring 2004

 

Integrated Development Environments - IDEs

This assignment is intended to introduce you to the Eclipse IDE and use it to modify JavaEyes.  As with homework #1, you must also submit a time log.  This homework will be graded as follows:

The Manual of Dexterous Compilation

Prologue

A learned software consultant was once approached by a bedraggled pilgrim who had not showered in many days.

"O wise one, I am troubled. I must continually master new languages, operating systems, and applications in order to remain competitive, and I must remember so many commands and keystrokes and directories where everything is supposed to be kept, that I cannot write a line of code without devoting months of effort and frustration. Yet you have mastered all these with ease, and even find spare time to go to the beach and see movies. How may I discover this programmer-nature, that I as well may become sought after in business and academia and still have a social life?"

The consultant responded by lifting his index finger.

The pilgrim went away, perplexed.

A week later, the pilgrim returned with bloodshot eyes, wearing black wrist guards.

"O wise one, woe is me! For I have reflected much upon your answer, and concluded that the only answer was to become a touch typist so that I could write code faster. I mastered the Dvorak keyboard and can now type at 80 words per minute, but now I have Repetitive Strain Injury and must wear these wrist guards and the pain is most bothersome! Clearly you must be mistaken when you lift your finger."

The consultant responded by depressing his index finger.

The pilgrim went away, more confused than ever.

The consultant was not surprised to find the pilgrim returning a full two weeks after that, this time pale and near death.

"O wise one, I believe that you do not know the answer. For your depressed finger suggests a bowed head, so I have pursued the ascetic life by fasting, meditating, and practicing yoga. I am much more spiritually complete, but I still find it overwhelming to sit down and write a line of code. Clarity of thought and purity of the soul is not the answer; you have misled me. I believe I know what this programmer-nature is: it is merely the ability to speed-read computer manuals together with a photographic memory. It is the only explanation for your success."

The consultant was silent for a moment, and then responded by lifting, and then depressing, his index finger.

At this the pilgrim was enlightened.


In this document and in other handouts, we will use the following convention to describe the process of wading through menus: Menu Name > First Menu Selection > ... > Last Menu Selection.

So What is an IDE, Really?

An IDE is just a tool which makes programming easier. But what is wrong with the mechanism we used last homework to edit, compile and run programs? That approach is fine for simple programs, but what if we wanted to create a large application? This would be a daunting task, as a text editor is not a very good interface for managing many files, being simultaneously edited by many software developers. How might we manage these files in a way that is consistent and easy to understand? Also, we might want help debugging the program, and special support for modifying the program based on the underlying semantics of the program.

Enter the IDE. A good IDE combines a text editor, compiler, and debugger into one package. It usually provides sophisticated project management utilities to help you organize your files. It is also common for an IDE to come with 3rd party version-control software. An IDE often provides specialized editors for editing user interfaces, icons, and other graphical components (although Eclipse does not).

If you still don't understand the advantages of an IDE, that's OK. You'll appreciate them more after you gain in programming experience. For now, just accept that IDEs are useful.

The best way to learn an IDE is by using it, so let's try building a small project in Eclipse. We have implemented JavaEyes in Java, and we will attempt to edit, compile, and run it from within Eclipse.

Accessing Eclipse

Let's get started. In this class, we are going to use Eclipse with the Dr. Java plug-in.  They are both open source, and freely available on all commonly available platforms (including Windows, Mac OS X, and Unix). To get Eclipse running, follow these instructions:

On Windows, Eclipse will create a 'workspace' directory that will contain all of your Java files.  If you ran Eclipse directly from the directory it is installed in, the workspace directory will be within the Eclipse directory.  If you ran it from a desktop shortcut, the 'workspace' directory will be in the directory you configured the shortcut to start in.  On Solaris and Macintosh, the projects get created in the directory specified by the '-data' option on the eclipse command line.

If you've done this correctly, you will be rewarded with a purplish splash screen as Eclipse starts, and then a large window showing Eclipse.  First, we want to open the Dr Java plug-in.  Do this by selecting Window > Open Perspective > Other.  Select "Dr Java" from the resulting dialog window.  On the left edge of Eclipse, you'll see an icon with a big white J on a black background.  Click on it, and you'll enter Dr Java mode (Eclipse calls different view modes 'perspectives').

The first thing we're going to do is create a new project:

You've now set up and configured your first Java project with Eclipse. Let's see what this project can do.

Compiling and Running a Project

With Eclipse, you do not have to manually compile your project.  Instead, Eclipse automatically compiles your code every time you save your files.  However, you can force it to recompile your project with the Project > Rebuild Project menu.

Now you can run the program.  Do so with the Run > Run As > Java Application menu.  You should see the JavaEyes program running in a new window. Move your mouse around in the window and watch the eyes follow it. When you're done playing with JavaEyes, close the window with the regular window buttons to close the application. You can run the program again with the Run > Run Last Launched menu. Or, more simply, click the little person running icon:

Editing a Project

We'd also like to be able to use our IDE to modify Java programs. Let's trying making some changes to JavaEyes and see what happens.  Look at the JavaEyes.java window in Eclipse.  Note that there are tabs along the top of the editor, and you can switch which file you are editing by clicking on the tabs.

Important! Even though juicy bits of code are displayed in your code editing window, it is not the focus of this exercise. Do not feel compelled to understand it at this point. It is meant to be an example which can help familiarize you with the tools that we will be using throughout this course.

Notice how different bits of code are colored differently. This is meant to aid the programmer in visualizing the syntax of Java statements. You'll experiment with Java expressions and statements in next week's homework.

If you've used other windows-based editors or word processors, a lot of the editing options available to you will be familiar. For instance, copying, cutting, and pasting are all available from the Edit menu. Search and replace options are available from the Search menu. We recommend turning on an option to display line numbers.  This can be useful since Java errors are reported by line number.  Do this through Window > Preferences.  Then click on the Java and then Editor nodes within the tree on the left.  In the Appearance tab, check "Show line numbers".

Now we'd like you to make some modifications to the code. The purpose of this part of the project is just to get you comfortable using the tools, not to experiment with Java expressions and statements (although you are welcome to).

The first modification we'll make is to change the color of the JavaEyes application. Look for the line that says:

private static final Color PUPIL_COLOR = Color.black;

This line sets the pupil color of the JavaEyes application. Change that line so that the background color will be blue, and then run the program again.  Don't forget to save the file before you run it.  Do that with File > Save.

private static final Color PUPIL_COLOR = Color.blue;

Some of you may notice that not all of the colors that you're familiar with will work. Some will produce errors when you try to compile. For instance, try setting the pupil color to "sepia". The program will not compile.  When you save the file, you'll see that the words "Color.sepia" get a red squiggly line underneath them.  In addition, there will be a small red X on the left margin of the editor by that line.  Finally, the editor tab showing JavaEyes.java and the JavaEyes.java entry in the project tree on the left will also have red Xs.  These are all indications to you that there is a problem.  If you mouse over the bit of code with the problem and hold the pointer still for a second, a tool tip will tell you what the problem is.  In this case, it will say that Color.sepia cannot be resolved.  I.e., that the compiler has never heard of that color.  You can also see the list of all the problems (in case there are others) by clicking on the "Problems" tab at the bottom of the Eclipse window (this tab will show up the first time there is a problem).  You can then double-click on each problem to go directly to the problem point within the code.

How can we find out what colors are available?  We need to examine the documentation for the Color class. This can be found here at (all the Java API documentation is available here - but we'll look at this in more detail another time). Scroll down to the Field Summary section of the document. This enumerates the list of available fields in the Color class. You are only allowed to use the colors specified here -- Java simply doesn't know about any other ones. Of course, you can create brand new colors if you wish, but these ones are the only ones that are predefined for you. If you haven't successfully changed the color yet, try using one of the colors specified in the interface.

Now try changing the physics of the eyes.  Use the Search > Search command to search for "physics".  But click on the "File Search" tab within the search window before you try using it.  Notice this puts little yellow arrows in the left margin of the editor at every line that contains the search terms.  In addition, the bottom window will show the search results.  You can double-click on the search results to highlight the results within the code window.  Then you can click the up or down arrows at the right side of the search window to cycle through the search results. Look for the line:

int physics = ATTRACTION;

Change the word ATTRACTION to REPULSION, save the file and re-run it.  What happens?

Lastly, you're going to comment out a portion of the code, in order to see what kinds of compiling errors you can get. Commenting means to mark off an area in your code file that will be deliberately ignored by the compiler. In Java, you use the following scheme to comment your code:

/* This is a comment. */
// This is also a comment.
/* This
* is
* also
* a comment.
*/ 
// This
is
not
a 
comment (and will produce an error)

Anything found within the delimiters /* and */ is considered a comment. But be warned that you are not allowed to "nest" comments. In other words, /* /* */ */ would not be a legal comment. The compiler would consider /* /* */ a comment, and it wouldn't know what to do with the final */.

Any text which comes after // on the same line is also considered a comment. Text which comes after // on a subsequent line is no longer considered commented (unless of course, it has been commented somewhere else).

Let's find the line of code that starts with addMouseMotionListener, so you can comment it out by inserting // at the beginning of the line.  Here's one new really fast way to find things called "incremental search" that is useful when you know what you are looking for and are sure you are already looking at the file that contains it.  Click anywhere in the JavaEyes.java source code and press "Ctrl-J" (or similar on a Macintosh).  Then, start typing the text you are looking for.  As you type, the first bit of code that matches what you type will be scrolled to and highlighted.  You can press Ctrl-J again to cycle through multiple occurrences of that item.

Now comment out the addMouseMotionListener line and run the program.  What happens?  Uncomment it the line and run again (don't forget to save before you run).  What happens if you do forget to save your program before running it?  Try it and find out.

Interactions Pane

The last major feature of Eclipse that we will cover here is the Interactions Pane.  This is actually part of the Dr Java plug-in, and is the primary reason we are using Dr. Java.  This pane allows you to interactively enter Java code without having to create a new source code file, saving it, compiling it and running a program.  Instead, you can play around with things much more dynamically.

Activate the Interactions Pane by clicking on the Interactions Pane tab.  Enter the following text:

JavaEyes eyes = new JavaEyes();

That creates an instance of the eyes (if you don't see them, the eyes window might be hidden behind Eclipse, so look around for it).  But more interestingly, it also gives you access to the JavaEyes object through the 'eyes' variable.  Let's try manipulating the eyes.  Enter this in the Interactions Pane:

eyes.setPupilRadius(5);

You may have to bring the JavaEyes window back to the front to see the result.  What happened?  Try setting the radius to other values.  Then try calling some of these methods, and try different parameter values.

eyes.setPhysics(2);                  // Valid parameters are 1 and 2
eyes.setEyeRadius(30);
eyes.setEyeSpace(5);
eyes.setPupilColor(java.awt.Color.pink);
new JavaEyes();

Note: If you accidentally close the Interactions Pane, you can get it back through Window > Show View > Other, and then select Dr Java/Interactions Pane from the resulting dialog window.

What To Turn In

It is really important that you go through this entire homework and play around.  We can't force you do it, but we can promise you that the effort you spend now learning these tools will enable you to work productively with all the fun projects in the coming weeks.  However, you can demonstrate to us that you did at least some of this.  So, please do the following:

Getting Help

One of the things we hope that you will take away from this class is that you will be a more productive (and happy!) programmer if you learn to work in groups. If you have a question, ask your classmates. We feel this helps both of you: it clears up your question, and reinforces another student's understanding of the material. The course assistants are also a good resource, and you should feel completely at ease with asking them for help. That's what they are there for, and you want them to feel needed, don't you?

Of course, there are also computerized resources that may be useful to you. If you have questions about Eclipse, you can find an online manual at Help > Help Contents. The class website also has some links to useful Java resources.


Epilogue

Many months later, the software consultant was again approached by the pilgrim. This time, however, the pilgrim looked vibrant and healthy, and was dressed in the finest of clothes.

"O wise one, I did as you suggested and switched to using Eclipse, and my productivity increased tenfold. I have received promotion after promotion, and am now a very wealthy man. My future as a software developer is at last secure."

The consultant smiled sadly, and for the first time, he spoke:

"Young one, did you remember to protect your program against viruses?"


This homework is derived from Lynn Stein's www.cs101.org project 0.