Eclipse Lab

Introduction

In this lab, you will practice the process of creating a class in Eclipse and submitting it through Gradescope.

Before working on this lab, you should already have completed the steps in Installing Java and Eclipse .

Part 1 - Coding in Eclipse

  1. Create a new Eclipse project named PointLab:
    File -> New -> Java Project

  2. 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.

    Point

    UML for the Point class

    UML for the Point class


    Note:

    1. 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)”.

    2. The equals method should return true if the other point has the same x and y coordinates.

    3. 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.

    4. 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);
      
          }
      
      }
      
  3. Once you have completed a syntactically correct Point class, move on to the next step.

Part 2 - 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.

  1. Create a new folder named “lib” in your Java project. Download the file /docs/eclipse/ PointCode.jar , and drag it into the newly-created folder.
  2. Right click PointCode.jar and select Build Path -> Add to Build Path.
  3. There should now be a “Referenced Libraries” entry in your project 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.

Part 3 - Running

  1. Download the file PointDemo.java to your Desktop, then drag it into the default package section of your project.
  2. 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 4 - JUnit

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.

  1. Download the file PointTest.java and drag it into your PointLab project. These are exactly the tests that will be used by Gradescope when you submit Point.java at the conclusion of this activity.
  2. Initially, this file will not compile. If you open it up you will see that many of the lines of code are labeled as errors. The problem here is that JUnit isn’t part of the Java language. It is an external library that must be added to the Java Build path before it can be used. There are several different ways that this can be managed in Eclipse:
    • Option 1: You should see that the first underlined statement at the top of PointTest.java is this:
      import static org.junit.jupiter.api.Assertions.*;
      
    If you hover your mouse overorg.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.
    • Option 2: A simple trick that you can use to add JUnit to the build path is to ask Eclipse to auto-generate a JUnit test file. Just right-click on any file in your project and select New -> JUnit Test Case. Make sure the top radio button in the dialog is set to “New JUnit Jupiter test”. Keep the rest of the default options and click “Finish”. This should create a new test file (which you can delete) and add JUnit 5 to the build path.
  3. Once everything is compiling successfully, you can execute the tests by right-clicking 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.

Part 5 - Checkstyle Eclipse Integration

  1. Recall that Checkstyle needs to be explicitly enabled for each new Eclipse project. Right click your PointLab project in the “Package Explorer” tab and then select Checkstyle -> Activate Checkstyle.
  2. Once you have completed the previous step, navigate to your file. Many lines of code in 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.

Part 6 - Eclipse Auto-Formatting

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.

  1. Once you have auto-formatted your code you must save it before Checkstyle will recheck it. Hopefully, many of the flagged formatting errors should now be gone. Most of the remaining issues probably relate to missing Javadocs.
  2. 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.
  3. Re-run Checkstyle, and address any remaining formatting issues.

Part 7 - Submitting Through Gradescope

  1. Submit Point.java through https://www.gradescope.com/ . You should have an email from your instructor that indicates you have been added to the course. If you have not used Gradescope before, you may need to create an account. If your code fails any of the submission tests, make any necessary modifications and resubmit until there are no failures.
Last modified April 30, 2022: practice coding exam (a2ce8c8)