Eclipse Lab |
CS 159 CS 159 Docs Labs Projects Labs Lab 01 - Eclipse Lab Lab 02 - Memory Lab Lab 03 - Two-Dimensional Arrays Lab Lab 04 - Debugger Lab Lab 05 - Class Design Lab View page source Edit this page Create child page Create documentation issue Print entire section Introduction Part 1 - Coding in Eclipse Part 2 - Adding A .jar File Part 3 - Running Part 4 - JUnit Part 5 - Checkstyle Eclipse Integration Part 6 - Eclipse Auto-Formatting Part 7 - Submitting Through Gradescope Tag Cloud .equals1 .jar1 .toString1 2D-array2 accessor1 activation record1 arguments1 array2 checkstyle2 class design1 constructor1 data modeling1 debugger1 docs1 eclipse3 exceptions1 format2 gradescope1 heap1 install1 jdk1 junit1 lab1 memory1 mutator1 OOP1 overload1 override1 project3 project11 project21 setup1 stack1 stub1 tutor1 validate1 visualizer1 Categories Docs2 Lab6 Project3 Labs Lab 01 - Eclipse Lab Eclipse Lab Tags: eclipse junit gradescope format checkstyle .jar Categories: Lab 6 minute read 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 Create a new Eclipse project named PointLab: File -> New -> Java Project 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 Note: 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);
}
}
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. 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. Right click PointCode.jar and select Build Path -> Add to Build Path. 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 Download the file PointDemo.java to your Desktop, then drag it into the default package section of your project. 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. 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. 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. 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 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. 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. 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. 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. Part 7 - Submitting Through Gradescope 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 February 22, 2022: added some provided files (b6e584a) © 2022 The Docsy Authors All Rights Reserved Privacy Policy About Goldydocs