JMU JMU - Department of Computer Science
Help Tools
Lab: Skills - Using the Eclipse Editor


http://imgs.xkcd.com/comics/real_programmers.png
(Courtesy of xkcd)

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. (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 "Installing and Configuring the IDE" on the course "Help" page starting after the step "Install Eclipse" (which has already been done on the lab computers).

    If you are using your own computer and you have not yet done so, follow the instructions for "Installing and Configuring the IDE" on the course "Help" page.

  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 (and Customizing) 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", 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 Source Files to a Project: This part of the lab will help you understand how to add a source file to a project in two different ways. The first way to add a source file to a project is to copy an existing source file. Specifically:
  1. Open a "file explorer"/"finder" window (outside of Eclipse) and navigate to the file you downloaded.
  2. Click-and-drag PictureFrame.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 PictureFrame.java has been copied.
  4. Double-click on PictureFrame.java to open it.

The second way to add a source file to a project is to create it in Eclipse. Specifically:

  1. Create a new class named WhatAnEgo in the default package of the project named eclipseskills. That is, click on File+New+Class, enter WhatAnEgo in the "Name" field, make sure the "Package" field is blank, select "public static void main(String[] args)", deselect all other "stubs", deselect "Generate comments", and click on Finish. (Note: Eclipse may warn you that you should not use the default package. You should ignore that warning.)
  2. What text appears in the newly created WhatAnEgo.java?


    Something like:
    public class WhatAnEgo
    {
        public static void main(String[] args) 
        {
            // TODO Auto-generated method stub
    
        }
    }
    
    Expand
4. Basic Editing:
  1. What text (exactly) appears in the tab for WhatAnEgo.java?


    WhatAnEgo.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. Replace the body of the main() method (i.e., the "TODO" comment) of WhatAnEgo.java with the following.
        System.out.print("Prof. ");
        System.out.print(args[0]);           // args[0] should be a name
        System.out.print(" is the best!");
        System.out.print("\n");              // '\n' is the newline char
    
        PictureFrame frame;
        // Done
    

    but do not save the changes.

  4. Now what text (exactly) appears in the tab for WhatAnEgo.java?


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


    WhatAnEgo.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 left of the semi-colon in the declaration of the first invocation of System.out.print().
  9. What line and column is the cursor on now? (Hint: Look at the bottom of the window.)


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


    On my machine, maroon.
    Expand
  12. Go to any instance of the word public and delete the b.
  13. What color is it now?


    On my machine, black. Hence, it must be a typo.
    Expand
  14. What else changed?


    A red X appears in the margin next to the line with the typo.
    Expand
  15. Hover the mouse over the red X. What message appears?


    Something like: "Syntax error on token pulic, public expected".
    Expand
  16. How does Eclipse know that there is a syntax error?


    It compiles the code as it is edited. In other words, there's no reason to click on a "Compile" button, Eclipse is constantly compiling the code to determine if it obeys the syntactic rules of (i.e., the grammar of) Java.
    Expand
  17. Fix the typo you just created.
  18. Click on the tab for PictureFrame.java and scroll through the code. What do you notice immediately?


    The code is not indented consistently, making it almost unreadable.
    Expand
  19. Click on Edit+Select All and then Source+Format or type Ctrl+A and then Shift+Ctrl+F. What happens?


    All of the code is selected and then the code is formatted (using the formatter you installed when you installed Eclipse). The formatter corrects the indentation, puts curly brackets in the right place, and does several other useful things.
    Expand
  20. Click on save-all.png.
5. Getting Help and Reducing the Amount of Typing: Eclipse knows a lot about the Java programming language and, as a result, can provide help when needed. This part of the lab will help you take advantage of this features.
  1. Click on the WhatAnEgo.java tab.
  2. Hover the mouse over the word System. What information appears?


    Information about the System class.
    Expand
  3. Hover the mouse over any occurrence of the word out. What information appears?


    Information about the static attribute out in the System class.
    Expand
  4. Click on the end of the line containing the declaration of the local variable named frame and press Enter. Where is the cursor?


    On the next line, properly indented.
    Expand
  5. Type System. (note the period). What happens?


    A list appears of all of the valid things that can be typed next. Note: If this doesn't happen, you may need to change your settings. To do so, click on Window or Eclipse (depending on your operating system) and then Preferences. Expand "Java", expand "Editor", expand "Content Assist", select "Advanced" and select everything that includes "Java". Then click on Apply and Close.
    Expand
  6. Type o (note that there is no period this time). What happens?


    Eclipse highlights either the first option starting with "o" or the most likely option starting with "o", depending on your settings.
    Expand
  7. Press Enter. What happens?


    Eclipse inserts the selected option into the source code.
    Expand
  8. Type . again. What happens?


    A list appears of all of the valid things that can be typed next.
    Expand
  9. Select one to see what happens.
  10. Delete the line you just added.
  11. Note: If, at any time, you want to force Eclipse to give you a list of options/recommendations, you can press Ctrl and the spacebar at the same time.
  12. Add a new statement (before the Done comment) that declares a Color object named highlight.
  13. What statement did you add?


    Color highlight;
    
    Expand
  14. What happened after you added this statement?


    An error indicator appeared on the line and the word Color was underlined in red.
    Expand
  15. Hover your mouse over the error indicator. What is wrong?


    Color can't be resolved to a type.
    Expand
  16. Right-click on the error indicator and pull down to Quick Fix. What happens?


    Eclipse displays several different ways to fix the error.
    Expand
  17. Which option is the correct "fix"?


    Import 'Color' (java.awt).
    Expand
  18. Select that option.
  19. What happens?


    Eclipse adds the statement:
    import java.awt.Color;
    
    at the top of the file. (Also, a warning indicator is displayed indicating that the variable named highlight is not used.
    Expand
  20. There's a VERY important lesson to be learned from this experience, that is much more general than it might first seem. What is it?


    While Eclipse can be very helpful, the first "recommendation" may not be the correct one (and may mislead you). You have to know how to correct defects!
    Expand
  21. Delete the statement that declares the variable highlight.
  22. Other than the obvious, what happens?


    A warning indicator appears on the import statement telling you that it is never used.
    Expand
  23. Delete the import statement.
  24. Save WhatAnEgo.java.
  25. Click on the tab containing "PictureFrame.java". Notice that the class contains private attributes but no getters or setters.
  26. PictureFrame.java doesn't contain setters because it is immutable. However, we would like to add getters. To so so, click on Source+Generate Getters and Setters..... Then, click on Select Getters, select "Generate methods comments", and click on Generate. What happens?


    Appropriately named and partially commented getters are created for all of the private attributes.
    Expand
  27. What other source code can Eclipse generate automatically?


    Most importantly, constructors, the toString() method, and the equals() method.
    Expand
  28. Click on Source+Generate toString()...., select all of the attributes, and then click on OK. What code is generated?


      public String toString()
      {
        return "PictureFrame [stand=" + stand + ", height=" + height + ", matte=" + matte + ", width="
            + width + "]";
      }
    
    Expand
  29. The specification for the PictureFrame class says that the toString() method must return a String containing the width, followed by "in. x ", followed by the height, followed by "in.". If the matte is greater than 0, this must be followed by " with a ", followed by the matte, followed by "in. matter". Finally, if it has a stand, it should be followed by " (w/ stand"). Does the code that was generated by Eclipse satisfy the specification?


    No.
    Expand
  30. Replace the toString() method generated by Eclipse with the following.
     /**
       * Return a human-readable String representation of this PictureFrame.
       *
       * @return  The String representation
       */
      public String toString()
      {
        String      result;
    
        result = ""+width+"in. x "+height+"in.";
        if (matte > 0.0) result += " with a "+matte+"in. matte";
        if (stand)       result += " (w/ stand)";
    
        return result;
      }
    
  31. There's a VERY important lesson to be learned from this experience, that is much more general than it might first seem. What is it?


    While Eclipse can be very helpful, it's "recommendations" are often incorrect! Eclipse can be a very helpful tool, and it can save you some time, but it can also confuse/mislead beginning programmers. Do not let Eclipse become a crutch.
    Expand
6. Using Checktyle and Other Static Analysis Tools: 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. Click on the tab for WhatAnEgo.java.
  2. Right-click on WhatAnEgo.java in the package explorer, pull down to "Checkstyle", and over to "Check code with Checkstyle". What happens?


    Some warning/error indicators appear in the margins of some lines, and those lines are highlighted.
    Expand
  3. Hover the mouse over the warning indicator next to the declaration of the class. What message appears?


    Missing a javadoc comment.
    Expand
  4. Add the following block comment to the top of the class.
    /**
     * A very simple Java program that does nothing but generate
     * some output.
     *
     * @author Prof. Bernstein
     * @version 1.0
     */
    

    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
  5. Click on the minus sign next to the first line of the comment. What happens?


    The comments are collapsed so that they take up less space.
    Expand
  6. Click on the plus sign next to the first line of the comment. What happens?


    The comments are expanded again.
    Expand
  7. Add the following block comment to the top of the main method.
      /**
       * The entry point of the application.
       */
    

    click on save.png, and re-run Checkstyle. What happens?


    Now only the parameter args is highlighted.
    Expand
  8. Hover the mouse over the warning/error indicator. What message appears?


    Expected @param tag for 'args'.
    Expand
  9. Add the following line to the comment.
       * @param args  The command-line arguments
    

    and click on save.png. What happens?


    The warning/error indicator goes away and the line is no longer highlighted.
    Expand
  10. Hover the mouse over the warning indicator in the margin of the line that declares the variable named frame. What information appears?


    The value of the local variable named frame is not used.
    Expand
  11. Add the following code just above the line containing the comment "Done".
            
        System.out.print("I have a picture of him in a frame that is ");
        System.out.print(frame.toString());
        System.out.print(".\n");
    

    and click on save.png. What happens?


    The warning indicator on the declaration goes away, but an error indicator appears in the margin of the statement that calls the toString() method of the frame object.
    Expand
  12. Hover the mouse over the error indicator. What information appears?


    The local variable frame may not have been initialized.
    Expand
  13. What are the formal parameters of the explicit value constructor in the PictureFrame class?


    An int containing the width, an int containing the height, an int containing the size of the matte, and a boolean indicating whether or not the PictureFrame has a stand.
    Expand
  14. In the line above the three lines you just added (but after the declaration of frame), instantiate frame so that it is 8in x 10in with a 1in matte and a stand. What code did you add?


        frame = new PictureFrame(8, 10, 1, true);
    
    Expand
  15. Click on save.png. What happens?


    The error indicator goes away
    Expand
  16. Click on the tab for PictureFrame.java.
  17. Run Checkstyle on PictureFrame.java.
  18. Click on the "Problems" tab under the tab containing the source code. If there is no "Problems" tab, click on Window+Show View+Problems.
  19. Are there any warnings and/or errors?


    Yes, because Checkstyle identified several aspects of the source code that are not consistent with the style guide for the course. These errors/warnings correspond to the lines in the source code that are highlighted.
    Expand
  20. Correct each of the style defects.
7. 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 PictureFrame.java, pull down to "Compare With", and over to "Local History...". What happens?


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


    A window appears with the two versions of PictureFrame.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. Close the "Compare..." window.
8. Integrated Development Environments (IDEs): An Integrated Development Environment is more than just an editor. One of the things an IDE provides is the ability to execute code. This part of the lab will help you gain skills with running code in Eclipse.
  1. Click on run.png. What happens?


    The "Console" tab appears and indicates that an ArrayIndexOutOfBoundsException was thrown.
    Expand
  2. Describe the purpose of the formal parameter args in the main() method.


    It contains the command line arguments that were passed to the program when it was run.
    Expand
  3. What command line arguments did you pass to main()?


    I didn't pass any!
    Expand
  4. Why was the exception thrown?


    Because there isn't an element 0 of the args array because I didn't pass any command line arguments.
    Expand
  5. Click on Run+Run Configurations.... What happens?


    A dialog box appears.
    Expand
  6. Select the run configuration for "WhatAnEgo" (if there is more than one). Then, click on the "Arguments" tab of the dialog box and enter Bernstein in the "Program arguments" field. Then click Run. What happens.


    The program runs (and now I understand why it was named WhatAnEgo.java.
    Expand
9. Temporarily Modifying Code: When debugging, it is common to add and remove code temporarily. This part of the lab will help you get better at doing so.
  1. You've decided that the program isn't doing what it should and you're thinking of deleting the first four lines that call the print() method. However, you're not sure and you don't want to re-type them if you don't have to. So, click and drag to select all of those lines. What happens?


    The lines are highlighted.
    Expand
  2. Click on Source+Toggle Comment. What happens?


    Each line is now a comment.
    Expand
  3. Click on Source+Toggle Comment again. What happens?


    The lines are un-commented.
    Expand

Copyright 2021