Algorithms and OOD (CSC 207 2013F) : 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.

$ eclipse & 

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

Alternately, you can start Eclipse by finding the appropriate menu item in the Applications menu, which looks like a little creature over a blue X under Xfce4.

Exercises

Exercise 1: Your First Program

a. Make a new 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.

c. Create a class in the package called HelloJava. If you are not sure how to create a new class, use File > New > Class.

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) {
       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 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.

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). What about +? 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 trying to follow the standard Sun (now Oracle) Java Code Conventions. Eclipse can help if you configure it correctly.

First, select Window > Preferences.

Next, select Java > Code Style > Formatter.

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

Finally, save the new preferences.

Select all of your code and hit Ctrl-I

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 Java Conventions [built-in] 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.

Copyright (c) 2013 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.