Algorithms and OOD (CSC 207 2014F) : Labs

Getting Started with Eclipse


Summary: In this laboratory, you will start using Eclipse. In particular, you will ccreate your first project in Eclipse.

Preparation

a. Make sure you have completed and understood the reading on IDEs and Eclipse.

b. Start Eclipse by typing the following in a terminal window.

$ /glimmer/bin/eclipse & 

You may recall that the ampersand means that the program will run in the background and you can continue using the terminal window.

We are using the version of Eclipse in /glimmer/bin because it is much newer than the version of Eclipse shipped with Debian.

You may also find it usful to create a startup item for Eclipse.

Exercises

Exercise 1: Your First Program

a. Make a new Java project in Eclipse and call it HelloEclipse. From the File menu, select New, and then Java Project.

b. Create a package in the class that will hold your class files. Name your package introduction. If you are not sure how to create a new package, use File > New > Package. (You may also find it useful to right click on the project and use New > Package.)

c. Create a class in the package called HelloJava. If you are not sure how to create a new class, use File > New > Class. (You may also find it useful to right click on the package and choose the obvious menu items.)

d. Copy and paste this code into your class. Make sure that you use a package that corresponds to the one you just created.

package introduction;

/**
 * A simple introductory Java class.
 */
public class HelloJava 
{
  /**
   * Print a silly message.
   */
  public static void main (String[] args) 
    throws Exception
  {
    System.out.print ("I am the 1337 h4x0r. Phear me!");
  } // main (String[])
} // class HelloJava

e. Run the program and marvel at the words that appear in the console!

Exercise 2: Switching Packages

a. Create a new package named edu.grinnell.csc207.username.intro. (You should be using package names like this, so you might as well get used to it.)

b. Drag HelloJava from one package to another in the Package Explorer.

c. Observe what happens.

Exercise 3: Experiments with Output

You've already seen that Java looks a bit like C, except that it's also a bit more verbose. For example, instead of printf with a pattern, you use System.out.println. (In a few days, you will stop using System.out and explore other ways to do output.)

a. Determine what happens if you have multiple calls to System.out.println.

b. Determine what happens if you use println with an integer or real number rather than a string.

c. Determine what happens if you use System.out.print rather than System.out.println.

d. Determine what happens when you try to print System.out. That is, determine the result of

    System.out.println(System.out);

Exercise 4: Your Second Program

a. Make another class in your fully qualified package and call it SimpleMath

b. Using the skeleton from above write a program that computes and prints out a simple sum. For example, your output might be

Adding 3 and 4 gives us 7.

Here's a sketch.

    int x = 3;
    int y = 4;
    System.out.print("Adding ");
    System.out.print(x);
    ...
    System.out.print(x+y);
    System.out.println();

Presumably, you wrote this program using a sequence of calls to System.out.print and System.out.println. Can we pack it into a single instruction?

c. Java “overloads” the + operation. If you combine two strings with +, you will get a string. For example, determine the result of the following instruction.

    System.out.println("Hello" + "World");

d. We saw earlier that System.out.println accepts things other than strings, such as integers and reals (and even System.out). Can + also take different types of parameters? Determine what happens if you combine a string and an integer with +. What about combining an integer and a string?

e. Rewrite your instructions to print a sum using just one call to System.out.println.

Exercise 5: Configuring Eclipse

In this course, we will be using a variant of the GNU C code conventions, adapted for Java. Of course, one could also use other conventions, such as the standard Sun (now Oracle) Java Code Conventions. Eclipse can help if you configure it correctly. We'll explore each.

First, select Window > Preferences.

Next, select Java > Code Style > Formatter.

Next, select Java Conventions [built-in] under Active Profile.

Finally, save the new preferences.

Hit Shift-Ctrl-F.

But those are the Java code conventions. I find them painful. So let's use the course conventions. (Note: those conventions are under development, so I may ask you to update the version you use.)

Select Window > Preferences again.

Click the Import ... button

Navigate to /home/rebelsky/share and select CSC207.xml.

Follow the natural steps.

Hit Shift-Ctrl-F.

Exercise 6: Hovering

a. Hover the cursor over one of the instances of System and note what happens.

b. Hover the cursor over one of the instances of out and note what happens.

c. Hover the cursor over one of the instances of print or println and observe what happens.

d. Hover the cursor over main or HelloJava and observe what happens.

For Those With Extra Time

Extra 1: Code Preferences

In one of the exercises, you configured Eclipse to format code in a certain way. Eclipse is highly configurable, so you might explore other preferences. Go back to the Formatter preferences and click New.... In the dialog box that appears, select the course conventions as the initialization profile and then name your preferences. Click OK and then explore the various preferences that you can set.

Extra 2: Refactoring

You'll note that Eclipse has a Refactor. Figure out what at least one item in that menu does.