In this lab, you will practice the process of creating a class in Eclipse and submitting it through Autolab.
Before working on this lab, you should already have completed the steps in Installing Java and Eclipse.
PointLab
: Create a new class that corresponds to the UML
diagram below. For now, don’t worry about commenting the code. We’ll address
that later.
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 other
point has the same x and y coordinates.
Notice that the parameter type for the equals
method
is Object
, not Point
. This is the standard
signature for an equals
method: objects of different
types should generally be considered not-equal, but it should be
possible to perform the comparison.
This means that equals
method implementations are
typically structured to use the instanceof
operator as in
the following example:
public class Person {
private String name;
// Other methods not shown...
public boolean equals(Object other) {
// Return false if other is not a Person.
if (!(other instanceof Person)) {
return false;
}
// Cast other to Person so that we can access
// members.
Person otherPerson = (Person) other;
// Now we can safely compare.
return name.equals(otherPerson.name);
}
}
Point
class, move on to the next step.
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.
PointCode.jar
and select Build Path -> Add to Build
Path.
PointCode.jar
archive. All classes
included in that archive are now available within your project. In
this case there is only one
class: PointDisplay.
PointDemo
class to get a feel for what
it should do. Run the code to confirm that everything is
working as expected.
JUnit is a tool that makes it possible to automate the execution of Java test code. It is also the tool that is used by Autolab to test your submissions. Don’t worry if you haven’t used JUnit before, we’ll spend more time on testing and JUnit later in the semester. For now, the goal is just to practice executing pre-written JUnit tests.
Point.java
at the conclusion of
this activity.
Option 1: You should see that the first underlined statement at
the top of PointTest.java
is this:
org.junit
on this line, Eclipse will
pop up a box with an error message (“the import org.junit cannot be resolved
”) and the message “three quick fixes available:
”. If you
scroll to the bottom and select “Fix project setup
” you should see a
dialog offering to Add JUnit 5 library to build path
. Click OK. Be careful!
If it offers to add JUnit 4, cancel and try the next option instead.
PointTest.java
and selecting
Run As -> JUnit Test . Hopefully, all of the tests
will pass. If not, fix the errors in Point.java
before moving
on to the next step.
PointLab
project in
the “Package Explorer” tab and then select Checkstyle -> Activate Checkstyle.
Point.java
should now be marked
with Checkstyle warnings. 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
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.
Assuming that your auto-formatter is configured correctly, you should
be able to re-format your Point.java
file by selecting
the entire file (CTRL-A) and then pressing CTRL-SHIFT-F.
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.
Point.java
through http://autolab.cs.jmu.edu. If
your code fails any of the submission tests, make any necessary
modifications and resubmit until there are no failures.
Don’t forget that Autolab is only accessible through the VPN. This page has instructions for connecting:
If you finish the steps above before the end of the lab period,
download the following two files and complete the unfinished method in
PointUtils.java
:
Once you have completed PointUtils.java
update the PointDemo
class
so that it uses your newly completed methods to draw a beautiful
picture.
There is nothing to submit for this part of the lab.