CS 149 Fall 2018 - Instructor: Molloy Sections: 10 and 11

Home
Calendar
Syllabus
Resources
Linux Install

Lab 17: Eclipse and Java Library Source Code

Eclipse and Java Library Source Code

Background

Throughout the semester, you have been using classes that come with the Java Platform, Standard Edition or "Java SE" for short. Did you know that the source code for Java SE is freely available? Have you ever wondered what "real world" Java code looks like? This lab has two main objectives: (1) introduce you to the Eclipse IDE, and (2) show you what Java library source code looks like. Eclipse is a heavyweight IDE that makes it easier to navigate large software projects.

Part 1 - Hello World

Eclipse provides two levels of organization:

  1. If you have not done so already, open Eclipse. At the "Select a workspace" prompt, specify a location for your workspace (e.g., /cs/home/stu/username/CS149).
  2. From the "Welcome" screen, click on the tutorials icon (graduation cap and tassel). Select the "Create a Hello World application" tutorial.
  3. Follow the instructions carefully, except for one detail. When creating projects for CS 149/159, we recommend that you do NOT create separate folders for source and class files.
    New project screenshot
    NOTE: Putting source and class files in the project folder enables you to switch back and forth between Eclipse, the command-line, and other editors without making any changes.
  4. Go ahead and close the "Task List" view on the right, since we won't be using it. Explore the menus, toolbars, and other areas of the program. Ask questions on features you are curious about.
  5. Be sure to see how the automatic syntax checking works by introducing some bugs in your HelloWorld program. Press Ctrl+Shift+F to "Format" your code.
  6. Explore the autocomplete feature by typing partial lines of code. For example, type the code System. and see the list of options that pop up. Then type System.out. and press Ctrl+Space to see a list of possible method calls. Use the arrow keys to select different methods, and see how the Javadoc displays in a yellow tooltip.

    Tooltip screenshot

At this point, you should have a working Hello World program. Do not proceed until you have completed the tutorial and Steps 4-6 above.

Part 2 - Java Library Source

Technically speaking, code like System.out.println() is not part of the Java language, but rather part of the Java library. You can learn a lot about programming by exploring the Java library source code.

  1. If you download the source code for the Java library, it comes in a single file named src.zip. From a CS Linux lab machine, type nemo /usr/lib/jvm/openjdk-8 to locate that zip file. Open it and find the source code for java.lang.String(under the java/lang folder)
  2. Eclipse automatically knows about this file if it's installed on your computer. In your HelloWorld.java program, add the line String str = "Hello"; and put your cursor on the word String. Then press the F3 button (which is a shortcut for "Open Declaration").
  3. Notice the "Outline" view on the right. Click on the substring methods and take a look at how they are implemented. Then close the "String.class" window, move the cursor to println, and press F3 again. Now see how that method is implemented.
  4. You can hover your mouse over pretty much anything for additional information. In the println(Object x) method, hover your mouse over the code valueOf and read the tooltip. Then put your cursor there, press F3, and see the source code for valueOf.

Part 3 - CodingBat String-1

When learning to use methods from String and other library classes, you may find the autocomplete feature of Eclipse to be very helpful.  But make sure it doesn't interfere with your learning how to write the code yourself!

  1. Create a new Eclipse project named Lab17. Don't forget to use the project folder as root for sources and class files(see Step 3 above in Part1).  You may want to configure that option to be the default.
  2. Create a new class named StringThing, but do NOT check the box to create a main method. Write a documentation comment including your name and today's date.
  3. Write the code that declares stubs for four string methods from Codingbat.com String1:extraEnd, firstTwo, firstHalf, and withoutEnd. Add stubs so that your code compiles.
  4. At this point you may want to setup your Eclipse preferences to match the CS149 style guide.  Go to Window - Preferences and select the General -> Editors -> Text Editors item:
    Eclipse preferences screenshot

  5. Change the settings to match the screenshot above: insert spaces for tabs, set displayed tab width to 4, and show print margin at 80 columns.  You may also want to show whitespace characters.  Then 
    click the Apply and Ok buttons.
  6. Make sure there is a blank line between each method. Press Ctrl+Shift+F to clean up the code. Quickly write the documentation comments for each method. (You may copy-paste the method descriptions directly from CodingBat.)
  7. Note that Eclipse will auto-generate a documentation template if you select a method and press Shift-Alt-J. You can accomplish the same thing by typing /** then pressing enter above an existing method.
  8. At the bottom of the class (on the line just before the last }), press Ctrl+Space. Arrow down to the main option, and press Enter. Write a simple program that calls your four methods with a test string of your choice("HelloThere") is a good example from Coding Bat.
  9. At this point, you should have a working StringThing program. Make sure it compiles and runs without any errors. You will turn in this file later on.
  10. Finish implemeting your four String-1 methods.  Then in the main method, write a loop that calls each of your methods for each of the provided command line arguments.  

At this point, you should be able to test your program from the command line like this: 
java StringThing This is a test!

 

Submit your StringThing.java to https://autolab.cs.jmu.edu  by the end of the day.  We strongly recommend you also complete the following section, especially if you are interested in using Eclipse on PA5+ and/or the final exam.  It won't take that long, and it will be worth it!


 

Part 4 - Eclipse Plugins(OPTIONAL)

One of the great advantages of using Eclipse is the rich third-party plugin library that exists.  Plugins are developed and maintained by other users and organizations, and are freely available.  This next section will walk through setting up some of the plugins we have and will use in CS149 and CS159.

Checkstyle

  1. Go to Help->Eclipse marketplace,  then find the checkstyle plugin and click the Install button
  2. Configure checkstyle to use our CS149.xml Style guide by Going to Window->Preferences->Checkstyle and importing the cs149.xml as an External Configuration file. (Click New, select the Type, name it cs149, and Browse to the location of the cs149.xml.).  Make sure you select Make Default button and click it and verify that this new config has a green checkmark next to it.
  3. Go to the Package Explorer panel(on the left), right-click your preoject, and select Checkstyle ->Activate Checkstyle to get comments in your main code window.  You will need to repeat this latst step for each project in the future; it's not enabled by default.

Formatter

When you press Ctrl+Shift+F to format your code, it uses the built-in style definitions.  They don't match our Checkstyle configuration, but they are easy to customize.  

  1. Go to Window->Preferences->Java->CodeStyle->Formatter, and then do an import of our CS149-formatter.xml file.  (We suggest you download this file to your workspace directory, so it's easy to find.)  

Take Home Installing on your Linux Mint VM on your own machine: 

  1. To install Eclipse on your Linux Mint Virtual machine follow these instructions: Go to
    http://www.eclipse.org/downloads/index.php to download the latest version of Eclipse.

  2. Make a directory in your Linux VM by running mkdir /home/user/eclipse where user is your username

  3. extract the archive into this directory with archive manager
    or
    tar -zxvf file_name.tar.gz -C /home/user/eclipse

  4. Run the eclipse-inst file by double clicking on it in your explore window.