Lab: Getting Started with Eclipse
Exercise 1: Creating a Program Using Eclipse
An object-oriented program is, as we learned in class, made up of a collection
of classes. Each class is stored in a separate file, where the name of
the file is the name of the class with a .java extension.
Thus, a class called Aquarium must be in the file Aquarium.java.
We might have many classes in a program, and we might have many programs.
How do we keep track of which classes are part of which programs?
The Eclipse development environment uses the idea of a project to
keep track of information about a program, such as which classes are part of
the program, where the program's starting point is, and so on. To run
or modify a program using Eclipse, the first thing to do is to start up Eclipse
and then create a new project or open an existing project to contain and keep
track of our classes.
Let's start by creating a very simple program with a single class. To keep
things simple, the only method we will create is a main method.
Exercise 2: Importing an Existing Project Using Eclipse
For this part of the lab, you will copy and run an existing
project.
Go to the web page for the upcoming GridIterator Lab. Download the zip file containing the project code, and unzip the folder someplace on your M: drive.
- Start up Eclipse if it isn't already running.
- Select "Import" in the Eclipse "File" menu.
- In
the window that appears, choose "Existing Project into Workspace" and then
click on the "Next>" button.
- Click on the "Browse" button and navigate to the "GridIterator"
folder in your directory on the campus server.
- You should see a folder labeled "GridIterator" appear in
the "Package
Explorer" pane on the left side of the Eclipse window.
- Run the application by right clicking the GridIterator folder and selecting "Run-As->Java Application".
Exercise 3: Refactoring and the Edit-Compile-Run Cycle
Now let's return to a project you were working on before, to add new
functionality to it. You will do this frequently in this course,
since enhancing an existing program (editing, compiling, and testing
existing files) is
much more common than creating a completely new one.
In fact, since it is almost impossible to
sit down and write a program absolutely correctly from scratch,
even new programs are often created by modifying previous programs.
- In the Package Explorer pane, select the project you created in Exercise 1 above. If you can't see
the class you created indented below the project, click on the plus sign next
to the project folder. (You might need to click on a plus sign next
to the "(default package)" icon also.) Click (or double-click,
if necessary) the class you created. You should see it appear in the
central editing pane.
- Now let's edit and personalize the program. Scroll down to the System.out.println
statement you added earlier. Replace the string between the double
quotes to make the program print out "Hello, your
name" instead. Run it again. Did you get what you expected?
If not, fix it!
- The name you chose earlier for your class is probably not very descriptive. Change
its name
to a more descriptive name for this class. The name should start with
a capital letter, since this is the convention for class names in Java. Eclipse
provides an easy way to change the class's name in a consistent way.
- Select the class in the Package Explorer pane.
- In the Refactor menu, choose "Rename."
- In the dialog box
that appears, enter the new name. Mark the
checkbox so that any references to the class name in your
code or in comments will be changed as well.
- Click on the "Preview" button. If your
class contains a "main" method, you may get a warning about other
classes that might use this one. You can just click on "Continue"
to proceed. You will get another pop-up window informing you of
what changes are to be performed. Click on "OK".
Note that
the name of the class in the Package Explorer pane changed. Scroll through
the class to make sure that other necessary changes were made also. Update
the class description in the "javadoc" comments.
- You can also give the project a more meaningful name. Select the project
name in the Package Explorer pane and then choose Rename in the Refactor
menu. After doing this, you may need to click on the plus sign next
to the project name to open it up and see its contents.
- You should also change the name of the directory containing your project,
to make it easier to find next time. Select your project in the
Package Explorer
pane and then choose "Move" in the Refactor menu. In the
dialog box that appears, change the name of the directory (the last segment
of the Location name) to the same name as your project. Click on the
"OK" button.
Exercise 4: Creating a New Project from Existing Source + Using JavaDocs
Download and unzip the file WordAnimation.zip to your M: drive.
Create a new project as you did in Exercise 1 above. - Expand your new project in the Package Explorer, and right click on the "src" icon. Select "Import..."
- Expand the "General" folder and select "File System". Press "Next".
- Browse to the folder where you created when you unzipped the file above.
- Check the boxes associated with the .java files and click OK.
- Right
click the project, and follow the same procedure to import the file
"alice.txt". Since that is not a Java file, you don't want to
import it into the "src" folder.
- Run the project.
- If you have time, modify the project so that every word is assigned a random color: (You will need to make use of the Java 6 API)
- Add an instance field to the MovingWord class to store a color (of type java.awt.Color)
- Modify
the MovingWord constructor so that it selects a random color ( You will
need to create an object of type java.util.Random, and use its
getNextInt method.)
- Modify the Draw method of the MovingWord
class so that it uses the appropriate color. (You may want to
look at the documentation for the java.awt.Graphics2D class.)