Lab 10: Thursday September 2nd

Eclipse



Getting Started with Eclipse

At home: Go to www.eclipse.org. Download and install eclipse following the directions you will find there.
Your computer must have java installed. The eclipse website will tell you which Java version you must have.
If you do not have the correct version installed, go to www.sun.com and find and install the appropriate version of Java.

In lab: Eclipse is started by running '/opt/eclipse/eclipse-3.0/eclipse &' at the command line

Running Eclipse

When you run Eclipse, especially the for the first time, you may be prompted to specify a workspace for Eclipse.
This is the directory where Eclipse will save your files. Eclipse will suggest a default workspace, either accept this choice or specify the directory you want eclipse to use.

When first run, Eclipse will often present you with a Welcome screen. This screen provides access to tutorials and sample files. Feel free to work through the Java development tutorial instead of the guidelines presented here. Afterwards, work through the portions of this lab that were not covered by the tutorial.
If you do not see a Welcome screen, you can manually call it up from the Help menu, the welcome screen is the first entry.

Creating a new class

Before you are able to create a class, Eclipse will want you to create a new project. A project is an eclipse notion. The project will group the files you are currently working on into one area within the workspace.

Select the new project option from the File menu.
When the project wizard appears, provide a name that you like (possibly lab10) and select Finish to create the project.
Now we're ready to create a class.

Select the new class option from the File menu.
Name the class Addr.
Notice the portion of the dialog window that will allow you to set the parent class and any implementing interfaces.
Click the box next to create a method stub for public static void main. Click Finish to generate the class.

Implementing our class

Eclipse has opened a class editor for our class, where some of the class has been implemented for us.
Notice that a main method has been provided, with nothing in the body.

Add a public method to Addr that accepts three ints and returns the result of adding these numbers together.

To experiment with our method, we need to add content to the body of main.

First, we will need to create an instance of Addr within main, so that we may call the non-static method we have written.
Remember, an instance is the result of saying new Addr(...).
Provide a name for the Addr, my suggestion is to call it myAddr.

Call the adding method of myAddr with the numbers 4, 6, and 10.

To see the result of this, we will need to use System.out.println. Place the call to myAddr's adding method within the parentheses of System.out.println.

This will cause the returned value to be displayed when the program is run.

Save your file (this is a step you should repeat often to avoid loss of work).

Running our program

To run our program, and see the result of adding 4, 6 and 10, first look on the right of your screen. You should see a sub window named outline.

Right click on the green C next to Addr in this portion of the eclipse window.
From the menu that appears, select 'Run as' and then 'Java application'

A new window will appear in the bottom center portion of the eclipse window, named console. You should see the number 20 displayed in this window. This is the result of running our program.

Making Addr interactive

Our current implementation of Addr isn't very interesting. We should add the ability for the human user to add any three numbers of their choice when running our program.

Java provides a notion of accepting additional information from the user on the command line. The Java system reads this information as a String, and packages any values up into an array to pass to the main method of the class.

To make Addr interactive, we will expect the user to provide three numbers on the command line, and then add them together.

If the user does not provide three numbers, we should nicely inform them of their mistake.

As mentioned above, Java will package any user input into a String array to provide to the program. This information is in the args parameter to main.

To begin making Addr interactive, we should check that the length of args given to main is at least 3.

If the length is at least three, we want to add the three numbers together. However, Java has presented us with three Strings.

Therefore, we must convert these Strings into ints if we are going to add them.

You should use the following Java code to do this:
Integer.valueOf( args[XX] ).intValue()
Replace XX with the appropriate position in args (i.e. 0, 1, 2)

The first method is a static method of the class Integer. It has the following purpose and header:
// To convert s, which is a String representation of a number, into an Integer
public static Integer valueOf( String s )

The second is a non-static method of the class Integer, with the following purpose and header:
// To retrieve the int value that is associated with this Integer
public int intValue()

After we convert out Strings into ints, we can use the converted values to call our adding method.
Report the value of this call using System.out.println

When the length of args is less than 3, we should tell the user of their mistake.
Do this by reporting the situation using System.out.println and a String of your choosing

Save your file.  And run it as we did before.

Entering values for our program

Looking at the console window, we now see the message about not providing sufficient arguments. Let's rectify that.

Look for a button with the arrow pointing to the right surrounded by a big green circle. Notice the smaller arrow pointing down to the right of this button. Click on the smaller arrow.

This should present you with a menu. Select the Run option from this menu. This should cause a new window to appear.

In this window, select the arguments tab. This tab will allow you to set arguments for your program.

Enter any three ints into the program arguments field. Separate your numbers with spaces.

Click on the run button at the bottom of the window.

The result of adding your three numbers should now be visible in the console window.

Seeing certain error messages

ProfessorJ and eclipse use different Java compilers. Therefore, the error messages you will see have now changed. The messages you are accustomed to seeing were tailored for novice programmers. The messages you will see from Java and eclipse are tailored for general programmers.

To explain a few common error situations, enter the following erroneous code fragments into your program, then run. The text in the right portion of the following table explains what has gone wrong in your program.

null.toString()
The error message you see indicates that you have attempted to access a method on a null value. Remember, null values do not have methods. In general when you see this error, it is likely that you forgot to initialize one of your fields. All fields should be initialized before you may use them.
args[1000]
The error message you see indicates that you have attempted to access a position in the array that does not exist. In general when you see this error, you have not checked that your parameter to access the array is less than the length of the array.
Integer.valueOf( "Howdy")
The error message you see indicates that you are trying to convert a String into an Integer, where the String does not contain just an int. In general, this error might indicate that you have confused the order of your outside arguments to the program.


Additional problem

Take our WeeklySchedule program from lecture. Put this program into Eclipse.
Add a method to the WeeklySchedule that will insert a new appointment into the WeeklySchedule.
Add a method, using System.out.println, to inform a human user of their appointments for the week.
Add a main method to WeeklySchedule that will populate the schedule with a variety of appointments. Then call the method to display these appointments to a human user.