Forte Community Edition

Forte Community Edition is a free Java environment available from Sun Microsystems. You can download Forte from http://www.sun.com/forte/ffj/ce/index.html. Forte is a pure Java program that runs on any platform that supports Java 2. Forte requires a Java 2 runtime, so you need to install the Java 2 SDK first before installing Forte. You can download the SDK from http://java.sun.com/j2se/. The Forte environment is easy to use, contains a nice debugger, and the price is right.

Starting Forte

Start Forte by clicking on the Forte Community Edition Icon. A startup screen appears, and Forte spends some time loading various modules.

{short description of image}

When Forte has finished loading, you see a toolbar at the top of the screen.

{short description of image}

You may also see one or more windows, for example a window entitled “Explorer”:

{short description of image}

Ignore those other windows for now.

Loading an existing program

If you already have your program in a Java file (or a directory containing multiple Java files), then you can simply load your program file (or, in the case of multiple files, the file containing the main method) with the File->Open File menu command. If your program doesn't start with a package command, you get a dialog like this:

{short description of image}

Accept this dialog. Your program file is now placed in an edit window:

{short description of image}

This is the best part about Forte. It is really easy to load in programs that you created in some other way, without having to fuss with projects.

Starting a new program

If you write a program from scratch, then you can start your work in Forte. It is always best to place each of your programs into a separate directory. First create the directory outside Forte, for example, with the mkdir command in a command shell.

{short description of image}

Tip: If your program uses the ConsoleReader class, copy the file ConsoleReader.java into the directory that you just created.

Select the menu command Tools->Add directory. In the resulting file dialog, type in the name of the directory.

{short description of image}

Next, select File->New from template from the menu. You get a dialog like this:

{short description of image}

Click on the “doorlatch” labeled Classes:

{short description of image}

Now select Empty and click on Next>. You get the following dialog:

{short description of image}

Replace the “ClassName” entry in the Name field with the class name that you want. Select the directory that you created for your program in the “Filesystems” tree.

{short description of image}

You get the following dialog:

{short description of image}

Select Yes (the default).

Finally, you get an editor window into which you can type your program.

{short description of image}

As you type in your program, occasionally select File->Save from the menu to save your work.

Tip: By default, Forte uses 2 spaces per tab stop. To change to 3 spaces per tab stop (which is used in the textbook), select the Tools->Options menu command. In the resulting dialog, click on the “doorlatch” next to the Editor Settings entry. Then click on the Java Editor entry. To the right, the Java editor settings appear. Click on the value next to the Number of Spaces Per Tab entry. Change it to 3 and close the dialog.

{short description of image}

Compiling a program

To compile a program in Forte, select Build->Compile from the menu. Alternatively, you can hit the F9 keyboard shortcut. If the compilation is successful, a message “Finished program name” appears in the Forte tool bar.

{short description of image}

If you made a syntax error, a window with compiler errors pops up.

{short description of image}

Double-click on an error message, and the cursor moves to the offending line in the edit window:

{short description of image}

Running a program

To run a program, select the Build->Execute menu option or hit the CTRL+F9 keyboard shortcut. The program runs. Any console output is directed to an output window:

{short description of image}

Note that the tab on the toolbar has changed from Editing to Running.

{short description of image}

Click on the Editing tab to go back to the edit window set.

Providing console input

When you run a program in Forte that requires console input from System.in, then the program stops each time you read from the console. You then need to type one line of input into the Program Input area of the output window and hit the ENTER key to continue.

{short description of image}

When the program is ready to take the next input line, it stops again, clears the Program Input area and waits for you. Type the next line and hit ENTER. Keep supplying input until the program stops.

Running applets

To run an applet, first compile the program as previously described. Then position the mouse inside the editor window of the applet file and click with the right mouse button. A property window will pop up:

{short description of image}

Click on the tab labeled Execution. Then click on the field next to the Executor entry and select Applet Execution from the drop-down list.

{short description of image}

Now select the Build->Execute menu option or hit the CTRL+F9 keyboard shortcut. A window with the applet pops up.

{short description of image}

Close the window to terminate the applet.

Tracing through a program.

To start tracing through a program, select the menu option Debug->Trace Intoor hit the F7 keyboard shortcut. If the debugger is not already running, this command starts the debugger and runs the program until the first line of the main method. For example, here is the debugger display when tracing into the Coins6 program.

{short description of image}

Whenever you select the menu option Debug->Trace Over (or the F8 keyboard shortcut), then the debugger executes one line of the program, without stepping inside method calls. For example, tracing over the call

Purse thePurse = new Purse();

will not trace inside the Purse constructor but simply run the program to the next line of the main method:

