CS 124, Fall 2017
Lab 6: Eclipse (and subroutines)

This lab introduces an "Integrated Programming Environment" (IDE) called Eclipse. Eclipse provides a complete GUI environment for writing, compiling, and running Java programs. It is a free program, but it is one of the most common environments used by professional Java programmers. It is a complex program that can do many things, but you don't have to understand everything that it can do in order to use it effectively. Interestingly, Eclipse is itself written in Java.

There is also a new, short programming assignment for you to work on, to help you get used to working in Eclipse. You will also write some subroutines from scratch for the first time. In addition to that assignment, you can work on the assignments from Lab 5 in Eclipse. Your work from both labs is due Friday of next week. (The work for Lab 5 was postponed from the original due date.)

You will not need to make a folder for use in this lab. Instead, you will create an Eclipse project. In order to complete the lab, you will need copies of three Java files, FirstEclipse.java and TextIO.java. You can find copies in /classes/cs124, as usual, but you will have to work through the lab to find out how to use them in Eclipse.

The work from Labs 5 and 6 is due by 3:00 PM next Friday, October 13. You will submit files named ArrayStats.java, Animation.java, and FirstEclipse.java. Submit them using the Submit web page. All the files are listed under "Lab5" on that web site. (But see the last section of this lab for information about how to find and submit a file from an Eclipse project.)

Running Eclipse

This section just describes Eclipse; the next section will tell you how to use it for a programming project. Eclipse is a widely used professional environment for program development. Note that Eclipse and IDEs in general are also discussed in Section 2.6 of the textbook. Eclipse is already installed on our Linux computers. You can access it in the "Programming" sub-menu of the application menu. You might want to add an "Eclipse" button to the panel at the top of the screen to make it easier to get to. To do that, simply right-click the "Eclipse" item in the menu, and select "Add to Panel".

Although it is not required for the course, you might consider running Eclipse on your own computer as well. It is available for Linux, Windows, and Mac OS. You can download the most recent version of the Eclipse installer from the Eclipse downloads page at


You should be sure to get the "Eclipse IDE for Java Developers". If you have a choice between "32 bit" and "64 bit" versions, you very likely want 64 bit. Note that you need to have Java on your computer before you install Eclipse, since Eclipse is itself written in Java. If you would like some help installing Java or Eclipse on your computer, ask for it.

Eclipse organizes your programming projects into "workspaces." A workspace is really just a directory, and a project is a subdirectory inside that directory. A project holds the files relevant to a given programming project, such as Java source code files, compiled class files, and data files. You should let Eclipse have complete control over the workspace directories, and you should not directly change any files that are stored in them. It is possible to have as many workspaces as you want, but for this course, you will probably keep all of your projects in the same workspace. (In fact, you can keep all the programs that you ever write in one workspace, if you want.)

