This lab introduces the DrJava programming environment. You will use DrJava to develop and test programs for both lab and homework assignments.
$ mkdir nhLab
lab2. This subdirectory will contain several files with an extension .java. These are text files containing program segments written in Java. They are called source files.DrJava is an easy to use software development environment for writing Java programs. It was created by the Computer Science Department at Rice University, and is currently under active development by the JavaPLT group at Rice. DrJava is designed primarily for students, and provides an interactive facility for developing Java code. It includes an editor and debugger, and incorporates the JUnit testing framework and javadoc documentation generating tool.
DrJava is discussed in Appendix II of the text. You can download DrJava for use on your home system from http://drjava.sourceforge.net/ or http://drjava.org.
drJava at the prompt: $ drJavaBe sure to key upper and lower case as shown. After a few moments a window similar to that shown below will appear.
DrJava's main window is divided into three panels. The largest area to the right is the definitions pane. You write and edit class definitions in the definitions pane. The open document list displays a list of open documents. You can view and edit a particular document by selecting it from the list. When you click on a document in the list, it will be displayed in the definitions pane. The bottom panel contains several tabbed panes. Clicking the tab displays the pane. For the present, we will be concerned with the interactions pane and the compiler output pane. You can enter and evaluate java statements and expressions in the interactions pane. Compilation results are displayed in the compiler output pane.
Click each of the Categories to get an idea of the options that can be set.
Since both DrJava and Java are being actively developed, the preference options available may not be exactly as described here. Furthermore, some of the options will have been set for you already. Your lab instructor will tell you what you need to do to properly configure DrJava. DrJava will remember your option settings. Options do not need to be set every time you run DrJava.
nhUtilities) should be listed in the Extra Classpath box. It will be something like /apps/java. Your home directory should also be listed. If you do not see this, ask your lab instructor.
-tag require -tag ensureIf it does not, check with your lab instructor.
> int i;
Be sure that you are keying next to the last "> " prompt in the window. Key the line exactly as shown, including the semicolon at the end. Press the "Return" or "Enter" key at the end of each line.
This creates an int variable named i in the DrJava environment:
> i = 17; 
The equals sign "=" doesn't mean "equals." It means "store the value given on the right into the variable named on the left." We can see the value stored in the variable by keying the name of the variable:
> i 17Note there is no semicolon at the end of the line. (We show DrJava's response in slanted font.)
> i = 18; 
> i 18 > i+1 19 > i*2 36
Keying the expression i+1 gives us the value stored in i plus 1. What does i*2 produce?
float variable and give it a value: > float f; > f = 17000000; > f 1.7E7
The expression 1.7E7 is a way of saying 1.7×107: that is, seventeen million.
> f+1and note the result. Keying
> f == f+1
says "f is equal to f+1." Can you explain the answer? What result do you get when you key
> i = 17000000; > i == i+1f into i? > i = f;
There are several things you should notice. First, the text is displayed in several different colors. Each color denotes a different kind of "lexical component." For instance, words like "public" and "class" are key words. Key words are displayed in a unique color. The color coding will help you recognize keying mistakes when you key a program.
Second, the text is indented in a uniform way. This indention reflects the structure of the program and makes it easier to read. When you key in a program, DrJava automatically indents for you. If the indention is not what you expect, it is often a sign that you have made a syntactic mistake.
* Create a new Counter..." and enter a few spaces. Do the same with the next two or three lines.
Finally, DrJava will show matching parentheses, braces, and brackets by highlighting. Again, this will help you find and avoid mistakes when you key a program.
}". Note that the code up to and including the matching open brace is highlighted. Do the same with a closing parenthesis, ")"
You will notice that each line in the display pane is now numbered. Error messages often mention a line number. You can also navigate to line by number by choosing the Go To Line... option from the Edit pull-down menu, or by keying Control-G. (The line numbers do not need to be displayed to do this.)
You can also navigate the display pane by choosing Find/Replace from the Edit pull-down, or by keying Control-F.
You will notice that the document name "Counter" is followed by an asterisk "*" in the open document list. This means that the document has been modified in the definitions pane but the changes have not been saved to disk. (You modified the document when you added and removes the spaces.)
Next, you will compile the class definition: that is, invoke a program called a compiler that will translate the source into "machine level" code that can be interpreted by the Java interpreted. (The interpreter is called the "Java virtual machine.") The compilation process will produce a new file named Counter.class in your lab2 directory.
Something is wrong. Notice that the Compiler Output tabbed pane at the bottom of the window is selected, and it indicates that five errors were found. The first error is highlighted, as well as the line in the definitions pane that caused the error.
If you read the error messages and look at the highlighted lines, you will notice that they all have something to do with the symbol "count". The problem is that we have forgotten to define this variable. (Don't be concerned if you don't understand this yet. This exercise is just to get you familiar with using DrJava!)
public class..."
private int count;followed by Return/Enter to insert another blank line. Note that DrJava automatically indents what you have keyed.
The beginning of the document should now look like this:
/** * A simple integer counter. */ public class Counter { private int count; /** * Create a new Counter, with the count initialized to 0. */ ...
As mentioned above, Javadoc produces a collection of HTML documents. These are usually viewed with a browser.
JUnit is a framework we will use to test our classes. Unfortunately, as this is written DrJava does not yet support the latest version, JUnit 4. The Test button on the tool bar will run an earlier version which we will not use.
You can obtain the latest version of JUnit for your personal system from http://www.junit.org.
> java org.junit.runner.JUnitCore nhLab.lab2.CounterTest FAILURES!!! Tests run: 3, Failures: 2 There were 2 failures: 1) testInitialState(nhLab.lab2.CounterTest) java.lang.AssertionError: expected:<1> but was:<0>The problem is a Counter should start at 0, but it starts at 1 as written.
/** * Create a new Counter, with the count initialized to 0. */ public Counter () { count = 1; }
Change the "1" to "0" in the line "count = 1". Save and Compile.
OK (3 tests)in the Console pane. (You can clear the Console pane by choosing Clear Console from the Tools pull down menu, or by right-clicking in the Console pane.)
You might want to spend a few minutes experimenting with some of the options available in the Edit and Tools pull-down menus.
If directed by your instructor, turn in output from the browser, and listings of the modified Counter class and the interactions history for the session. Be sure everything you turn in is clearly labeled with your name and section number.
You can print the interactions history as follows.
$ lp ~/nhLab/lab2/temp.history