Eclipse
Introduction
In this lab, you will explore Eclipse. If you already use the Eclipse
editor, this lab may provide you with more information. If you are new
to Eclipse, make sure to watch the videos in the Resources section below
to get oriented to the vocabulary and layout of Eclipse.
All Eclipse applications are built as a project. If you choose to use
Eclipse this semester, each lab and each PA that you complete will
be in its own project and each project will have its own folder.
Resources
Dr. Piercy's
A Brief Tour of Eclipse (~9 min) covers basic vocabulary and explains
different areas in the Eclipse screen.
Norm Krumpe's
Eclipse Tutorial (~3 min) covers additional information you'll need
to complete this activity.
Part 1 - Hello World
- If you have not done so already, open Eclipse. At the "Workspace"
prompt, fill in a location for your workspace. Note this will be where your java
programs are stored.
- Choose the tutorials icon. (Pen and green check-mark).
- Choose "Create a Hello World application".
- Follow the steps in the tutorial. Be sure to see how the
automatic syntax checking works by introducing some bugs. Also explore
the autofill options (showing the list of available members of an
object).
Part 2 - Coding in Eclipse
- Create a new Eclipse project named
PointLab
:
File -> New -> Java Project
- Enter the Project Name - PointLab - then under Project Layout choose
the option to use project folder as root for sources and class files. This will
make zipping the files together to submit online easier.
- Click Finish and the project should show up on the left in the Package Explorer.
- Use Eclipse to create a new class that corresponds to the UML
diagram below. To create the class choose File -> New -> Class .
Use the Browse button to choose the project folder you just created above. Name
the class. The other default values should be just fine.
Don't worry about commenting the code just yet. We'll address
that later on.
The return value of the toString
method should
consist of an open-parenthesis followed by the x-coordinate, followed
by a comma, then a space, then the y-coordinate followed by a
close-parenthesis. For example: "(1.0, 2.0)"
.
The equals
method should return true if the two
points have the same x position and y position.
- Once you have completed a syntactically
correct
Point
class, move on to the next step.
Part 3 - Adding A .jar File
One standard mechanism for distributing Java code is through .jar
files. A .jar file is a compressed archive that can hold any number
of Java classes in either source or binary form (as .java files or as
.class files). For some projects in this course you will be provided
with .jar files containing support code.
-
Create a new folder named "lib" in your Java project. Download the
file PointCode.jar, into the newly-created folder.
If the file does not show up in the Package Explorer on the left, refresh the
diplay by selecting File -> Refresh and it should appear.
-
Right click
PointCode.jar
and select Build Path -> Add to Build
Path.
- There should now be a "Referenced Libraries" entry in your
package explorer. Expanding that entry should allow you to inspect
the details of the
PointCode.jar
archive. All classes
included in that archive are now available within your project. In
this case there is only one
class: PointDisplay. It should be under the
default package.
Part 4 - Importing and Running
- Download the
file PointDemo.java to your Desktop,
then drag it into the folder of your project where you stored your source code.
- Look over the
PointDemo
class to get a feel for what
it should do. Run the code to confirm that everything is
working as expected.
Part 5 - Checkstyle Eclipse Integration
Checkstyle is a development tool that makes it easy to test Java
code against a particular coding standard. Our Web-CAT submission
system will analyze your programming assignments using Checkstyle to
verify that they conform to the CS159 style guide. It will be in
your best interest to use Checkstyle off-line so that you can avoid
wasting your limited submissions. Checkstyle can be run from the
command line, but today we will try it out using an Eclipse plug-in.
- Navigate to the following web-page in a separate browser window or tab:
http://eclipse-cs.sourceforge.net.
Drag the blue "Install" button onto your Eclipse window and follow the
instructions for installing the plugin.
- Download the following XML file to your
Desktop: CS159s19_checkstyle.xml.
This file contains a Checkstyle configuration that matches the CS159
style guide. This is exactly the same configuration that AutoLab will
use to check your submissions.
- Configure the Checkstyle plugin to use the configuration
file: Eclipse
-> Preferences -> Checkstyle -> New.
Select "External Configuration" as the Type and then select the file
you downloaded in the previous step. Click "OK" and then set this
configuration as the default by selecting this new file and then clicking
Set as Default on the right.
-
Unfortunately, Checkstyle needs to be explicitly enabled for each new
Eclipse project. Right click your
PointLab
project in
the "Package Explorer" tab and then select Properties -> Checkstyle. Click on the
check-box labeled "Checkstyle active for this project".
-
Once you have completed the previous step, navigate to
your
Point.java
file, right-click in the editor window
and select Checkstyle
-> Check Code with Checkstyle. Many lines of
code should now be marked in yellow. Each of these marked lines
contains a violation of the style guidelines that would prevent you
from successfully submitting a programming assignment. Clicking on
the yellow magnifying glasses in the left margin will show you a
description of the problem. We'll fix these problems in the next
part of the lab.
Part 6 - Eclipse Auto-Formatting
One of Eclipse's handiest features is the ability to automatically
handle low-level code formatting. This dramatically reduces the
effort involved in producing consistently formatted (and thus more
readable) code. This auto-formatting feature will also help you write
code that conforms to the course coding standards.
-
Download the
file CS159s19_formatter.xml.
This XML file contains the configuration information necessary for Eclipse
to format your code in a way that is consistent with the CS159
coding standards.
-
Configure Eclipse to use the file you just downloaded: Preferences -> Java -> Code Style -> Formatter -> Import.
-
Once you have configured the auto-formatter you should be able to re-format your
Point.java
file by selecting the entire file (CTRL-A) and then pressing SHIFT-CTRL-F.
- Once you have auto-formatted your code (and saved it!), re-validate using
Checkstyle. Hopefully, many of the flagged formatting errors should now
be gone. Most of the remaining issues probably relate to missing
Javadocs.
-
Add appropriate Javadoc comments to your
Point
class. Eclipse will automatically generate a Javadoc template for
any method. You can either type /**[ENTER]
just above
the method, or select the method and press SHIFT-ALT-J.
-
Re-run Checkstyle, and address any remaining formatting issues.
-
Note that a human-readable version of the style guidelines is available as
CS159s19_StyleGuide.pdf
Part 7 - JUnit Integration in Eclipse
JUnit is a framework that automates testing by providing a standard format for tests and
an easy way to execute them (see
JUnit FAQ).
The JUnit site provides a wealth of useful information on JUnit and the host of JUnit-based products
( visit
http://www.junit.org/).
For most of the programming assignments and labs in this course, you
will be required to test your own code using JUnit. We will talk more about
testing before we require you to write your own JUnit tests, but today we will
learn how to create a test class in Eclipse and use provided tests to test your
code.
- Select File -> New -> Junit Test Case. Select
"New JUnit Jupiter test" and
name your class PointTest. If you were creating your own test class you would
now click Next, and select the Point class to
make stub testing methods. Instead click Finish.
-
Now you should be able to copy the text only of the test class provided to you
and paste it into this class. Make sure and download the test class into a text
editor such as Atom or Notepad before copying it. DO NOT copy it from the webpage.
PointTest.java
- To run JUnit tests, select Run -> RunAs -> JUnit Test. This will test your methods using PointTest.
- Finish debugging your Point class using the test cases provided.
Part 8 - Submitting Through AutoLab
-
Navigate
to http://autolab.cs.jmu.edu and
log-in.
- Upload Point.java
- If your code fails any of the submission tests, make any necessary modifications and resubmit until there are no failures.