When Eclipse starts up for the first time, it asks you to select a workspace. (There is a box that you can check if you don't want to be asked to select a workspace each time you run Eclipse.) By default, the name of the workspace will be "workspace", but you can change the name if you want. For example, you could name it "workspace-cs124".

When the Eclipse window first opens, it will be filled with a "Welcome" screen. The icons on this screen link to a large amount of information about Eclipse. You can browse through this information some time, if you like, but for now, just close the Welcome screen by clicking the "×" next to the word "Welcome", near the top left corner of the window. To avoid seeing the Welcome screen every time you start Eclipse, uncheck the box labeled "Always show Welcome at startup" at the bottom left corner of the Welcome screen.

Eclipse uses the terms view and perspective to describe the way that information is organized and presented in its window. A "view" is one of the panels in a "perspective." The window typically contains several views. Each view contains a certain type of information. All the views that are visible in the window constitute a perspective. A perspective is meant to contain all the views that are used in some particular activity. For now, you will just use the Java perspective, which is organized for Java programming. Each perspective is highly customizable. You can add and delete views and move them around. If you ever delete a view accidentally, you can get it back by selecting it from the "Show View" submenu in the "Window" menu. If you mess up a perspective, you can get back the original, default setup with the "Reset Perspective" command in the "Perspective" submenu of the "Window" menu.

After you have closed the Welcome screen, the window will show the Java perspective. Initially, all the views are empty. Here is what the Java perspective might look like after a little work has been done:

Eclipse Window showing Java Perspective

In this window, I have closed a couple of the views that I don't use: the "Task List" and "Outline" views, which originally appeared on the right edge of the window. Remember that a view can be closed by clicking the small "×" next to the name of the view, and it can be reopened using the "Window" / "Show View" menu.

The "Package Explorer" view, on the left, is central to much of the work that you do in Eclipse. It contains a list of your programming projects and the Java files and resources that are contained in those projects. In the above picture, there is just one project, named Lab6. Clicking on the small triangle (or plus sign) next to the project name will show/hide the resources contained in the project. In a new project, there will be a directory named src where the source files for the project will be stored. Inside the src folder, you will see Java files organized into packages. In this course, we will only use the "default package," as shown in this example.

The lower right section of the window contains several views. In the picture, the "Console" view is showing. To see one of the other views, such as "Problems" or "Javadoc", just click on the tab that contains the name of the view. Sometimes, another view will pop up automatically in this area to show the output of some command. When you run a program, standard input and output are done in the "Console" view. Errors and warnings from the Java compiler are displayed in the "Problems" view.

The central area of the window is occupied by editor views. Here, I've created a Java file, HelloFromEclipse.java, and have opened it for editing. Another source file, named TurtleGraphics.java, has also been opened and is in another tab. (That file is not actually part of this lab; you might work on it next week.)

The view of HelloFromEclipse.java shows several of the nifty features of the Java editor. The source code that you type is checked for syntax errors as you type. Errors are marked with small red carets at the bottom of the line. The error is also marked by a red rectangle in the right margin of the editor and sometimes by an icon in the left margin of the editor; if you hover your mouse over any error or error marker, you see an error message for the error. For the first error in the picture, for example, you would be told that a semicolon is missing. In the third line of main(), the word "printnl" is underlined with error markers. (It's a misspelling of "println".) This error has an error light bulb () in the left margin of the editor. The light bulb means that Eclipse is not just telling you about the error but is offering you some ideas for fixing it. If you click the light bulb, you get a list of actions that can be taken to fix the problem. You will also see the list if you hover the mouse over the underlined error itself. For the example, here is what I get when I hover the mouse over the incorrect word "printnl":

change to print(), and other options

Clicking on an action, such as "Change to print()", will apply that option automatically. Sometimes, you will see a warning light bulb (). A warning indicates a potential problem, but not an error. Warnings will not prevent the source code from being compiled successfully.

In fact, Eclipse might be a little too enthusiastic in marking warnings and errors! You do not necessarily have to fix every warning. And you do not have to fix every error as soon as it appears. In fact, it's impossible to do so. In some cases the error will go away by itself after you've typed in more of your program. And remember that the fix for a programming error does not always go at the location of the error; sometimes the problem is elsewhere in the file. Furthermore, Eclipse's error system is only effective if you routinely get most of your program correct in the first place — don't expect Eclipse to make solid Java programming skills unnecessary!

In fact, many of the fixes that Eclipse will offer will do things to your code that you won't understand. Not every fix is a good idea! Don't let Eclipse fix your code unless you understand what it wants to do and why!

Note that Eclipse will even spell-check your comments. It will mark a misspelled word in a comment with a red underline. If you hover the mouse over a misspelled word, you will get a list of possible corrections. Click an item in this list to apply the correction to the word.

Your First Eclipse Project

It's time for you to start using Eclipse. Start up Eclipse, as described above. Close the "Welcome" screen (and probably the "Task List" and "Outline" views as well). You will create a project just like the one shown in the above illustrations.

To create your project, right-click in the "Project Explorer" pane on the left edge of the Eclipse window. In the pop-up menu that appears, go to the "New" submenu, and select "Java Project." (Alterntively, select "New" / "Java Project" from the "File" menu.) A "New Project" wizard will pop up. All you have to do is enter a name for the project, in the box labeled "Project Name". Enter Lab6 or any other name as the name of the project. After entering the project name, click the "Finish" button. The project will be added to "Project Explorer", and a directory of the same name will be created in your Eclipse workspace directory.

In general, you will create some new Java classes, and you will need to add some existing files into your project. For this example, you will add the two required files, FirstEclipse.java and TextIO.java.

To add the files: Open a file browser window in which you can see the files. (Browse to /classes/cs124.) In the Eclipse window, click the triangle next to the project name (such as "Lab6"), so that you can see the folder labeled "src". You can then use Copy-and-Paste to copy the files from the file browser window into the src folder in Eclipse. (That is, right-click a file in the file browser window, select "Copy", then right-click the src folder in the Eclipse window and select "Paste".) Alternatively, you can drag files from the file browser window onto the src folder in the Eclipse window. When you do that, you will be asked whether you want to "Copy" or "Link" the files. Choose "Copy." Copies of the files will be added to the project.

After adding the files, if you click the triangle next to "src", you should see that the three Java files have been placed into the "(default package)". To open a file in the Eclipse editor, just double-click its name in the Package Explorer view.

You need to learn how to create a new class in Eclipse!: Right-click "(default package)" or "src" in the "Package Explorer" view. In the pop-up menu, go the "New" submenu again, and select "Class":

menus for creating a class

A class creation dialog box will pop up. Again, you just have to fill in the name of the class, and click the "Finish" button. Note that you have to enter the name of the class, not the name of the file, so don't try to add a ".java" extension to the name. The name must be a valid Java class name. Use any name you like for the class name, since you will not turn in the class. The Java file will be added to the src folder in your project, in the "default package." Furthermore, a Java editor will open, showing you the initial contents of the file. As you will see in the editor window, Eclipse has already added the declaration of the class. All you have to do is fill it in! Note that you can close the editor in the usual way by clicking the little ×. To open the file again, double-click its name in the "Project Explorer."

Eclipse often has alternative ways of doing things. Another way to create a class is with a button in the toolbar. Look for the group of two small buttons at the top of the Eclipse window:

buttons for creating packages and classes

Click the button on the right in this group to create a new class. (When you create a class using the button, you should first click in the Package Explorer to select the location of the class that you are creating. Otherwise, you'll have to enter the location by hand in the class creation dialog box.)

I strongly suggest that you try typing a short program into the new class, to get some experience with programming in Eclipse.

In Eclipse, you don't have to do anything to complile a program, since that is done automatically. To run a program, right-click either on the name of the class in the "Project Explorer" or on the editor window that contains the program. In the pop-up menu, go to the "Run As" submenu, and select "Java Application" from the submenu. The program will start. The output from the print statements appears in the "Console" view in the bottom right area of the Eclipse window. In order to type a response, you must first click the Console window! Type in your response and press return.

For another way to run a program, if an editor window for the program is currently the selected view, you can run the program by clicking on the "Run" button (run button) in the toolbar. If you click the little black triangle to the right of the "Run" button, you'll get a list of all the programs that you have run; select a program from this list to run it.

At this point, you might experiment more with Eclipse, or you might just go on to work on the programming assignment later in this lab.

You have surely already noticed that the Java editor in Eclipse does a certain amount of work for you. It automatically inserts indentation. When you type a "{" and press return, the matching "}" is inserted in the correct position. Furthermore, when you type a period, a list of things that can follow the period will pop up, and you can select an item from the list instead of typing it in. This is called Content Assist. You can invoke Content Assist at any time while you are typing by pressing Control-Space. If you do this in the middle of a variable name or method name, pressing Control-Space will either complete the name or offer a list of possible completions. It can be used in other ways as well. For example, if you press Control-Space after typing the "(" at the beginning of a method's parameter list, you will get information about the parameters of the method. By the way, when Content Assist pops up a list, you can get rid of the list by pressing the Escape key.

Content Assist is a good thing, but I find the way it pops up automatically while I am typing to be rather annoying. You can turn off this feature in the Eclipse Preferences. Select the "Preferences" command from the "Window" menu. In the Preferences dialog box, click the little triangle next to "Java" to open the list of Java preferences (if necessary), then click the triangle next to "Editor", and finally click on "Content Assist." In the Content Assist preferences, uncheck the box labeled "Enable Auto Activation" and click "OK".

Eclipse has a huge number of preference settings that can be used to customize the environment. Most of the default settings are OK, but there are a few that I usually change. If you want to do the same: Under "Java / Editor / Mark Occurrences", turn off "Keep marks when the selection changes". Under "Java / Compiler / Errors/Warnings / Potential Programming Problems", change "Serializable class without serialVersionUID" from Warning to Ignore, and change "Possible accidental boolean assignment", "'Switch' case fall-through" and "Null reference" from Ignore to Warning.)

Eclipse has a lot of other useful features. We will encounter more of them as time goes on, and you can undoubtedly discover a few new ones on your own. But here are a few of the most useful:

Programming Exercise: FirstEclipse.java

FirstEclipse.java will be a version of the ArithmeticQuiz program from Lab 4. This version will use subroutines. We discussed this idea in class yesterday. Some of the code for the program is already in the file, and you will write some of it yourself, and some of it can be copy-and-pasted from this web page into Eclipse.

You should have already added FirstEclipse.java to your Eclipse project. (If not, do it now!) Double-click FirstEclipse.java in the Project Explorer to open it in an Eclipse editor. (Recall that it is in the "default package" in the "src" folder under the project name.)

We need to make a lot of random integers in this program. Even though making a random integer is a short line of code, it would be nice to have a subroutine to do it, both to cut down on the amount of code and to make the code clearer and more readable. Copy the following subroutine definition into the program. Remember that it cannot go inside another subroutine. (That is, don't put it inside main().) But it must be inside the class definition. You can put it either before or after the definition of main():

 * Returns a randomly selected integer in the range min to max, inclusive.
private static int randInt(int min, int max) {
    int rand;
    rand = min + (int)( (max-min+1)*Math.random() );
    return rand;

Eclipse will mark the subroutine with a warning saying that it is not used locally. Remember that warnings are not errors. In this case, the warning will go away as you add code to the program.

Now, you can use randInt() in the program. Copy the following code into the program. It goes in main() after the comment that says, "Make up the problem, by choosing op, num1, and num2 at random.":

switch ( randInt(1,4) ) {
case 1: // Addition
    op = '+';
    num1 = randInt(0,100);
    num2 = randInt(1,40);
    answer = num1 + num2;
case 2: // Subtraction
    op = '-';
case 3: // Multiplication
    op = '+';
default: // Division
    op = '+';

As a little programming exercise, add lines to this switch statement to choose appropriate values for num1, num2, and answer in the cases of subtraction, multiplication, and division. You can use some of the same ideas that you used in your own ArithmeticQuiz program, but use the randInt() subroutine to generate the random numbers!

Next, the program has to ask the user the problem and score it. This can be done using the askProblem() subroutine that we discussed in class yesterday. Add the following code to main(), just after the comment that says, "Ask the problem, and get the user's score for this problem":

int points;  // points for this problem
points = askProblem( op, num1, num2, answer );
score += points;

This time, you will get an actual error because the subroutine askProblem() has not been defined. Let's let Eclipse help with the error... Hover the mouse over the word "askProblem", which is underlined in red because of the error. A pop-up will appear containing the quick fix, "create method addProblem(char, int, int)". Click on it, and the error will go away. An outline of the method has been added elsewhere in the program. (Remember that "method" is just another word for "subroutine" in Java.) Go to it, delete the comment that says "TODO Auto-generated method stub", and replace it with the code of the subroutine. This code is another programming exercise. Ask the user the problem specified by the parameters named op, num1, and num2, giving them a second chance if necessary, and determine their score. The return value of the subroutine should be the user's score on the problem. (By the way, if you want to use printf to ask the question, the correct format specifier for an output of type char is %c.)

That completes the program — except for adding a comment for askProblem(). Hopefully, you will find this version of the program more attractive than the version that you wrote for Lab 4!

Working on Programs from Lab 5

It would be a good idea to add your two programs from Lab 5, ArrayStats.java and Animation.java, to your Eclipse project. Add them in the same way that you added TextIO.java and FirstEclipse.java. That is, drag them or copy-and-paste them from a file browser window. The first thing that you should do with the programs is make sure that they are indented properly:

  • Open one of the files in an editor view.
  • Use the "Select All" command to select all the text in the program (Hit Control-A, or find the command in the Edit menu.)
  • Use the "Correct Indentation" command to fix the indentation of the selected text. (Hit Control-I, or find the command in the Source menu.)
  • You should find it easier to complete the programs using Eclipse than using a text editor. Don't forget to add comments!

    About Comments

    Every subroutine that you write should have a comment. The comment should come immediately before the subroutine definition, and it should start with /** and end with */. Comments that start with /** are "Javadoc" comments, and they should follow a certain format. You are not responsible for following the exact format for now, but you should still use /** for the comments on your subroutines. You should also use a /** comment for the comment on the program that goes before "public class...". Note that in Eclipse, if you click the line before a subroutine or class definition, type /**, and press return, the Eclipse will add an outline of a Javadoc-style comment.

    (Javadoc is discussed in Section 4.5.4 of the textbook. For now, you can just imitate the comments on the subroutines that were already provided in the program.)

    Turning in Your Work

    You will turn in your work as usual, using the web page at http://math.hws.edu/submit. There are three files to submit: FirstEclipse.java, ArrayStats.java, and Animation.java. The files are in your Eclipse project. The only problem is how to find the actual file on your computer.

    The file is in the Eclipse workspace that you created the first time Eclipse started up. The workspace is a directory, which is in your home directory unless you specified a different location.

    To find your file: Open the Eclipse workspace in a file browser window. Inside, you will find a folder with the same name that you used for the project in Eclipse. Inside that folder is a folder named src. And the Java files for your project are in that src folder. When you submit your files, you will have to browse to that src folder to find it.