Professional Documents
Culture Documents
BlueJ is an Integrated Development Environment (IDE) for Java thats specifically designed for teaching Java at an introductory level. BlueJ is easy to install and use, it is available for free, and it runs on all modern operating systems. This tutorial has been designed to work with our beginning Java book, Murachs Beginning Java 2, JDK 5. Because of that, it doesnt teach Java. To make it easy for you to use this tutorial with our Java book, each topic in this tutorial includes references to the corresponding chapters in the book whenever thats necessary or helpful. How to get started with BlueJ
How to download and install BlueJ How to create a new project How to create a new class How to save and edit source code How to compile source code How to run an application How to open existing projects
2
2 4 4 6 8 10 12
14
14 16 18 20 22 24
26
26 28 28 28
30
30 32
Perspective
34
Notes
Although this procedure is for downloading and installing BlueJ for Windows, you can use a similar procedure for non-Windows systems. For information about installing BlueJ on other operating systems or about troubleshooting installation problems, you can refer to the documentation thats available from the BlueJ web site (see figure 16). How to download and install BlueJ
Figure 1
Figure 2
Figure 3
A compile-time error
Figure 4
10
11
The console
Figure 5
12
13
Description
To open an existing BlueJ project, select the Open Project (Ctrl+O) command from the Project menu. Then, use the resulting dialog box to select the directory that contains the project. To open an existing non-BlueJ project and convert it to a BlueJ project, select the Open Non BlueJ command from the Project menu. Then, use the dialog box shown above to select the directory that contains the files for the project, and click on the Open in BlueJ button. When you open a non-BlueJ project, BlueJ adds bluej.pkg and bluej.pkh files to the directory to identify it as a BlueJ project directory. Then, the directory will be displayed with a different icon so you can tell which projects have been converted to BlueJ projects.
Note
If you want to use BlueJ with the downloadable applications and exercises for Murachs Beginning Java 2, JDK 5, you can use the Open Non BlueJ command to open the applications and exercise starts and convert them to BlueJ projects. How to open existing projects
Figure 6
14
15
The BlueJ window for the Line Item application presented in chapter 6
Description
The dotted arrows in this diagram show the uses relationships. For example, the LineItem class uses the Product class. When you create or import a class, BlueJ automatically creates uses arrows like the ones shown in this diagram based on the code in the classes. To make the class diagram easier to read, you can drag and drop the icons for the classes to rearrange them. To remove an arrow, right-click on it and select the Remove command from the resulting menu. To add a uses arrow, click on the button with the dotted arrow, then on the class that uses another class, and then on the class thats being used by the first class. To add a new class to a project, click on the New Class button to create a new class. To import all of the classes in a directory, select the Import command from the Project menu and use the resulting dialog box to select the directory that contains the classes. To import a single class, select the Add Class From File command from the Edit menu and use the resulting dialog box to select the class. To remove a class from a project, right-click on the class and select the Remove command. How to work with a class diagram
Figure 7
16
17
Object Inspector dialog boxes for the Product and LineItem objects
Description
To create an object and place it on the object bench, right-click on the class, select a constructor of the class, and respond to the resulting dialog box. To execute a method of an object, right-click on the object and select the method. Once you respond to the resulting dialog box, the method will be executed. If a constructor or method requires parameters, BlueJ will prompt you to enter the parameters. To enter a string, enclose the string in quotes. To enter an object reference, click on an object on the object bench or manually enter the name of the object. To remove an object, right-click on the object and select the Remove command. To view the data thats stored in an object, right-click on the object and select the Inspect command. If the object refers to another object, you can display the data for that object by double-clicking on the arrow icon for the object reference. How to work with the object bench
Figure 8
18
19
Description
The solid arrows in this diagram show inheritance relationships. For example, the Book and Software classes inherit the Product class. Most of the time, BlueJ automatically creates inheritance arrows like the ones shown in this diagram when you create or import a class. To add an inheritance arrow, click on the button with the solid arrow, then click on the subclass, and then click on the superclass. To remove an inheritance arrow, right-click on the arrow and select the Remove command. How to work with inheritance
Figure 9
20
21
The BlueJ window for the Product Maintenance application presented in chapter 8
Description
The BlueJ window clearly identifies interfaces by putting <<interface>> at the top of the interface icon. In this diagram, the ProductDAO interface inherits the ProductConstants, ProductReader, and ProductWriter interfaces. To add an interface to a project, click on the New Class button. Then, enter a name for the interface and select the Interface option from the resulting dialog box (see figure 2). To remove an interface, right-click on the interface and select the Remove command. When coding a class that implements an interface, you can view the documentation for the interface by selecting the Interface option from the drop-down list at the right side of the code editors toolbar. How to work with interfaces
Figure 10
22
23
The BlueJ window for the Line Item application presented in chapter 9
Description
To add a new package to a project, you can select the New Package command from the Edit menu. This creates a subdirectory within the current directory. If you add a new class to a package, BlueJ automatically adds the necessary package statement to the class. To remove a package from a project, you can right-click on the package and select the Remove command from the resulting menu. This will delete the directory for the package and all subdirectories and classes within that directory. To navigate through existing packages, you can double-click on the package to navigate down the hierarchy, or you can double-click on the <go up> icon to navigate up the hierarchy. How to work with packages
Figure 11
24
25
Description
To generate or view the documentation for a project, you can select the Project Documentation (Ctrl+J) command from the Tools menu. If the project doesnt contain any documentation, this command will generate the documentation and display it in a browser. If the project already contains documentation, BlueJ will display a dialog box like the one shown above. Then, you can decide whether to regenerate the documentation or just view it in a browser. BlueJ stores the documentation for a project in a subdirectory named doc thats subordinate to the projects root directory. How to generate and view documentation
Figure 12
26
27
Description
A breakpoint is indicated by a small stop sign icon thats placed on the line number to the left of the line of code. To set a breakpoint for a line, open the code editor window for the class and click on the line number. This will only work for lines that contain code or braces, not for method declarations or blank lines. To remove a breakpoint, click on it. Once you have set a breakpoint, you can run the application. When the application encounters the breakpoint, the Debugger window will be displayed as shown in the next figure. BlueJ automatically removes all breakpoints when the application finishes running. How to set and remove breakpoints
Figure 13
28
29
Description
When a breakpoint is reached, program execution is stopped before the line is executed. In the code editor window, the arrow shows the line that will be executed next. In the Debugger window, the Call Sequence pane shows the stack trace, which is a list of methods in the reverse order in which they were called. You can click on any of these methods to display the method. This opens a new code editor window if necessary. The other panes in the Debugger window show the values for all variables that are currently in scope, including static variables, instance variables, and local variables. If a variable refers to an object, you can view the values for that object by double-clicking on it to display an Object Inspector dialog box (see figure 8). To single-step through code, select the Step or Step Into button. These buttons work similarly, unless the next line of code contains a method call. In that case, they work a little differently. To continue executing code until the next breakpoint, select the Continue button. To pause the application execution at its current location, select the Halt button. To end the applications execution, select the Terminate button. How to work with the Debugger window
Figure 14
30
31
Description
To add a new applet to a project, click on the New Class button. Then, enter a name for the applet and select the Applet item from the resulting dialog box (see figure 2). To run an applet, right-click on the applet and select the Run Applet command to display the Run Applet dialog box. Then, select the appropriate option to run the applet in the Applet Viewer or a web browser, enter the height and width of the applet in pixels, and select the OK button. When you do, BlueJ generates a web page for the applet and runs the applet. The name of the web page thats generated is the name of the applet class plus an extension of html. For example, the web page for the applet shown in this figure will be stored in a file named FutureValueApplet.html. How to work with applets
Figure 15
32
33
Description
The BlueJ Tutorial shows you how to work with BlueJs basic features, including how to install BlueJ on Macintosh, Linux, and other systems. The BlueJ Reference Manual contains more comprehensive information about working with BlueJ. You can also display the BlueJ Tutorial by selecting the BlueJ Tutorial command from BlueJs Help menu. How to view and download the BlueJ documentation
Figure 16
34
Perspective
In this tutorial, you learned how to use BlueJ to enter, edit, compile, and run a program. You learned how to use BlueJ to work with object-oriented features such as inheritance and interfaces. And you learned how to use BlueJ to debug applications and run applets. These are all of the skills you need to use BlueJ with our book, Murachs Beginning Java 2, JDK 5.
Summary
BlueJ is an Integrated Development Environment (IDE) for Java thats specifically designed for teaching Java at an introductory level. BlueJ is easy to install and use, it is available for free from the BlueJ web site (www.bluej.org), and it runs on all modern operating systems. BlueJ automatically maintains diagrams that show the relationships between the classes in a project. These diagrams are based on the Unified Modeling Language (UML) standard thats used for object-oriented analysis and design, and they include arrows that show uses relationships and arrows that show inheritance relationships. BlueJ includes an object bench that allows you to test and inspect the objects that your project uses. BlueJ includes an object inspector that allows you to view the values that are stored in an object. BlueJ includes a debugger that can help you find and fix any bugs in your applications. To use the debugger, you can set a breakpoint to stop program execution. Then, you can step through the statements in your applications and view the values of variables and the stack trace. The documentation thats available from the BlueJ web site contains additional information about working with BlueJ, including information about installing BlueJ on Macintosh, Linux, and other systems.
35
Exercise 1
Before you do this exercise, you should follow the procedures shown in chapter 1 (figures 1-4 through 1-7) of Murachs Beginning Java 2, JDK 5 to install and configure the JDK. Then, you should follow the procedure in figure 1 of this tutorial to install BlueJ. In addition, we recommend that you download the directories and files for our book from our web site (www.murach.com) and install them on your C drive. For complete instructions, please refer to appendix A of the book. This exercise will guide you through the process of using BlueJ to enter, save, compile, and run a simple application.
5. Use the Save command (Ctrl+S) in the Class menu to save your changes, and then close the code editor window.
36
7. Right-click on the TestApp icon and select the main method from the resulting menu. Then, respond to the resulting dialog box to run the application. It should display a console that says This Java application has run successfully. 8. Close the console window.