Download Using the DrJava IDE

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS101
Using DrJava in CS101
Fall 2014/2015
This lab assignment will familiarize you with the process of creating Java programs using DrJava.
To use JCreator instead, please see the “Using JCreator for CS101 Assignments” document.
Be sure to follow the instructions in the “Install and Setup DrJava for CS101 Assignments” document before continuing.
Having designed a program, you must type the source code into a file using an editor, then compile and run it. If the compiler
reports (syntax) errors you must return to the editor to find and correct them. Once the program compiles correctly you can run it.
If it doesn’t work as expected (i.e it has logical errors), you must again return to the editor to correct it.
Using an integrated development environment (IDE) —a program that combines facilities for creating, editing, compiling and
testing programs—helps make this process quicker and more efficient. There are many IDE’s available, but this assignment
concentrates on using one called DrJava. Later, you may like to look at some of the others, for example, NetBeans, Eclipse,
JBuilder, BlueJ, JCreator, etc. You should also look at how to use Java without an IDE –see the optional assignment on the CS101
“Bits ’n Pieces” web page. For now, carefully read and follow the steps below in sequence…
Organizing your work with DrJava

Java programs often consist of multiple files, including lots of source code files, images and other resources, test files, and, of
course, the executable class files themselves. Most IDE’s allow you to collect all these related files together into what they
call a “project”. Even though DrJava doesn’t force you to use projects, doing so will make your life easier, especially when
you have many programs to work on. In DrJava, a project is essentially just a folder (with a .drjava file in it, as we will see).

On your H: drive you should already have a \private\cs101 folder containing sub-folders for lab02, lab03, lab04, lab05, lab06,
lab07, lab08, lab09, lab10, lab11 & lab12. If not, please create them now.

CS101 lab assignments will generally ask you to write several independent programs, each of which you should place in a
separate project –in other words, you should place each one in an appropriately named folder within that assignment’s subfolder, for example, lab02a, lab02b, lab02c, etc. within the lab02 folder. You can create such project folders from inside
DrJava while saving a file, or from outside it before starting your work.

One final suggestion, save this cs101_template.txt file to your private\cs101 folder on h: If you open it with a text editor you
will see it has several bits of Java code. You will use these as the starting point for each program you do this semester.

Run DrJava (how you do this depends on your O.S. and how DrJava was installed).
Your First Java Program

By default, when DrJava opens it displays a new “[Untitled]” file into which you can directly type your program. Rather than
starting from scratch, simply copy-paste the first code template from the cs101_templates.txt file.

Save your program as Lab02a.java in a new project folder called lab02a, i.e. save it in H:\private\cs101\lab02\lab02a\ Note
that the filename now appears in the list on the left (and the full pathname is in the window’s title!)

Try to compile your program by clicking the “Compile” button. Undoubtedly DrJava will signal an error. Read the error
message and then correct the Java code –hint: change the Java class name from “XXXX” to “Lab02a”. Save your program
again and recompile it. Hopefully it should be ok this time.

Take a look at the program and see if you can predict what it will do. Click the “Run” button to see if you were right.

Congratulations, you have now completed your first Java program (with a little help!)

Before moving on, complete the project creation process by selecting Project|New, navigating to the lab02a folder and
entering the project name as “lab02a” (DrJava will automatically add the .drjava file type extension to the filename). A
project properties dialog will appear. For now, simply accept the defaults by clicking OK.

Note the left panel now has the project name at the top and under it a line saying “[Source Files]”. Under this is the name of
your Java program, in this case “Lab02a.java”. If you click the “New” button now, an empty “[Untitled]” file is created, but
this time in a separate section called “[External Files]”. Type something into the new file –e.g. a few random letters-- and
then “Save” it to the same “lab02a” folder as your Lab02a.java file, but with the name “junk.java”. Note how it is now
included in the “[Source Files]” section along with your other file. Clicking on a filename allows you to see and edit it.

Exit DrJava (saving everything as necessary), then take a look at the files in the lab02a folder. You should see the two .java
source files, a Lab02a.class file (the compiled –bytecode- version of your Lab02a.java file) and the lab02a.drjava project file.
You may also see some .java~ files (these contain the previously saved version of your source code… just in case!)
Applets Anyone?

Java programs actually come in several flavours including: simple text-based console applications (like the one you just
created), applets (restricted graphics-based programs designed to run in a web page) and standalone graphics-based
applications (like normal Windows programs).

