Table Of ContentProgramming with
VisualAge for Java
The VisualAge Series
Bitterer, Brassard, Nadal, and Wong
VisualAge and Transaction Processing in a Client/Server Environment
Bitterer, Hamada, Oosthuizen, Porciello, and Rambek
AS/400 Application Development with VisualAge for Smalltalk
Bitterer and Carrel-Billiard
World Wide Web Programming: VisualAge for C++ and Smalltalk
Carrel-Billiard and Akerley
Programming with VisualAge for Java
Carrel-Billiard, Jakab, Mauny, and Vetter
Object-Oriented Application Development with VisualAge for C++ for OS/2
Carrel-Billiard, Friess, and Mauny
Programming with VisualAge for C++ for Windows
Fang, Chu, and Weyerhäuser
VisualAge for Smalltalk and SOMobjects: Developing Distributed Object Applications
Fang, Guyet, Haven, Vilmi, and Eckmann
VisualAge for Smalltalk Distributed: Developing Distributed Object Applications
Nilsson and Jakab
VisualAge for C++: Visual Programmer’s Handbook
Programming with
VisualAge for Java
Marc Carrel-Billiard
John Akerley
INTERNATIONAL TECHNICAL SUPPORT ORGANIZATION
SAN JOSE, CALIFORNIA 95120
PRENTICE HALL PTR
UPPER SADDLE RIVER, NEW JERSEY 07458
(cid:211) Copyright International Business Machines Corporation 1998. All rights reserved.
Note to U.S. Government Users — Documentation related to restricted rights — Use, duplication, or disclosure is subject to
restrictions set forth in GSA ADP Schedule Contract with IBM Corp.
This edition applies to Version 1.0 of the VisualAge for Java product set, and to all subsequent releases and modifications
until otherwise indicated in new editions.
Comments about ITSO Technical Bulletins may be addressed to:
IBM Corporation ITSO, 471/80-E2, 650 Harry Road, San Jose, California 95120-6099
For information about redbooks:
http://www.redbooks.ibm.com/redbooks
Send comments to:
redbooks@vnet.ibm.com
Published by Prentice Hall PTR
Prentice-Hall, Inc.
A Simon & Schuster Company
Upper Saddle River, NJ 07458
Acquisitions Editor: Michael E. Meehan
Manufacturing Manager: Alexis R. Heydt
Cover Design: Andreas Bitterer
Copy Editor: Maggie Cutler
Production Supervision: Craig Little
The publisher offers discounts on this book when ordered in bulk quantities. For more information, contact:
Corporate Sales Department, Prentice Hall PTR, One Lake Street, Upper Saddle River, NJ 07458
Phone: 800-382-3419; FAX: 201-236-7141; E-mail (Internet): corpsales@prenhall.com
For book and bookstore information
http://www.prenhall.com
Printed in the United States of America
1 0 9 8 7 6 5 4 3 2 1
ISBN 0-13-911371-1
Prentice-Hall International (UK) Limited, London
Prentice-Hall of Australia Pty. Limited, Sydney
Prentice-Hall Canada Inc., Toronto
Prentice-Hall Hispanoamericana, S.A., Mexico
Prentice-Hall of India Private Limited, New Delhi
Prentice-Hall of Japan, Inc., Tokyo
Simon & Schuster Asia Pte. Ltd., Singapore
Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro
To my family: Dominique, Thomas, and Fanny.
Marc
To Jasna for writing and editing insight and, more importantly,
support.
John
Foreword
When we set about to develop a VisualAge programming environ-
ment for Java, we first needed to understand the kinds of applica-
tions that businesses wanted to build, applications that could
leverage the capability that Java brought to the Web. We discov-
ered that businesses wanted to do more than add graphical appeal
to their web sites, they wanted to extend their enterprise reach to
the web. More than Web advertising and simple CGI scripts, busi-
nesses wanted to create real applications that leveraged their
existing environments and data. VisualAge for Java accepts this
business goal and is making it a reality today for our customers,
large and small.
The development environment itself is object-based and tied
closely to the dynamic nature of Java to support incremental com-
pilation and debug, plus automatic version control down to the
method level. In this book you will learn all about VisualAge for
Java, get the Java basics, work through real programming exam-
ples that explain the Java programming language, JavaBeans,
user interface programming and explore visual programming with
the VisualAge visual composition editor. Developing applets and
JavaBeans through visual programming and code generation
make this a powerful rapid application development environment
for Java.
Marc and John are recognized by the development team as con-
tributors to VisualAge. Their work on this book will enhance your
Java programming skills and your experience with VisualAge for
Java.
The CD-ROM that accompanies the book contains VisualAge for
Java Entry version. This version is a full function edition of the
product that is only limited by the total number of classes you can
create. Along with this book I am sure VisualAge for Java Entry
will launch you into the exciting world of Java and the Internet.
Install it on your own system and give it a try.
Welcome to the world of VisualAge!
Paul Buck
Solution Manager - IBM VisualAge for Java
IBM Corporation
vi
Preface
Welcome to the world of visual programming with Java! With
VisualAge for Java, you are ready to take the plunge into a radi-
cally new way of programming. If you have just purchased your
IBM VisualAge for Java and are dying to build your first applica-
tion or applet, you are reading the right book. Indeed, learning
VisualAge for Java by example is this book is all about. With Visu-
alAge for Java, application construction has never been easier.
Even the most complex applications can be constructed from the
large set of predefined Java beans. This book, the first in the Visu-
alAge for Java series, shows you how to get started with IBM Visu-
alAge for Java.
What Is Java?
Less than a year ago Java “spilled” into the world of network com-
puting (Coffee-related metaphor is a big part of the Java scene!).
An object-oriented programming language, Java was designed
from the ground up as a secure, portable, and efficient language
for programming appliances and personal digital assistants
(PDAs). Java did not quite take off in this market place however,
and for this reason was repurposed for the Web by Sun Microsys-
tems. If you are reading this book this means that you care about
Java and that is good because Java may change the Web pro-
foundly as well as the programming of client/server applications.
Who Should Read This Book?
This book is best used as a starting point for learning Java in the
VisualAge for Java environment. If you do not know object-ori-
ented programming, this book will help you get started with a
powerful way of developing your applications. For readers who
already have some knowledge of Java, this book has been orga-
nized such that you can easily find the parts that describe how to
use VisualAge for Java.
Let’s Get Practical
This book is not a complete Java reference book. It does not have
complete descriptions and usage examples of all Java packages.
Instead, this book concentrates on how to use VisualAge for Java
to build Java classes and beans. If you are looking for a practical
vii
How This Book Is Organized
guide on how to program Java with VisualAge for Java, look no
further, you are holding the book you need. Through 15 chapters,
this book takes you on a Java journey. During the trip, you design
and program an ATM applet from the ground up, modeling and
implementing its logic using object-oriented principles, sketching
its view or user interface on the chalkboard, and building it visu-
ally with the Visual Composition Editor. The trip ends with you
assembling the application’s model and view to complete your final
applet.
How This Book Is Organized
This book consists of three parts. In the first part (Chapters 1
through 9) we introduce the Java language and the object-oriented
concepts and principles you need to know to program in Java. We
also introduce the IBM VisualAge for Java integrated develop-
ment environment (IDE), which you learn to use in each chapter.
In the second part (Chapters 10 through 12), we introduce the Jav-
aBeans specification, which describes the Java component soft-
ware that IBM VisualAge for Java uses to support the IBM award-
winning construction from parts paradigm. You learn about visual
programming in Java through the Visual Composition Editor of
VisualAge for Java. In the last part (Chapters 13 through 15), we
show you how to improve and publish your applet. We conclude
with a short overview of the Enterprise edition of IBM VisualAge
for Java.
In this book each chapter is divided in two parts, the first concen-
trating on Java as a language, and the second concentrating on
how to implement what you have learned, using VisualAge for
Java. Here is an overview of each chapter:
q Chapter 1, “Introduction to the Environment”
The first chapter is for those who cannot wait! We quickly
introduce VisualAge for Java, and in just a few steps you are
able to build your first applets and applications.
q Chapter 2, “Java Basics”
If you are new to the Java language, read this chapter for a
crash course on the language and its syntax. In the second
part of this chapter you explore the VisualAge for Java IDE
and learn how to test your code in the Scrapbook.
q Chapter 3, “Objects and the Java Language”
This chapter introduces you to objects and shows you how you
can use objects in Java. You learn your first object-oriented
concepts, such as objects, classes, interfaces, messages, and
encapsulation. To illustrate the new concepts, you build your
viii VisualAge for Java
How This Book Is Organized
first class, a bank account class, using the VisualAge for Java
SmartGuides. You refine the bank account class throughout
the book.
q Chapter 4, “Organizing Your Code”
Well-organized code can save you time and prevent headaches!
This chapter introduces you to Java packages and VisualAge
projects which help you organize your code. You also learn to
use SmartGuides to import and export code to and from the
VisualAge for Java IDE.
q Chapter 5, “Lifecycle of Java Objects”
To exist, objects must be created first. In this chapter you
learn how to create objects and how to get rid of them when
you no longer need them. Using the VisualAge for Java Smart-
Guides and the Scrapbook, you can put into practice what you
have learned.
q Chapter 6, “Reuse in Java”
If you want to grasp the key concepts of object-oriented pro-
gramming, this chapter is your open Sesame! Using the ATM
example, you learn how to achieve code reuse, using inherit-
ance and aggregation relationships between objects. You also
learn how polymorphism can help you write better code. In the
second part of this chapter, you use VisualAge for Java to cre-
ate more classes and expand the ATM model.
q Chapter 7, “Error Handling and Debugging”
Run-time errors can occur unexpectedly, and handling them
can be a very tedious task when developing an application.
Fortunately Java has an innovative approach that enables you
to cleanly incorporate error handling from the start in your
code. This chapter describes that approach and introduces you
to the powerful VisualAge for Java debugger, which helps you
track even the most tricky bugs. As always, you practice the
new concepts by providing your ATM model with a new excep-
tion class and the mechanism for handling it.
q Chapter 8, “File I/O and Persistence”
Like most programming languages, Java provides your pro-
grams with facilities to support input and output through the
console or using files. Input and output streams are introduced
in this chapter along with a powerful mechanism, serializa-
tion, that enables you to save objects state and restore it later
on. Using serialization, you transform your ATM model to
make it persistent.
Preface ix
How This Book Is Organized
q Chapter 9, “Managing Your Code”
Modifying your code is one thing, retrieving the modification
history is another. With VisualAge for Java every modification
you make in your code is kept in a underlying database. Thus
you can backtrack your modifications and restore old versions
of your code. This chapter describes the versioning mechanism
provided by VisualAge for Java and how you can take advan-
tage of it in the implementation of your ATM applet.
q Chapter 10, “Building User Interfaces with Java”
Once your ATM model is built, you must provide it with a view
that represents the user interface of your applet. This chapter
shows you how to use the Abstract Windowing Toolkit (AWT)
to create user interfaces in Java. Several small applications
are built to illustrate the concepts learned.
q Chapter 11, “Visual Programming and JavaBeans”
With the new release of the Java Developer’s Toolkit (JDK
1.1), a new software component specification, called Java-
Beans, is introduced. This chapter explains Java software com-
ponents, or beans, and shows how you can build your own
beans using the VisualAge for Java Visual Composition Editor.
Visual programming is also introduced. To illustrate the con-
cepts and techniques described in this chapter, you build a cal-
culator that you reuse later in your ATM applet.
q Chapter 12, “Visual Programming in Action”
After you have grasped the power of JavaBeans and the visual
construction from beans provided by the Visual Composition
Editor, we show you the techniques that you must master to
build a user interface visually. You practice by building the
view of your ATM applet that you assemble with the ATM
model to compose the final applet.
q Chapter 13, “Improving Your ATM Applet”
In this chapter you learn about multithreading, run-time type
information handling with the Visual Composition Editor, and
internationalization of your program. These advanced fea-
tures are illustrated by improving your applet.
q Chapter 14, “Publishing Your Applet”
Once your applet has been fully tested, you may want to pub-
lish it on the Internet. This chapter shows you how to do
that—from writing an HTML page that embeds your applet, to
setting up your Web server.
x VisualAge for Java