www.traintelco.com Java ™ in 60 Minutes a Day b423149 FM.qxd 6/30/03 3:36 PM Page i www.traintelco.com b423149 FM.qxd 6/30/03 3:36 PM Page ii www.traintelco.com Richard F. Raposa Java ™ in 60 Minutes a Day b423149 FM.qxd 6/30/03 3:36 PM Page iii www.traintelco.com Executive Publisher: Robert Ipsen Vice President and Publisher: Joe Wikert Senior Editor: Ben Ryan Editorial Manager: Kathryn A. Malm Developmental Editor: Jerry Olsen Production Editor: Vincent Kunkemueller Media Development Specialist: Angie Denny Text Design & Composition: Wiley Composition Services Copyright © 2003 by Richard F. Raposa. All rights reserved. Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rose- wood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8700. Requests to the Pub- lisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, E-mail: permcoordinator@wiley.com. Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, inci- dental, consequential, or other damages. For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley logo and related trade dress are trademarks or registered trademarks of Wiley in the United States and other countries, and may not be used without written permission. The Gearhead Press trademark is the exclusive property of Gearhead Group Corporation. Java is a trademark of Sun Microsystems, Inc. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Cataloging-in-Publication Data: See Publisher ISBN: 0-471-42314-9 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 b423149 FM.qxd 6/30/03 3:36 PM Page iv www.traintelco.com A Note from the Consulting Editor Instructor-led training is proven to be an effective and popular tool for training engi- neers and developers. To convey technical ideas and concepts, the classroom experi- ence is shown to be superior when compared to other delivery methods. As a technical trainer for more than 20 years, I have seen the effectiveness of instructor-led training firsthand. 60 Minutes a Day combines the best of the instructor-led training and book experience. Technical training is typically divided into short and discrete modules, where each module encapsulates a specific topic; each module is then fol- lowed by “questions and answers” and a review. 60 Minutes a Day titles follow the same model: each chapter is short, discrete, and can be completed in 60 minutes a day. For these books, I have enlisted premier technical trainers as authors. They provide the voice of the trainer and demonstrate classroom experience in each book of the series. You even get an opportunity to meet the actual trainer: As part of this innova- tive approach, each chapter of a 60 Minutes a Day book is presented online by the author. Readers are encouraged to view the online presentation before reading the rel- evant chapter. Therefore, 60 Minutes a Day delivers the complete classroom experi- ence—even the trainer. As an imprint of Wiley Publishing, Inc., Gearhead Press continues to bring you, the reader, the level of quality that Wiley has delivered consistently for nearly 200 years. Thank you. Donis Marshall Founder, Gearhead Press Consulting Editor, Wiley Technology Publishing Group b423149 FM.qxd 6/30/03 3:36 PM Page v www.traintelco.com b423149 FM.qxd 6/30/03 3:36 PM Page vi www.traintelco.com To my wife, Susan, for her motivation and support, and to our children, Megan, Ryan, Katelyn, and Emma, for letting me use the computer for hours at a time. b423149 FM.qxd 6/30/03 3:36 PM Page vii www.traintelco.com b423149 FM.qxd 6/30/03 3:36 PM Page viii www.traintelco.com Acknowledgments xix About the Author xxi Introduction xxiii Chapter 1 Getting Started with Java 1 Why Java? 1 The Java Virtual Machine 2 The Editions of Java 4 J2SE 4 J2ME 5 J2EE 5 Downloading the Java 2 SDK 6 Installing the SDK 7 Running the SDK Tools 8 Running the javac Compiler 9 Running the JVM 10 A Simple Java Program 10 Step 1: Write the Source Code 11 Step 2: Compile the Program 13 Step 3: Run the Program 14 Summary 17 Chapter 2 Java Fundamentals 21 Java Keywords 21 Identifiers 22 Java’s Eight Primitive Data Types 23 Variables 24 Assigning Variables 25 Integral Types 27 Contents ix 423149 FM.qxd 7/10/03 2:03 PM Page ix www.traintelco.com [...]... 273 Adding Elements to a Vector Accessing and Removing Elements in a Vector The Hashtable Class 275 277 281 Adding Elements to a Hashtable Accessing Elements in a Hashtable Summary 283 285 290 Chapter 10 Interfaces An Overview of Interfaces Declaring Interfaces User-Defined Interfaces Write the Interface Source Code Compile the Interface Implementing an Interface Write a Class That Implements Paintable... 130 Using this in a Constructor Summary Chapter 6 131 136 Understanding Inheritance An Overview of Inheritance The is a Relationship Implementing Inheritance Instantiating Child Objects Single versus Multiple Inheritance The java. lang.Object Class The Methods of the Object Class Method Overriding The super Keyword The final Keyword 139 139 144 145 146 149 150 151 154 157 160 final Methods 161 The Instantiation... Class Add the paint() Method Write a Class That Uses Paintable www.traintelco.com 295 295 296 298 299 299 300 300 301 302 302 Contents Using Interfaces Exposing Methods via an Interface Forcing Behavior on a Class Declaring Fields in Interfaces Extending Interfaces Extending Multiple Interfaces Interfaces and Polymorphism Summary 303 304 310 316 317 319 321 326 Chapter 11 Exception Handling Overview of... almost every aspect of Java and the J2EE technologies xxi www.traintelco.com www.traintelco.com Introduction An Overview of Java in 60 Minutes a Day I will never forget taking my first Java class at Sun Microsystems in Dallas, Texas, in May, 1998 I had heard the many promises about Java and how it would revolutionize software development, but I was skeptical and arrogant as I sat in the back of the class... www.Wiley.com/compbooks/60minutesaday Summary Reading this book is the next best thing to sitting in on one of my classes After 5 years of teaching Java to hundreds of students, I have learned what’s important to new Java programmers and what’s not The book is written in the first person, as if I am lecturing in front of a class, and it contains notes and tips that I’m sure you will find useful I hope you... discussed in this chapter include an overview of JavaBeans, the Bean Builder, properties, events, and hooking beans together in a builder tool JavaBeans are used in many of the Java technologies, including an important role in JavaServer Pages, a popular Java technology for simplifying Web page development Who Should Read This Book This book is targeted towards programmers who want to learn Java I make... classes This chapter covers the details of writing and implementing interfaces Knowledge of interfaces is an absolute must in Java, so study this chapter closely www.traintelco.com Introduction xxvii Chapter 11: Exception Handling Exception handling is a built -in feature of Java, and you need to know how to catch an exception before continuing further in the book This chapter discusses the two types... techniques for writing and starting a thread: implementing the Runnable interface, extending the Thread class, and using the Timer class The wait() and notify() methods of the Object class are also discussed in detail You can do some fun things with threads, as you will discover by doing the labs in this chapter Chapter 16: Input and Output The java. io package contains some great classes for performing just... Programming AWT versus Swing Creating Windows 367 367 369 java. awt.Frame Class javax.swing.JFrame Class 369 372 Containers and Components 375 Adding Components to a Container Layout Managers FlowLayout Manager BorderLayout Manager Panels GridLayout Manager BoxLayout Manager Nesting Panels Using No Layout Manager Summary Chapter 13 GUI Components and Event Handling The Delegation Model The Event Listener Interfaces... Bean into the Bean Builder Step 7: Using the Movie Bean in the Builder Tool Bound Properties 669 669 672 675 676 676 677 678 678 681 681 684 Step 8: Binding Properties in the Bean Builder Constrained Properties Vetoing an Event www.traintelco.com 687 690 693 Contents Overview of Events Step 9: Hooking up Buttons to the Movie Bean Step 10: Viewing Beans in Preview Mode 694 695 696 Generating User-Defined . www.traintelco.com Java ™ in 60 Minutes a Day b423149 FM.qxd 6/30/03 3:36 PM Page i www.traintelco.com b423149 FM.qxd 6/30/03 3:36 PM Page ii www.traintelco.com Richard F. Raposa Java ™ in 60 Minutes. effectiveness of instructor-led training firsthand. 60 Minutes a Day combines the best of the instructor-led training and book experience. Technical training is typically divided into short and. xii www.traintelco.com Using Interfaces 303 Exposing Methods via an Interface 304 Forcing Behavior on a Class 310 Declaring Fields in Interfaces 316 Extending Interfaces 317 Extending Multiple Interfaces