OO Programming with Java

RAD 7 How-To

Creating a Workspace 2

Working with the Java Perspective 4

Creating a Java Project 6

Creating a Package 9

Creating a Class 11

Creating an Interface 15

Working with the Source Code Editor 16

Automatically Generating Getters and Setters 20

Executing a Class’s Main Method 23

Using the Debugger 26

© 2007 LearnQuest v2 RAD 7 How-To Page 1 of 28

OO Programming with Java

RAD 7 How-To

Overview

RAD is an IBM product based on the open source Eclipse project. Eclipse is not a Java specific IDE; it contains support for many other languages. Eclipse is easily extensible via plug-ins. RAD includes many useful plug-ins above those that are provided with Eclipse.

I.  Creating a Workspace

1.  Click on Start > All Programs > IBM Rational Software Development Platform > IBM Rational Application Developer > IBM Rational Application Developer. After the RAD splash screen you will see the workspace launcher dialog. For workspace, type c:\RADWorkspace and press the OK button:

The work space is simply a directory where all your source code and compiled code will be stored. Each Java project you create in the workspace will be stored in its own directory under the work space directory.

2.  The Welcome view will appear. Notice that it is tabbed and that there is a small X next to the word “Welcome” in the tab. Dismiss this screen by pressing the X:

II.  Working with the Java Perspective

RAD helps you organize your work through the use of perspectives. A perspective is a collection of windows – many of which are tabbed – called views. Each perspective offers a set of views that are organized around a specific type of work. In this class we will be working exclusively with the Java perspective but you should be aware that other perspectives exist.
To switch to the Java perspective:

1.  Locate the perspective button in the upper right-hand portion of your RAD window:

2.  Press this button and a menu of perspectives will appear:

3.  Select Java and RAD will switch to the Java perspective:

Notice that next to the perspectives button, a new button – for the Java perspective – has appeared. It is depressed because you are currently viewing the Java perspective. If you select another perspective, the Java perspective will remain open so that you can switch back to it quickly by pressing the Java button.
On the left-hand side of the screen you will see the Package Explorer tab. When you start creating Java projects, they will appear here as directories.
The gray area in the middle of the screen is where your source code will appear. This area is tabbed and you can have as many source code files open at the same time as you need.
In the lower right-hand portion of the screen you will see three tabs: Problems, Javadoc and Declaration. As you create source code, RAD automatically runs it through the compiler for you and displays any problems it finds in this view. We will explain the other two views later.

III.  Creating a Java Project

1.  RAD supports many different types of projects. In this class all our projects will be basic Java projects. Here is how to create one:
Select File >New >Project from RAD’s main menu bar:

2.  The New Project dialog will appear. Expand the Java folder and select Java Project within the folder:



Select Next.

3.  Fill in the project name and select Next:



Notice that on this screen you can change the Java compiler you are using.
You can also elect to have separate source code and compiled code folders.

4.  The Java Settings dialogue appears:



Each of these tabs allows you to configure a different advanced aspect of your project. We will not need these tabs in this class. If you are curious, your instructor can provide more information on the function of these tabs.
Select Finish and your project will be generated.

IV. Creating a Package

1.  Highlight your project by selecting it. Then, while it is highlighted, right click over the project.

2.  In the pop-up menu that appears, select New >Package:




2. The New Package dialogue appears. Fill in a name for your package and select Finish:



3.  Your new package will appear under your project in the Package Explorer view.



Notice that the package icon to the left of the package name is white. This is because there is nothing in the package. After you’ve added things (classes, interfaces) to the package it will change color to indicate that there is something in the package.



V.  Creating a Class

1.  Highlight the package in which you wish to place the new class. While the package is highlighted, right click and in the pop-up menu that appears, select New > Class:



2.  The New Class dialogue will appear. This is a very useful dialogue. Take a few moments to examine its various options.



Fill in the class name. You do not need to append .java as RAD will assume it.
If you wish your class to extend from any class other than Object, select the Browse button next to the Superclass entry field.

The Superclass Selection dialogue will appear.


It might seem that you can only select java.lang.Object. But as you start typing a class name, the “Matching Types” text area will fill in. In the image below, we’ve typed the word Array:


Select the class you want to extend from and select OK.
The same process applies to implementing Interfaces. Select the Add button next to the Interfaces text area, and the Interface Selector dialogue will appear.
In the lower part of the New Class dialogue, you can choose to have a main method and/or a few other basic methods from superclasses.

3.  Select Finish and your new class will be generated.

VI. Creating an Interface

Creating an interface is very similar to creating a class.

1.  Highlight the package you want to pace the interface in. While the package is highlighted, right click, and select New->Interface from the pop-up menu that appears. The New Interface dialogue will appear:



2.  Fill in the name, and add any extended interfaces using the Add button.

3.  Click Finish and your interface will be generated.

VII. Working with the Source Code Editor

