working with Eclipse
Once you have snarfed your first project you can begin work on it in Eclipse. Eclipse is an extremely versitile IDE (Integrated Development Environment) that can help you quickly and easily build your programs. In order to take advantage of all of its features you should first familiarize yourself with the workbench. By default, Eclipse should open in the Java perspective (which is confirmed by the Java perspective icon in the top right corner). This perspective is best suited for working with Java and Java programs.
elements of the workbench
There are six main parts that are of concern for you:
- The Menubar at the top of the window
- It includes the Ambient menu, which allows you too access all of Ambient's features. This menu changes depending on what you are doing in a particular moment.
- The Toolbar underneath the Menubar
- It provides shortcuts to functions in the menubar that have been symbolized with icons. Howevring your icon over an icon should display a tip describing its function.
- Package Explorer on the left side
- Displays all projects and files in your workspace.
- Outline view on the right side
- Displays the structure (classes, methods, and variables) of the file you are currently working on.
- The Editor in the main part of the screen
- Lets you work on files, edit code, fix errors, etc.
- Console window on the bottom
- Will display output and allow for input when you run programs.
using the package explorer
At first, you should only see one object (the project you snarfed earlier) with a little plus sign in front of it. Click on the plus sign to expand the object and see the objects it contains. Do this to all children until the view is completely expanded. You should now see that there are some classes listed in the default package. Notice also that you can see an outline of the class. All data members and methods are displayed. In order to open a class or a specific method, simply double-click on its name.
The entry named JRE System Library represents that Java library you are currently using (make sure you are using the right version for the project and class you are in - if in doubt ask your professor or TA).
using the java editor
Once you open a file the Java Editor will display its contents. Using the editor is just like using any text editor. However, there are some additional features that should help you write programs.
- Syntax highlighting
By default, the editor analyzes your code and highlights and marks certain elements to make the code easier to read. For example, it highlights keywords in purple, comments in light blue or light green, and strings in blue.
- Error detection and warnings
When the editor finds an expression that it does not understand or cannot evaluate, it will underline it with a red line. You might not be able to run a program before all errors are fixed. If the editor thinks a line is unnecessary or could potentially cause problems, it will underline it in yellow.
Usually, when the editor detects an error (or some other problem announced with a warning), it will also attempt to present you with a solution. To do so, the editor will display a yellow light blub on the left edge of the line in which the error occured. Clicking on the bulb once will open a menu that will offer you a list of possible solutions (see the image above). Selecting one of these solutions will immediately alter your code (however, you can always undo the changes). Alternatively, you can display that list by pressing Ctrl + 1 when the cursor is on the offending piece of code.
- Using auto-completion features
Eclipse can automatically guess what you are trying to do and offer to finish the task for you. To see an example of this:
- Type a keyword or part of a keyword and press Ctrl + Space. Eclipse will show you a list of elements that you may wish to use. For example, if you type for and press the two keys simultaneously, it will show you a list of predefined for loops. Selecting one of them will insert the loop into your code.
- When using member methods or data members of classes, we use the dot operator in Java (e.g., MyClass.printHelloWorld()). Oftentimes we know that there is a method we would like to use, but we cannot remember its exact name. If you are not sure, simple type the name of the object you are working followed by a period and wait for a moment. A list will appear that shows all members that you may use.
- Code Refactoring
Eclipse allows you to easily perform tasks that can be quite cumbersome. For example, you can tell Eclipse to rename a class, method, or variable and it will change all references to that element in all files in your entire project! Simply place the cursor on the element you want changed, right-click on it and select Refactor > Rename.
- Code Formatting
Eclipse also allows you to easily format your code in a clear, readable way. To format the code of the file you are currently editing, go to the Menubar and select Source > Format. You might also find the Organize Imports function useful.