Run DrJava again. This time select “Project” and then lab02a.drjava from the list of recent projects. Everything should be
back exactly the way you left it. Note: if you have installed DrJava to your O.S. you may be able to directly open it from the
.drjava project file.

To “delete” your junk.java file, click on it, then click the “Close” button. Finally, click “Yes” to remove it from the project.
Note: the file is actually still in the folder on your disk, it is just no longer considered part of the project; you can delete it
from your O.S.

Your next project will be lab02b. Select “Project|New” to create the new project (saving the previous one first, if prompted to
do so). Remember to create a new folder, lab02b, inside your lab02 folder, then save the lab02b.drjava project file inside it.

This time, copy-paste the Applet template from your cs101_templates.txt file. Change the class name from “XXXX” to
“Lab02b” and save the file as “Lab02b.java” (again, inside the lab02b project folder).

Compile and run your program as before. Notice that this time a new window appears containing the message “Hello…”
inside a rectangle. This is Java’s applet_viewer. The resulting Lab02b.class file can be embedded in an html webpage and
viewed in a web browser (assuming the appropriate security permissions have been granted).

Go back to your first project, lab02a, by selecting it from the Project menu again.
Correcting Syntax Errors

Select Lab02a.java and, in the edit window, remove the semicolon from the end of the two lines that begin with the
“System.out.” then save and again compile your program. This time it will not compile correctly because of the syntax errors
you just introduced. The “Compiler Output” window at the bottom of the screen gives details of each of the errors, including
the line number on which they were discovered. You should see two messages that say “Error: ';' expected”. Error messages
are not always as clear as this one, so you sometimes have to be a bit of a detective to find the real source of the problem.

To correct a syntax error you must return to the editor window and locate the problem. When a particular error message is
selected in the Compiler Output window, DrJava highlights the corresponding line in the editor and puts the cursor there
ready to start typing. Go ahead and correct the problems, then try recompiling again. Try introducing other errors and see
what messages you get (e.g. try removing one of the quotation marks, and changing println to Println, and main to man.) Just
for fun, see if you can modify the program so that it outputs a personalized greeting message.
Online Help

Select “Help” from the menu to learn more about using DrJava. (Shortcut key F1)

You can also get help on the Java language and packages themselves by selecting Tools|Javadoc|OpenJavaAPIjavadoc. This
pops up a dialog into which you can type the name of the class you are interested in. Pressing OK then opens the relevant
documentation in the default browser. (Shortcut key Shift-F6)

When the cursor is on a class name in the editor you can select Tools|Javadoc|OpenJavaAPIjavadocForWordUnderCursor to
directly obtain help for that class. (Shortcut key Ctrl-F6)
Some Points to Note

The bytecode (.class) files are currently stored together with the source code (.java) files. It is usually better to put them in a
separate folder that can be easily distributed without the source code. To do this select “Project|properties” and set the “Build
directory” to a folder called “classes” within your project folder, e.g. H:\private\cs101\lab02\lab02a\classes. You might also
set the “Main Class” to the name of your project, i.e. “Lab02a”. Press OK, then recompile your program. Examine the
contents of the folder to see that all the .class files are now in the “classes” sub-directory. Note: delete any .class files not in
this folder, they are no longer used. Try “Project|CleanBuildDirectory”, and then examine the folder’s contents. Recompile
your program and look again.

It is always advisable to keep a backup copy of your work on another disk, just in case! To do this, simply copy the entire
folder containing your assignments/projects to another location, for instance, your USB flash drive.

Java ignores line breaks (except in certain places, such as in identifiers, string literals, and single line comments) and allows
additional white space (space and tab characters) to be used as desired. Blank lines can thus be used to separate logical
sections and sub-sections can be indented progressively to the right, making it easier to understand the structure of your code.
DrJava helps you keep things straight when entering your program code, by automatically indenting a new line by the same
amount as the preceding one. When you type an opening curly bracket (brace), the editor automatically increases the level of
indentation. Pressing Enter and then the closing curly bracket decreases the indentation and puts the brace in the correct place
(as required by CS101/2 Style Guidelines) so you have no excuse for badly laid out, and hence difficult to read, programs!

Finally, don’t forget to complete the header comment in every program you write. In particular, make sure to add your name,
the date, and a brief description of the program. Hint: Modify your template file to include your name.
And Finally…
After completing the preceding steps, demonstrate your new ability --to create, debug and run a Java program-- to the TA.
Congratulations, you are now well on your way to becoming a professional programmer, only another four years and a few
hundred thousand lines of code to go.
Have fun!