/**
 * CS149 - Programming Fundamentals
 * Department of Computer Science
 * James Madison University
 * @version Fall 2019
 */

Getting started with Eclipse

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 the location of your CS149 folder (e.g., /cs/home/stu/username/CS149). In other words, select the same CS149 folder you have been using all semester.

  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.

    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 regarding features you are curious about.

  5. Be sure to see how the automatic syntax checking works by introducing some bugs in your HelloWorld program. Then mess up the indenting, and 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 tooltip.

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-11 to locate that zip file. Open it and find the source code for java.lang.String (under the java.base/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 also hover your mouse over pretty much anything. 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 - Starting HW9

When learning to use methods from String and other library classes, like BigInteger 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 HW9. Don't forget to use the project folder as root for sources and class files (see Step 3 above). You may want to configure that option to be the default.

  2. Create a new class named BigFractions, 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 defines the reduce method from HW9. "Stub out" the method so that it compiles.

  4. At this point you may want to set up your Eclipse preferences to match the CS 149 style guidelines. Go to Window -> Preferences and select the General -> Editors -> Text Editors item:

  5. Change the settings to match the screenshot above: insert spaces for tabs, and show print margin at 80 columns. You may also want to show whitespace characters. Then click the Apply and Ok buttons.

  6. Press Ctrl+Shift+F to clean up the code. Quickly write the documentation comments for reduce.

  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 reduce with the sample inputs from the problem description.

  9. At this point, you should have a working (but not completed) BigFractions program. Make sure it compiles and runs without any errors.

  10. Finish implementing your reduce method. Then, if you have time, update your main method so that your program reads two integers from the command line and prints the output of reduce. You should be able to test your program from the command line like this: java BigFractions 14 4


This activity is based on a lab developed by Chris Mayfield.