JMU JMU - Department of Computer Science
Help Tools
Lab: Skills - Using Eclipse to Check Syntax and Style


Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.

Getting Ready: Before going any further, you should:

  1. Download the following files:
    to an appropriate directory/folder (e.g., the course downloads directory/folder). In most browsers/OSs, the easiest way to do this is by right-clicking/control-clicking on each of the links above and then selecting Save as... or Save link as....

1. Getting Started: This part of the lab will help you get started.
  1. If you are completing this lab on a lab computer, and this is the first time (this semester) that you are using Eclipse on a lab computer, follow the instructions for "Configuring the Basic Components" in the course "Help" page on "Installing and Configuring the Development Tools". (Note that Eclipse has already been installed on the lab computers, it just hasn't been configured for this course.)

    If you are using your own computer and you have not yet done so, follow all of the instructions in the course "Help" page on "Installing and Configuring the Development Tools".

  2. Start Eclipse (if it is not running).
  3. If necessary, "clean up" Eclipse by clicking on File and pulling down to Close All.
2. Creating a Project: Eclipse (and most IDEs) organize code into projects. In the context of a university/college course in Computer Science, each lab/assignment/etc... should be in a different project. This part of the lab will help you get comfortable creating (and customizing, if necessary) projects.
  1. Create a new Java project called eclipseskills. To do so, first click on File+New+Java Project. (Note: If Java Project is not an option, you may need to select Other... and expand Java.) Then, enter eclipseskills in the "Project name" field, select "Use default location", select "Create separate folders for sources and class files", unselect "Create module-info.java file", and click on Finish.
  2. In the Eclipse "Package Explorer", expand the project.
  3. Customize the project if necessary. (Note: You will only need to customize the project if you are using different formatters/checks/templates for different projects. Otherwise, you can use the general settings for all projects if you set them up properly when installing the development environment.) See the course "Help" pages for more information.
3. Adding an Existing Source File to a Project: This part of the lab will help you understand how to add an existing source file to a project.
  1. Outside of Eclipse, open a "file explorer" or "finder" window (depending on your operating system) and navigate to the file named TripRecord.java that you downloaded earlier.
  2. Click-and-drag TripRecord.java into the src directory/folder in the eclipseskills project. When prompted, select "Copy files" and click on OK. (Note: If Eclipse does not allow you to do this, it may be because your are not using the "Java Perspective".)
  3. Expand the src folder/directory and the (default package) so that you can see that TripRecord.java has been copied.
  4. Double-click on TripRecord.java to open it.
4. Review: This part of the lab will help you review some things that you should know about Java classes.
  1. "What kind of thing" is line 1?


    An in-line comment.
    Expand
  2. "What kind of thing" are lines 2 through 8?


    A block comment. Specifically, a block comment that describes the class in "javadoc" format.
    Expand
  3. Where can you learn about "javadoc" comments?


    Obviously, there are many places. But it's always good to start with the Department's Wiki since it tends to be targeted towards the needs of JMU students. In this case: https://wiki.cs.jmu.edu/student/java/javadoc
    Expand
  4. "What kind of thing" is line 11?


    A declaration statement.
    Expand
  5. What character terminates statements in Java?


    A semi-colon.
    Expand
  6. "What kind of thing" are lines 21-25?


    A constructor.
    Expand
  7. "What kind of thing" are lines 32-34?


    A method (specifically, an accessor or getter).
    Expand
  8. How many instance attributes are in the TripRecord class?


    Three (one int named passengers, one double named duration, and one double named length).
    Expand
5. Basic Editing:
  1. What text (exactly) appears in the tab TripRecord.java?


    TripRecord.java
    Expand
  2. What appears in the left margin of the comment that starts with TODO? (Note: This will happen any time a comment that contains the text TODO and is a good way to include reminders in your source code.)


    An icon indicating that this is something that still needs to be done. (You can hover your cursor over the icon to get more information.)
    Expand
  3. Change "Madisn" to "Madison" in line 6.
  4. Now what text (exactly) appears in the tab for TripRecord.java?


    *TripRecord.java
    Expand
  5. Click on save.png or File+Save.
  6. Now what text (exactly) appears in the tab for TripRecord.java?


    TripRecord.java
    Expand
  7. What do you think the asterisk in the tab indicates (when it is there)?


    The file has been changed/edited since it was last saved.
    Expand
  8. Click just to the right of the semi-colon in the declaration of the attribute named passengers.
  9. What line and column is the cursor on? (Hint: Look at the bottom of the window.)


    The way my version is formatted, it's line 11, column 28 (which is denoted as 11:28).
    Expand
  10. The word private is a visibility/accessibility modifier in Java so, Eclipse presents it in a special color.
  11. What color?


    On my machine, maroon.
    Expand
  12. What color are attributes, method names, and parameters?


    On my machine, attributes are blue, method names are black, and parameters are brown.
    Expand
  13. Click anywhere on line 1 (i.e., the TODO comment), and type Ctrl-D. This should delete the entire first line.
  14. Save the file.
  15. Click on the minus sign next to the first line of the first block comment. What happens?


    The comment is collapsed so that it takes up less space.
    Expand
  16. Click on the plus sign next to the first line of the first block comment. What happens?


    The comment is expanded again.
    Expand
  17. Change the indentation of several lines in the file (i.e., indent some lines more and some lines less). Now, click on Edit+Select All and then on Source+Correct Indentation. What happened?


    All of the indentation was corrected.
    Expand
6. Checking Syntax: Eclipse compiles source code "on the fly", so it immediately detects syntax errors. This part of the lab will help you become comfortable with this feature.
  1. Delete the semi-colon at the end of the statement in line 10. What happens?


    A warning/error indicator appears in the margin of the line that was just changed.
    Expand
  2. Hover your mouse over the warning/error indicator. What happens?


    A message appears that says something like "Syntax error, insert ;".
    Expand
  3. Fix the syntax error. What happens?


    The warning/error indicator disappears.
    Expand
  4. Change the assignment statement in line 21 so that 50.5 is assigned to this.passengers. What is the syntax error?


    Something like "Type mismatch, Can't convert from double to int".
    Expand
  5. Fix the error.
  6. Change the assignment statement in line 21 so that (int)50.5 is assigned to this.passengers. What isn't this a syntax error?


    Because the double literal 50.5 is being typecast (not rounded)to the int value 50.
    Expand
  7. Change the assignment statement in line 21 so that duration is assigned to this.passengers. What is the syntax error?


    It's the same error (even though a variable is used instead of a literal) - "Type mismatch, Can't convert from double to int".
    Expand
  8. Fix the error.
  9. Change the assignment statement in line 23 so that 5 is assigned to this.length. Why isn't this a syntax error?


    Because Java will widen the int literal 5 into a double.
    Expand
  10. Change the assignment back to what it was (e.g., by clicking on Edit-Undo typing).
  11. Change line 50 so that it returns the value 10.9. What is the syntax error?


    Something like "Type mismatch; cannot convert from double to int."
    Expand
  12. Why is this a syntax error?


    Because the declaration of the getPassengers() method says it returns an int, but the return statements actually returns a double.
    Expand
  13. Fix the error.
  14. Change the identifier in line 10 to passenger. How many warnings/errors are there now (and what are they)?


    Three - one one line 10 ("The value of the field is never used"), one on line 21 ("passengers cannot be resolved to a field"), and one one line 50 ("passengers cannot be resolved to a field").
    Expand
  15. In this case, given what you know about the original code, what is the cause of these three warnings/errors?


    The misspelling in line 10.
    Expand
  16. "Fix" the error (incorrectly) in line 10, by deleting that statement. What happens?


    There are now only two warnings/errors.
    Expand
  17. What does this tell you about fixing syntax errors?


    It's important to find the root cause of the error, which may not be where the warning/error occurs.
    Expand
  18. Fix the error correctly. That is, edit the code so that line 10 again declares a private instance attribute of type int named passengers
7. Logic Defects Caught by Eclipse: Eclipse can't detect most logic defects, but it does help with some.
  1. Change the assignment statement in line 23 to length = length;. What is the warning/error?


    Something like "The assignment to variable has no effect"
    Expand
  2. What is this warning/error telling you?


    That you have assigned the parameter named length to itself, which achieves nothing (because it is tantamount to taking something out of a box and then putting it back in the box).
    Expand
  3. Fix the error.
  4. What is the purpose of the reference this in line 23?


    It distinguishes the instance attribute named length from the parameter named length.
    Expand
  5. What is the . in line 23?


    It is the membership operator. In line 23 it says that this.length is an instance attribute of the owning object.
    Expand
8. Logic Defects Not Caught by Eclipse: Unfortunately, Eclipse can't detect even some obvious logic defects. This part of the lab helps you understand some of them.
  1. Change line 23 so that it assigns the instance attribute named length to the parameter named length. What change did you make?


    length = this.length;
    Expand
  2. Is this a logic error?


    Yes, because the purpose of a constructor is to initialize the instance attributes, and this statement does not do that.
    Expand
  3. What warning/error appears?


    None appear - this is not a logic error that Eclipse can detect. You have to find these errors on your own!
    Expand
  4. Change line 23 so that it assigns the parameter named duration to the instance attribute named length. What change did you make?


    this.length = duration;
    Expand
  5. Is this a logic error?


    Yes, because the wrong value is being assigned to the instance attribute named length.
    Expand
  6. What warning/error appears?


    None appear - again this is not a logic error that Eclipse can detect. You have to find these errors on your own!
    Expand
  7. Change line 23 so that it correctly initializes the instance attribute named length. What change did you make?


    this.length = length;
    Expand
9. Checking Style: If you installed everything properly, you can have Eclipse (using a program called Checkstyle) check to make sure that your code conforms to the course style guide. This part of the lab will help you use Checkstyle.
  1. Delete the leading spaces on line 21 (so that the statement starts in column 1) and save the file. Is this a syntax error?


    No, the Java syntax does not require a particular indentation scheme (as does, for example, Python).
    Expand
  2. Is this a style error?


    Yes - the course styleguide (which you should read if you haven't already) requires that statements in methods be indented.
    Expand
  3. Right-click on TripRecord.java in the package explorer, pull down to "Checkstyle", and over to "Check code with Checkstyle". What happens?


    A warning/error indicator appears in the margin of the line that was just changed, and the line is highlighted.
    Expand
  4. Hover the mouse over the warning indicator next to the declaration of the class. What message appears?


    Something like "Child has incorrect indentation level".
    Expand
  5. Fix the style error, click on save.png, and re-run Checkstyle. What happens?


    The warning/error indicator on that line goes away and the line is no longer highlighted.
    Expand
  6. Delete the period at the end of line 14, save the file, and re-run Checkstyle. What happens?


    A warning/error indicator appears that says something like "First sentence should end with a period".
    Expand
  7. Fix the error.
  8. Change the first occurrence of the word passengers in line 16 to passenger and save the file. Is this a style error?


    Yes, because all formal parameters must have a Javadoc comment that describes them, and the formal parameter named passengers no longer does.
    Expand
  9. Re-run Checkstyle. What happens?


    Two different warnings/errors are displayed. One on line 16 and one on line 20.
    Expand
  10. Given what you know about the code, which is correct?


    The one on line 16.
    Expand
  11. Fix the style error (i.e., spell passengers correctly) in line 16, save the file, and re-run Checkstyle. What happens?


    Both warnings/errors disappear.
    Expand
  12. What does this tell you about correcting style errors?


    Even though Checkstyle is very helpful, thought is required.
    Expand
  13. Activate Checkstyle so that you don't have to run it manually. Specifically, right-click on the project, pull down to Checkstyle, and over to Activate Checkstyle.
  14. Change the spelling of the getDuration() method to GetDuration() and save the file. Is this a style error?


    Yes, the styleguide requires that method names start with a lower-case letter.
    Expand
  15. Did Checkstyle detect the error?


    Yes.
    Expand
  16. Fix the error and save the file.
10. Style Issues Not Caught by Checkstyle: Unfortunately, Checkstyle does not check for everything in the styleguide. This part of the lab will help you understand that you are ultimately responsible for conforming to the style guide.
  1. Change the order of the getLength() and getDuration() methods without changing the methods themselves and save the file. Is this a style error?


    Yes, the styleguide requires that methods be in alphabetical order.
    Expand
  2. Does Checkstyle detect this error?


    No.
    Expand
  3. Fix the error.
11. Comparing Files: "diff" utilities are a convenient way to compare two (or more) versions of the same file. "Visual diff" utilities do so by showing the two files side-by-side in a window with visual cues that help you easily see the differences. This part of the lab will help you compare two versions of the same Java source file to see what changes you made.
  1. In the "Package Explorer", right-click on TripRecord.java, pull down to "Compare With", and over to "Local History...". What happens?


    A "History" window appears.
    Expand
  2. Select the earliest version of TripRecord.java. What happens?


    A window appears with the two versions of TripRecord.java side-by-side.
    Expand
  3. Drag the scrollbar up and down. What happens?


    The two files scroll simultaneously.
    Expand
  4. How does the "visual diff" tool indicate differences between the two versions?


    It highlights them in various different colors.
    Expand
  5. How does the "visual diff" tool indicate that \(n\) lines in one file correspond to \(m\) lines in the other?


    It puts the blocks in between horizontal lines and draws a diagonal/curving line between the two versions.
    Expand
  6. Make sure that you've corrected everything that you were supposed to.
  7. Close the "Compare..." window.

Copyright 2022