{short description of image}

Contrast that with the menu option Debug->Trace Into(or the F7 keyboard shortcut). This command traces inside method calls. For example, tracing into the line

Coin coin1 = new Coin(0.01, "pennies);

stops at the first line of the Coin constructor:

{short description of image}

Watching values

To see the value of a variable of a simple type (such as a number or a string), simply hold the mouse pointer over the variable name when the debugger is stopped. Then the contents of the variable is displayed in a small rectangle next to the variable name. For example, here is the contents of the aValue variable in the Coin constructor.

{short description of image}

To watch the value of a variable, use the Debug->Add Watch menu command or the SHIFT+F8 keyboard shortcut. A dialog like this pops up:

{short description of image}

Enter the name of the variable that you want to watch. For example, you may want to enter this to watch the implicit parameter of the current method.

To see all variables that are currently being watched, select View->Debugger Window from the menu. The watched variables are in the Watches tab.

{short description of image}

Click on the “door handle” to expand the value to its instance fields. If the instance fields themselves are of class type, you can keep expanding them. For example, here we expand the this variable:

{short description of image}

If a variable name is temporarily meaningless, then a message is displayed next to it.

{short description of image}

When you trace back into a scope where the variable name is meaningful, the value is redisplayed.

Setting breakpoints

Tracing through a program can be tedious. Often, you want the program to run at full speed until a particular line of code is encountered. To set a breakpoint at a line, click on it with the mouse, then select the Debug->Add/Remove Breakpointor hit the CTRL+F8 keyboard shortcut. The line turns blue, indicating that a breakpoint has been set.

{short description of image}

Now select the menu option Debug->Continue or hit the ALT+F5 keyboard shortcut. The program runs to the next breakpoint, stopping only for user input.

You can set as many breakpoints as you like.

To remove a breakpoint, click on it with the mouse, then select the Debug->Add/Remove Breakpointor hit the CTRL+F8 keyboard shortcut.

Stopping the debugger

When the program has completed, the debugger stops automatically. A message “Debugger finished” appears on the status line.

{short description of image}

When you want to terminate a debugging session without running the program to the end, select the menu option Debug->Finish Debugging.

Now you probably want to click on the Edit tab of the Forte toolbar to get the editing windows back.

Preparing console programs for debugging with Forte 1.0

Unfortunately, version 1.0 of Forte has a bug that prevents it from debugging programs that read from the console. To overcome this problem, you should either upgrade to Forte 1.0.1 (or higher) or modify the ConsoleReader class by following the directions below.

Add the nine boldface lines shown here to the ConsoleReader class:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import javax.swing.JOptionPane;

/** 
   A class to read strings and numbers from an input stream.
   This class is suitable for beginning Java programmers.
   It constructs the necessary buffered reader, 
   handles I/O exceptions, and converts strings to numbers.
*/

public class ConsoleReader
{  /**
      Constructs a console reader from an input stream
      such as System.in
      @param inStream an input stream 
   */
   public ConsoleReader(InputStream inStream)
   {  reader = new BufferedReader
         (new InputStreamReader(inStream)); 
   }

   /**
      Constructs a console reader that reads from an option pane
   */
   public ConsoleReader()
   {  reader = null;
   }

   /**
      Reads a line of input and converts it into an integer.
      The input line must contain nothing but an integer.
      Not even added white space is allowed.
      @return the integer that the user typed
   */
   public int readInt() 
   {  String inputString = readLine();
      int n = Integer.parseInt(inputString);
      return n;
   }
   
   /**
      Reads a line of input and converts it into a floating-
      point number. The input line must contain nothing but 
      a nunber. Not even added white space is allowed.
      @return the number that the user typed
   */
   public double readDouble() 
   {  String inputString = readLine();
      double x = Double.parseDouble(inputString);
      return x;
   }
   
   /**
      Reads a line of input. In the (unlikely) event
      of an IOException, the program terminates. 
      @return the line of input that the user typed, null
      at the end of input
   */
   public String readLine() 
   {  String inputLine = "";

      if (reader == null)
         return JOptionPane.showInputDialog("Program input:");
      try
      {  inputLine = reader.readLine();
      }
      catch(IOException e)
      {  System.out.println(e);
         System.exit(1);
      }

      return inputLine;
   }

   private BufferedReader reader; 
}

In your program, change the line

console = new ConsoleReader(System.in);

to

console = new ConsoleReader();

Note: If you make this modification, be sure to close all file systems that contain an unmodified ConsoleReader class. Select View->Explorer window from the menu, then click on each file system that doesn't belong to your program and select Tools->Remove Filesystem from the menu.