JMU JMU - Department of Computer Science
Help Tools
Lab: Skills - Using VSCode 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 VSCode on a lab computer, follow the instructions for configuring VSCode for this course that are available on the course "Help" page. (Note that Java and VSCode have 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, install and configure all of the tools following the instructions in the course "Help".

  2. Create a directory/folder for this lab (e.g., named lab01) under the src directory.
  3. Copy the files you downloaded to the directory/folder that you just created.
  4. Start VSCode.
  5. Click on explorer.png in the activity bar. (This will open the explorer view in the primary side bar.)
  6. Expand CS159, src, and the directory/folder you created for this lab.
  7. Open an editor containing TripRecord.java by double-clicking on the file with that name in the explorer view.
2. 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 3?


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


    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 13?


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


    A semi-colon.
    Expand
  6. "What kind of thing" are lines 23-27?


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


    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
3. Basic Editing:
  1. What, if anything, is overlayed on the explorer.png icon at the left side of the window?


    Nothing.
    Expand
  2. What "words" appear at the start of line 3? (Note: This is a good way to include reminders in your source code.)


    The "words" TODO.
    Expand
  3. Change "Madisn" to "Madison" in line 6.
  4. Now, what, is overlayed on the explorer.png icon at the left side of the window?


    The number 1.
    Expand
  5. Click on File+Save (or type Ctrl+S).
  6. Now, what is overlayed on the explorer.png icon at the left side of the window?


    Nothing.
    Expand
  7. What do you think the number tells you?


    The number of files that have been changed/edited since they were 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 status bar at the bottom of the window.)


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


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


    On my machine, dark blue.
    Expand
  13. Click on Help+Show All Commands, enter "Delete" in the textfield, and find the command that deletes an entire line.
  14. Click anywhere on line 3 (i.e., the TODO comment), and press the keys needed to to delete the entire line.
  15. Save the file.
  16. Hover the mouse over the blank space between the line number and the start of the first block comment and click on the character that appears. What happens?


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


    The comment is expanded again.
    Expand
  18. Change the indentation of several lines in the file (i.e., indent some lines more and some lines less). Now, right-click inside the document and pull down to Format Document. What happened?


    All of the indentation was corrected.
    Expand
4. Checking Syntax: VSCode 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 11. What happens?


    An editor decoration appears in line that was just changed.
    Expand
  2. Hover your mouse over the editor decoration. What happens?


    A message appears that says something like "Syntax error, insert ;".
    Expand
  3. What color is the is the editor decoration?


    Red.
    Expand
  4. Find this error in the problems view. What caused it?


    It is a syntax error in Java.
    Expand
  5. Fix the syntax error. What happens?


    The editor decoration disappears.
    Expand
  6. Change the assignment statement in line 22 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
  7. Change the assignment statement in line 22 so that (int) 50.5 is assigned to this.passengers. Why isn't this a syntax error?


    Because the double literal 50.5 is being typecast (not rounded)to the int value 50.
    Expand
  8. Change the assignment statement in line 22 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
  9. Change line 22 so that passengers is assigned to this.passengers (as it was originally).
  10. Change the assignment statement in line 24 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
  11. Change the assignment back to what it was.
  12. Change line 51 so that it returns the value 10.9. What is the syntax error?


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


    Because the declaration of the getPassengers() method says it returns an int, but the return statement actually returns a double.
    Expand
  14. Change line 51 back to what it was originally.
  15. Change the identifier in line 11 to passenger. How many warnings/errors are there now (and what are they)?


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


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


    There are now only two warnings/errors.
    Expand
  18. 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
  19. Fix the error correctly. That is, edit the code so that line 11 again declares a private instance attribute of type int named passengers
5. Logic Defects Caught by VSCode: IDEs can't detect most logic defects, but they can help with some.
  1. Change the assignment statement in line 24 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. What color is the is the editor decoration?


    Orange.
    Expand
  4. Find this warning/error in the problems view. What caused it?


    It is a violation of the course style guide that was identified by Checkstyle.
    Expand
  5. Fix the defect.
  6. What is the purpose of the reference this in line 24?


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


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


    this.length = 0;
    Expand
  2. Is this a logic defect?


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


    None appear - this is not a logic defect that VSCode can detect. You have to find these defects on your own!
    Expand
  4. Change line 24 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 defect?


    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 defect that VSCode can detect. You have to find these defects on your own!
    Expand
  7. Change line 24 so that it correctly initializes the instance attribute named length. What change did you make?


    this.length = length;
    Expand
7. Checking Style: If you installed everything properly, you can have VSCode (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 (or tab) on line 22 (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 defect?


    Yes - the course styleguide (which you should read if you haven't already) requires that statements in methods be indented.
    Expand
  3. Does VSCode indicate that there is a style defect?


    Yes, an editor decoration appears one the line.
    Expand
  4. What color is the is the editor decoration?


    Orange.
    Expand
  5. Find this warning/error in the problems view. What caused it?


    It is a violation of the course style guide that was identified by Checkstyle.
    Expand
  6. Hover the mouse over the editor decoration. What message appears?


    Something like "Child has incorrect indentation level".
    Expand
  7. Fix the style defect. What happens?


    The editor decoration on that line goes away.
    Expand
  8. Delete the period at the end of line 15. What happens?


    An editor decoration appears one line 14 that says something like "First sentence should end with a period".
    Expand
  9. Fix the defect.
  10. Change the first occurrence of the word passengers in line 17 to passenger and save the file. Is this a style defect?


    Yes, because all formal parameters must have a Javadoc comment that describes them, and the formal parameter named passengers no longer does.
    Expand
  11. What editor decorations appear?


    Two different editor decorations are displayed. One on line 17 and one on line 21.
    Expand
  12. Given what you know about the code, which is correct?


    The one on line 21.
    Expand
  13. Fix the style defect (i.e., spell passengers correctly) in line 17. What happens?


    Both editor decorations disappear..
    Expand
  14. What does this tell you about correcting style defects?


    Even though Checkstyle is very helpful, thought is required.
    Expand
  15. Change the spelling of the getDuration() method to GetDuration(). Is this a style defect?


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


    Yes.
    Expand
  17. Fix the defect.
8. 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 defect?


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


    No.
    Expand
  3. Fix the defect.
  4. Fix any remaining style defects.
9. 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 explorer view, right-click on TripRecord.java and pull down to Open Timeline. What happens?


    A time Line view appears under the expolorer view.
    Expand
  2. Select a previouse 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. Close the "Visual diff" window.

Copyright 2024