To edit your source code click on the source code file name in the Project Explorer. The file will open to the right.



Click your mouse inside the editor view and you can start entering code. Here are some helpful pointers for working with the editor.
1. Cursor Position and Line Numbers
The position of the cursor is displayed at the bottom of the screen. The first number is the line (row) number. The second number is the number of characters from the beginning of the line.
2. Dealing with Errors
When you make an error, RAD will automatically underline the problematic code and place a red circle with a white X in it to the left of the line. RAD will try to create a list of possible corrections. If it has any – and it often does – there will be a small yellow light bulb next to the red circle.

If we mouse over the red circle, an explanation of the error will appear:


If we single-click the light bulb, a list of possible corrections appears:



As you mouse over the suggestions on the right a demonstration of the code to be changed is displayed on the left. Highlight an item in the right and press the enter key, to select a correction.
A list of problems also appears in the Problems view. You can double-click any of these problems to be taken directly to the source-code line in error.
3. Using Templates
Java source code tends to be bulky, so RAD includes a useful collection of short cuts. For example if you type sysout and then type [Ctl] + [Space Bar], RAD will convert sysout to System.out.println.


There are many of these templates, and you can create your own. To view them all, Select Window >Preferences… from RAD’s main menu. The Preferences dialogue will appear. In the left-hand side of the dialogue, expand the Java node. Within the Java node, expand the editor node and then select Templates. You will see this screen:




Scroll down through the listed templates until you find sysout. Highlight sysout:

In the Preview text area you will see the code that will be generated when you type sysout and then press [Ctl] + [Space]. The cursor will be placed at the end of the line where the ${cursor} expression occurs.
More information on templates is available in RAD’s built-in help. Select Window->Search and search for “Code templates”.

VIII. Automatically Generating Getters and Setters

Java programs make frequent use of private attributes which can only be accessed via two methods (the “getter” and “setter”) which are created for just that purpose.
This technique is so common that RAD provides a short cut which will automatically generate get and set methods for any or all attributes in a class.
In this how-to we will use a simple class (MyExampleClass.java) to demonstrate this technique:



To generate the getters and setters, right click in a blank area of the editor, and select Source >Generate Getters and Setters:




The following dialogue will appear:



All your attributes are listed here; you can select all attributes or only some. You can generate only setters, only getters, et cetera.


When you’re done, click OK and your methods will be written for you:




IX. Executing a Class’s Main Method

Java systems can have hundreds of classes and any one of them, or even all of them, can have a main method.

When testing a program form inside RAD, you will need to tell RAD which main method you want to execute. This is done through the Run dialogue.

1.  Select Run >Run… from the main menu bar. The Run dialogue will appear:

2.  Select Java Application from the left-hand side and press the New button:



Fill in a name for this run configuration. In the image above, we’ve defaulted to “New_configuration”.
If your project is not selected, press the Browse button to find it. Once you have the correct project, press the Search button to find all the main methods in the project. The Choose Main Type dialogue appears:



In this painfully simple example, only one class in our project has a main method. If there had been other mains, they’d have been listed here.
Select the class you want to run and press OK.
Back in the Run dialogue, press Run in the lower right corner of the dialogue.
The console window:
All your print statements as well as any errors during execution will appear in the console view. If this view doesn’t appear in your Java perspective, select Window Show View >Console to open it.

X. Using the Debugger

The debugger allows you to execute a program one line at a time, pausing after each line to examine the attributes in your objects.
Before running the debugger you must create break points – places where execution will pause – in your source code.
When you begin a debugging session, RAD will ask you if you want to switch to the debugging perspective. Obviously, you will want to do so!
We will explain the various parts of the Debugger in the following example.

1.  To create a breakpoint in your source code, simply double-click in the area just to the left of the line at which you want to pause. Do this in the source code editor. A small blue disk will appear, indicating the presence of a breakpoint.

2.  To begin your debugging session, select Run >Debug from the main menu bar. The debugging dialogue works the same way as the run dialogue. Set up the main method you want to debug here.

3.  When you start the debugger, you will be asked if you want to switch to the debugging perspective. Say yes.


An image of the debugging perspective:


The Debug view shows technical information about the execution stack. It also contains the controls for the session:



You can mouse over each button to discover its name.


The Step Over button will execute a method without stepping you through its code block.


The Step Into button will let you execute a method line by line:

·  if the code is in another file, RAD will open the file for you.

·  if you attempt to step into a method for which you do not have the source code, RAD will display a window asking you to locate the source code. It is usually best to close this window and step over until you get back to your source code.

·  In the center of the screen your source code is displayed, with the console displayed below.

·  In the upper right, the variables window will display your attributes.

4.  As you step through your code, you can use the variable window to examine your attributes. Objects will be displayed as nodes. You can expand these to discover the object’s contents.

© 2007 LearnQuest v2 RAD 7 How-To Page 1 of 28