Java By Example Clayton Walnum C O N T E N T S Introduction Who This Book Is For● Hardware and Software Requirements● Compiling the Programs in This Book● A Word to the Wise● On to the Wonderful World of Java● Chapter 1 Java Overview The Java Story● Introducing Java● Java Programs● The Java Developer's Kit● Where Is Java?● Example: Installing HotJava● Example: Installing the JDK● Summary● Review Questions● Review Exercises● http://www.ngohaianh.info Chapter 2 Running Java Applets The Sample Java Applets The Appletviewer Tool❍ Example: Running TicTacToe❍ The Animator Applet❍ The BarChart Applet❍ Other Demo Applets❍ ● Adding Applets to an HTML Document Optional Attributes for Applets❍ Applet Parameters❍ Non-Java Browsers❍ Example: A Java-Powered Home Page❍ ● Summary● Review Questions● Review Exercises● Chapter 3 Applets and the Internet Local and Remote Applets Local Applets❍ Remote Applets❍ ● Clients and Servers● Security● Example: Your Pages on the Web● Summary● Review Questions● Review Exercises● Chapter 4 Object-Oriented Programming Concepts From Switches to Objects● An Obvious, Yet Brilliant, Solution● Object-Oriented Programming● http://www.ngohaianh.info Encapsulation❍ Classes as Data Types❍ Inheritance❍ Polymorphism❍ Example: Encapsulation, Inheritance, and Polymorphism❍ Summary● Review Questions● Review Exercises● Chapter 5 Constants and Variables Constants● Variables● Naming Constants and Variables● Example: Creating Your Own Identifiers● Data Types Integer Values❍ Floating-Point Values❍ Character Values❍ Boolean Values❍ ● Variable Scope Example: Determining a Variable's Scope❍ ● Summary● Review Questions● Review Exercises● Chapter 6 Simple Input and Output Windows and Graphics● Displaying Text in an Applet Example: Creating and Running Applet1❍ How Applet1 Works❍ ● Getting Input from the User How Applet2 Works❍ ● http://www.ngohaianh.info Example: Retrieving text from a TextField control❍ How Applet3 Works❍ Displaying Numerical Values● Summary● Review Questions● Review Exercises● Chapter 7 Math Operators The Addition Operator Example: Using the Addition Operator❍ Example: Multiple Additions❍ ● The Subtraction Operator Example: Using the Subtraction Operator❍ Example: Multiple Subtractions Using Mixed Data Types❍ Example: Casting a Result to a Different Data Type❍ ● The Multiplication Operator Example: Multiplication and Data Types❍ ● The Division Operator Example: Integer Versus Floating-Point Division❍ ● The Modulo Operator● The Increment Operator● The Decrement Operator● Example: Using Mathematical Calculations in an Applet How Applet5 Works❍ ● The Order of Operations Example: Order of Operations❍ Example: More Order of Operations❍ Example: Still More Order of Operations❍ Example: One Last Order of Operations❍ ● Summary● Review Questions● Review Exercises● http://www.ngohaianh.info Chapter 8 Expressions Types of Expressions● Expressions Within Expressions● Comparison Operators Example: Using Comparison Operators❍ ● Logical Operators Example: Using Logical Operators❍ Example: Using Multiple Logical Operators❍ Example: Combining Different Comparison and Logical Operators❍ ● Writing Logical Expressions● Order of Operations● Summary● Review Questions● Review Exercises● Chapter 9 The if and switch Statements Controlling Program Flow● Program Flow and Branching● The if statement Example: The Form of an if Statement❍ Multiple if Statements❍ Multiple-Line if Statements❍ The else Clause❍ Example: Using the if Statement in a Program❍ ● The switch Statement Example: Using the break Statement Correctly❍ Example: Using the switch Statement in a Program❍ ● Summary● Review Questions● Review Exercises● http://www.ngohaianh.info Chapter 10 The while and do-while Loops The while Loop Example: Using a while Loop❍ Example: Using a while Loop in a Program❍ ● The do-while Loop Example: Using a do-while Loop❍ Example: Using a do-while Loop in a Program❍ ● Summary● Review Questions● Review Exercises● Chapter 11 The for Loop Introducing the for Loop● Example: Using a for Loop● Example: Using a for Loop in a Program● Changing the Increment Value● Example: Looping with Different Increments● Using Variables in Loops● Example: Controlling for Loops with Variables● Summary● Review Questions● Review Exercises● Chapter 12 Functions The Top-Down Approach to Programming● Example: Using Functions as Subroutines● Defining and Calling Functions● Example: Using Functions to Return Values● Example: Putting Functions to Work● Summary● Review Questions● http://www.ngohaianh.info Review Exercises● Chapter 13 Arrays An Introduction to Arrays Example: Creating an Array❍ Example: Using a Variable as a Subscript❍ ● Multidimensional Arrays Example: Creating a Two-Dimensional Array❍ ● Example: Using Two-Dimensional Arrays in an Applet● Summary● Review Questions● Review Exercises● Chapter 14 Classes Classes and Objects Defining a Simple Class❍ Declaring Fields for a Class❍ Defining a Constructor❍ Example: Creating an Object by Calling a Constructor❍ Defining Methods❍ ● Example: Using Classes in Applets● Understanding the Applet● Using Inheritance Creating a Subclass❍ Adding Fields and Methods to the Subclass❍ Example: Adding Fields and Methods❍ ● Example: Using a Subclass in a Program● Overriding Methods of the Superclass● The this Keyword● Summary● Review Questions● Review Exercises● http://www.ngohaianh.info Chapter 15 Writing a Simple Applet The Simplest Java Applet● The Five Stages of an Applet's Life Cycle● Example: Overriding the Life Cycle Methods● Summary● Review Questions● Review Exercises● Chapter 16 Drawing Graphics The Applet's Canvas● Example: Using the Coordinate System● Drawing Shapes● Example: Drawing a Rectangle● Example: Drawing Other Shapes● Understanding the ShapeApplet Applet Drawing Ovals❍ Drawing Arcs❍ Example: Drawing Arcs in an Applet❍ Drawing Polygons❍ ● Summary● Review Questions● Review Exercises● Chapter 17 Graphical Text Dealing with Graphical Text Getting Font Attributes❍ Example: Displaying Font Information❍ Getting Font Metrics❍ Example: Displaying Font Metrics❍ ● Creating Fonts Example: Creating a Font with Multiple Styles❍ ● http://www.ngohaianh.info Using the Font❍ Example: Displaying Different Sized Fonts❍ Summary● Review Questions● Review Exercises● Chapter 18 Label and Button Controls Labels Example: Creating a Label❍ Methods of the Label Class❍ ● Buttons Example: Adding a Button to an Applet❍ Handling Multiple-Button Events❍ Example: Handling Multiple Buttons in an Applet❍ ● Summary● Review Questions● Review Exercises● Chapter 19 Checkbox and TextField Controls Checkboxes Example: Creating Nonexclusive Checkboxes❍ Checkbox Groups❍ Checkbox Methods❍ Example: Handling Checkboxes in an Applet❍ Responding to a Checkbox Event❍ Example: Handling Checkbox Events in an Applet❍ ● TextFields TextField Methods❍ Example: Using Echo Characters❍ ● Summary● Review Questions● Review Exercises● http://www.ngohaianh.info Chapter 20 Choice Menu, Text Area, and Scrolling List Controls Choice Menus Example: Creating a Choice Menu❍ Choice Menu Methods❍ Example: Responding to Menu Events in an Applet❍ ● Scrolling Lists Example: Creating a Single-Selection List❍ Example: Creating a Multiple-Selection List❍ Example: Creating a Scrolling List❍ Methods of the List Class❍ Example: Using a Scrolling List in an Applet❍ ● The TextArea Control Example: Creating a TextArea Control❍ Methods of the TextArea Class❍ ● Summary● Review Questions● Review Exercises● Chapter 21 Scrollbar and Canvas Controls Scrollbars Example: Creating a Scrollbar❍ Responding to a Scrollbar❍ Example: Using a Scrollbar in an Applet❍ Canvases❍ Example: Using a Canvas in an Applet❍ ● Summary● Review Questions● Review Exercises● http://www.ngohaianh.info [...]... Questions q Review Exercises Appendix A Answers to Review Questions q Chapter 1 q Chapter 2 q Chapter 3 q Chapter 4 q Chapter 5 q Chapter 6 q Chapter 7 q Chapter 8 q Chapter 9 q Chapter 10 q Chapter 11 q Chapter 12 q Chapter 13 q Chapter 14 q Chapter 15 q Chapter 16 q Chapter 17 q Chapter 18 q Chapter 19 q Chapter 20 q Chapter 21 q Chapter 22 q Chapter 23 q Chapter 24 q Chapter 25 q Chapter 26 q Chapter... shelf and leave the store.) At this point, Java is virtually guaranteed its place in Internet history Want to know why? Turn the page and keep reading Clayton Walnum, May 19 96 http://www.ngohaianh.info Chapter 1 Java Overview CONTENTS q The Java Story q Introducing Java q Java Programs q The Java Developer's Kit q Where Is Java? q Example: Installing HotJava q Example: Installing the JDK q Summary q Review... Chapter 31 q Chapter 32 q Chapter 33 q Chapter 34 q Chapter 35 q Chapter 36 Appendix B Glossary Appendix C IDEs and Tools q IDEs r r Java+ r q Diva JavaMaker Tools r r VbToJava r q AppletGen PortaFilter Summary Credits Java By Example Copyright© 19 96 by Que® Corporation All rights reserved Printed in the United States of America No part of this book may be used or reproduced in any form or by any means,... devices supported by the control NOTE The *7 screen display featured a number of animated figures, of which Duke (now the little guy considered to be the Java mascot) was one Once you get involved with Java, you're liable to see a lot of Duke, who pops up on the Sun Microsystems Web site (Figure 1. 1), and who is featured in some of Sun's sample Java applets Figure 1. 1 : Duke has become the Java mascot The... contains the main HotJava application (HOTJAVA.EXE), as well as many of the Java environment's tools, including the compiler, interpreter, and profiler The LIB folder contains a number of other files needed for HotJava: Figure 1. 7 : After extraction, the HOTJAVA directory will contain all of HotJava's files lib/classes.zip lib/properties/ lib/hotjava/ and lib/templates/ This file is needed by the compiler... whose Internet Explorer 3 (currently in beta) offers Java support After more than five years of development, Java has found its home Figure 1. 2 : The new Netscape Navigator 2.0 Web browser is Java capable Introducing Java By now, you may be curious why Java is considered such a powerful tool for Internet development projects You already know that Java is a simplified version of C++ Anyone who has struggled... the Java compiler to locate and report type conflicts q Compiled Before you can run a program written in the Java language, the program must be compiled by the Java compiler The compilation results in a "byte-code" file that, while similar to a machine-code file, can be executed under any operating system that has a Java interpreter This interpreter reads in the byte-code file and translates the byte-code... HotJava Web browser itself, shown in Figure 1. 4 This basic browser is completely written in the Java language, showing how Java handles not only normal programming tasks such as looping and evaluating mathematical expressions, but also how it can handle the complexities of telecommunications programming Figure 1. 4 : The HotJava Web browser is written entirely in the Java programming language The Java. .. using Java' s tools to create your own applets Where Is Java? All this talk about Java doesn't do you much good until you get your own copy of the Java Development Kit (JDK) You'll probably also want a copy of HotJava and Netscape Navigator 2.0, so that you can try out the Web pages you create with your Java applets Two versions of the JDK are included on this book's CD-ROM While version 1. 0 .1 has been... Example: Using a Thread in an Applet Deriving a Class from Thread r r q Example: Creating a Thread Class Example: Using a Separate Thread in an Applet Synchronizing Multiple Threads r Example: Using a Synchronized Method r Understanding ThreadApplet3 q Summary q Review Questions q Review Exercises Chapter 32 Writing Java Applications q About Java Applications q The Simplest Java Application r r q Example: . Wonderful World of Java Chapter 1 Java Overview The Java Story● Introducing Java Java Programs● The Java Developer's Kit● Where Is Java? ● Example: Installing HotJava● Example: Installing. Questions Chapter 1 Chapter 2● Chapter 3● Chapter 4● Chapter 5● Chapter 6● Chapter 7● Chapter 8● Chapter 9● Chapter 10 ● Chapter 11 ● Chapter 12 ● Chapter 13 ● Chapter 14 ● Chapter 15 ● Chapter 16 ●. Writing Java Applications About Java Applications● The Simplest Java Application Example: Building an Application❍ Example: Getting an Application's Arguments❍ ● Windowed Applications Example: