Statistics
24
Views
0
Downloads
0
Donations
Uploader

高宏飞

Shared on 2025-12-20
Support
Share

AuthorJoyce Farrell

Java Programming, Ninth Edition, provides the beginning programmer with a guide to developing applications using the Java programming language. Java is popular among professional programmers because it can be used to build visually interesting graphical user interface (GUI) and Web-based applications. Java also provides an excellent environment for the beginning programmer—a student can quickly build useful programs while learning the basics of structured and object-oriented programming techniques. This textbook assumes that you have little or no programming experience. It provides a solid background in good object-oriented programming techniques and introduces terminology using clear, familiar language. The programming examples are business examples; they do not assume a mathematical background beyond high school business math. In addition, the examples illustrate only one or two major points; they do not contain so many features that you become lost following irrelevant and extraneous details. Complete, working programs appear frequently in each chapter; these examples help students make the transition from the theoretical to the practical. The code presented in each chapter also can be downloaded from the publisher’s website, so students easily can run the programs and experiment with changes to them. The student using Java Programming, Ninth Edition, builds applications from the bottom up rather than starting with existing objects. This facilitates a deeper understanding of the concepts used in object-oriented programming and engenders appreciation for the existing objects students use as their knowledge of the language advances. When students complete this book, they will know how to modify and create simple Java programs, and they will have the tools to create more complex examples. They also will have a fundamental knowledge about object-oriented programming, which will serve them well in advanced Java courses or in studying other object-oriented languages su

Tags
No tags
ISBN: 1337397075
Publisher: Cengage Learning
Publish Year: 2019
Language: 英文
Pages: 898
File Format: PDF
File Size: 11.3 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
JAVA™ PROGRAMMING JOYCE FARRELL Australia • Brazil • Mexico • Singapore • United Kingdom • United States N I N T H E d I T I o N 97070_fm_hr_i-xxiv.indd 1 27/02/18 7:34 pm
This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed. Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. The publisher reserves the right to remove content from this title at any time if subsequent rights restrictions require it. For valuable information on pricing, previous editions, changes to current editions, and alternate formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for materials in your areas of interest. Important Notice: Media content referenced within the product description or the product text may not be available in the eBook version.
Java™ Programming, Ninth Edition Joyce Farrell SVP, GM Skills: Jonathan Lau Product Team Manager: Kristin McNary Associate Product Manager: Kate Mason Executive Director of Content Design, Skills: Marah Bellegarde Director, Learning Design – Skills Computing: Leigh Hefferon Learning Designer: Natalie Onderdonk Product Assistant: Jake Toth Marketing Director: Michele McTighe Marketing Manager: Stephanie Albracht Content Project Manager: Michele Stulga Senior Designer: Diana Graham Production Service/Composition: SPi Global Cover image: Colormos/Photodisc /Getty Images © 2019, 2016, 2014, 2012 Cengage Learning, Inc. Unless otherwise noted, all content is © Cengage. ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced or distributed in any form or by any means, except as permitted by U.S. copyright law, without the prior written permission of the copyright owner. Unless otherwise noted all screenshots are courtesy of Microsoft Corporation. Unless otherwise noted all tables/figures exhibits are © 2019 Cengage®. For product information and technology assistance, contact us at Cengage Customer & Sales Support, 1-800-354-9706 or support.cengage.com. For permission to use material from this text or product, submit all requests online at www.cengage.com/permissions. Library of Congress Control Number: 2018933919 Softbound ISBN: 978-1-337-39707-0 Loose Leaf ISBN: 978-1-337-68590-0 Cengage 20 Channel Center Street Boston, MA 02210 USA Cengage is a leading provider of customized learning solutions with employees residing in nearly 40 different countries and sales in more than 125 countries around the world. Find your local representative at www.cengage.com. Cengage products are represented in Canada by Nelson Education, Ltd. To learn more about Cengage platforms and services, visit www.cengage.com. To register or access your online learning solution or purchase materials for your course, visit www.cengagebrain.com. Notice to the Reader Publisher does not warrant or guarantee any of the products described herein or perform any independent analysis in connection with any of the product information contained herein. Publisher does not assume, and expressly disclaims, any obligation to obtain and include information other than that provided to it by the manufacturer. The reader is expressly warned to consider and adopt all safety precautions that might be indicated by the activities described herein and to avoid all potential hazards. By following the instructions contained herein, the reader willingly assumes all risks in connection with such instructions. The publisher makes no representations or warranties of any kind, including but not limited to, the warranties of fitness for particular purpose or merchantability, nor are any such representations implied with respect to the material set forth herein, and the publisher takes no responsibility with respect to such material. The publisher shall not be liable for any special, consequential, or exemplary damages resulting, in whole or part, from the readers’ use of, or reliance upon, this material. Printed in the United States of America Print Number: 01 Print Year: 2018 97070_fm_hr_i-xxiv.indd 2 27/02/18 7:34 pm
Brief Contents Preface � � � � � � � � � � � � � � � � xiv CHAPTER 1 Creat ing Java Programs � � � � � � � � � � 1 CHAPTER 2 Us ing Data � � � � � � � � � � � � � � � � 49 CHAPTER 3 Us ing Methods, Classes, and Objects � � � 110 CHAPTER 4 More Object Concepts � � � � � � � � � � 170 CHAPTER 5 Making Decis ions � � � � � � � � � � � � 230 CHAPTER 6 Looping � � � � � � � � � � � � � � � � 283 CHAPTER 7 Characters, Str ings, and the StringBuilder � � � � � � � � � � � � � 330 CHAPTER 8 Arrays � � � � � � � � � � � � � � � � � 369 CHAPTER 9 Advanced Array Concepts � � � � � � � � 416 CHAPTER 10 Introduct ion to Inher i tance � � � � � � � � 467 CHAPTER 11 Advanced Inher i tance Concepts � � � � � � 513 CHAPTER 12 Except ion Handl ing � � � � � � � � � � � 570 CHAPTER 13 F i le Input and Output � � � � � � � � � � � 635 CHAPTER 14 Introduct ion to Swing Components � � � � 698 CHAPTER 15 Us ing JavaFX and Scene Bui lder � � � � � � 758 APPENdIx A Work ing wi th the Java Plat form � � � � � � 799 APPENdIx B Data Representat ion � � � � � � � � � � � 804 APPENdIx C Formatt ing Output � � � � � � � � � � � � 810 APPENdIx d Generat ing Random Numbers � � � � � � � 820 APPENdIx E Javadoc � � � � � � � � � � � � � � � � 826 G lossary � � � � � � � � � � � � � � � � 834 Index � � � � � � � � � � � � � � � � � 853 iii 97070_fm_hr_i-xxiv.indd 3 27/02/18 7:34 pm
97070_fm_hr_i-xxiv.indd 4 27/02/18 7:34 pm
Table of Contents Preface � � � � � � � � � � � � � � � � xiv CHAPTER 1 Creat ing Java Programs � � � � � � � � � � 1 Learning Programming Terminology � � � � � � � � � � � � � 2 Comparing Procedural and Object-Oriented Programming Concepts � � � � � � � � � � � � � � � � � � 5 Procedural Programming � � � � � � � � � � � � � � � � � 5 Object-Oriented Programming � � � � � � � � � � � � � � 6 Understanding Classes, Objects, and Encapsulation � � � � 7 Understanding Inheritance and Polymorphism � � � � � � � 9 Features of the Java Programming Language � � � � � � � 10 Analyzing a Java Application that Produces Console Output � � � � � � � � � � � � � � � � � � � � 12 Understanding the Statement that Produces the Output � � � � � � � � � � � � � � � � � � � � � � 13 Understanding the First Class � � � � � � � � � � � � 15 Understanding the main() Method � � � � � � � � � � � 17 Indent Style � � � � � � � � � � � � � � � � � � � � � � 19 Saving a Java Class � � � � � � � � � � � � � � � � � � 20 Compiling a Java Class and Correcting Syntax Errors � � � � 22 Compiling a Java Class � � � � � � � � � � � � � � � � 22 Correcting Syntax Errors � � � � � � � � � � � � � � � � 23 Running a Java Application and Correcting Logic Errors � � � 29 Running a Java Application � � � � � � � � � � � � � � � 29 Modifying a Compiled Java Class � � � � � � � � � � � � 29 Correcting Logic Errors � � � � � � � � � � � � � � � � 31 Adding Comments to a Java Class � � � � � � � � � � � � 32 Creating a Java Application that Produces GUI Output� � � � 35 Finding Help � � � � � � � � � � � � � � � � � � � � � � 38 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � � 39 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 41 Chapter Summary � � � � � � � � � � � � � � � � � � � � 41 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 45 v 97070_fm_hr_i-xxiv.indd 5 27/02/18 7:34 pm
vi C o N T E N T s CHAPTER 2 Us ing Data � � � � � � � � � � � � � � � � 49 Declaring and Using Constants and Variables � � � � � � � 50 Declaring Variables � � � � � � � � � � � � � � � � � � 51 Declaring Named Constants � � � � � � � � � � � � � � 52 The Scope of Variables and Constants � � � � � � � � � 54 Concatenating Strings to Variables and Constants � � � � 54 Pitfall: Forgetting that a Variable Holds One Value at a Time � � � � � � � � � � � � � � � � � � � � � � 57 Learning About Integer Data Types � � � � � � � � � � � 60 Using the boolean Data Type � � � � � � � � � � � � � � 65 Learning About Floating-Point Data Types � � � � � � � � � 67 Using the char Data Type � � � � � � � � � � � � � � � � 68 Using the Scanner Class to Accept Keyboard Input � � � � 74 Pitfall: Using nextLine() Following One of the Other Scanner Input Methods � � � � � � � � � � � 77 Using the JOptionPane Class to Accept GUI Input� � � � � 82 Using Input Dialog Boxes� � � � � � � � � � � � � � � � 83 Using Confirm Dialog Boxes � � � � � � � � � � � � � � 86 Performing Arithmetic Using Variables and Constants � � � � 88 Associativity and Precedence� � � � � � � � � � � � � � 89 Writing Arithmetic Statements Efficiently � � � � � � � � 91 Pitfall: Not Understanding Imprecision in Floating-Point Numbers � � � � � � � � � � � � � � � � 91 Understanding Type Conversion� � � � � � � � � � � � � � 96 Automatic Type Conversion� � � � � � � � � � � � � � � 96 Explicit Type Conversions � � � � � � � � � � � � � � � 97 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �101 Key Terms � � � � � � � � � � � � � � � � � � � � � � �102 Chapter Summary � � � � � � � � � � � � � � � � � � � �102 Exercises � � � � � � � � � � � � � � � � � � � � � � � �105 CHAPTER 3 Us ing Methods, Classes, and Objects � � � 110 Understanding Method Calls and Placement � � � � � � � �111 Understanding Method Construction� � � � � � � � � � � �114 Access Specifiers � � � � � � � � � � � � � � � � � � �115 Return Type � � � � � � � � � � � � � � � � � � � � � �116 Method Name � � � � � � � � � � � � � � � � � � � � �116 Parentheses � � � � � � � � � � � � � � � � � � � � �117 Adding Parameters to Methods � � � � � � � � � � � � � �121 Creating a Method that Receives a Single Parameter � � �122 Creating a Method that Requires Multiple Parameters � � �125 Creating Methods that Return Values � � � � � � � � � � �127 Chaining Method Calls � � � � � � � � � � � � � � � � �129 97070_fm_hr_i-xxiv.indd 6 27/02/18 7:34 pm
vii Learning About Classes and Objects � � � � � � � � � � �133 Creating a Class � � � � � � � � � � � � � � � � � � � �136 Creating Instance Methods in a Class � � � � � � � � � � �138 Organizing Classes � � � � � � � � � � � � � � � � � �141 Declaring Objects and Using Their Methods � � � � � � � �145 Understanding Data Hiding � � � � � � � � � � � � � � �147 An Introduction to Using Constructors � � � � � � � � � � �150 Understanding that Classes Are Data Types � � � � � � � �154 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �158 Key Terms � � � � � � � � � � � � � � � � � � � � � � �158 Chapter Summary � � � � � � � � � � � � � � � � � � � �159 Exercises � � � � � � � � � � � � � � � � � � � � � � � �163 CHAPTER 4 More Object Concepts � � � � � � � � � � 170 Understanding Blocks and Scope � � � � � � � � � � � � �171 Overloading a Method � � � � � � � � � � � � � � � � � �179 Automatic Type Promotion in Method Calls � � � � � � � �181 Learning About Ambiguity � � � � � � � � � � � � � � � �185 Creating and Calling Constructors with Parameters � � � � �187 Overloading Constructors � � � � � � � � � � � � � � �188 Learning About the this Reference � � � � � � � � � � � �192 Using the this Reference to Make Overloaded Constructors More Efficient � � � � � � � � � � � � � �195 Using static Fields � � � � � � � � � � � � � � � � � �199 Using Constant Fields � � � � � � � � � � � � � � � � �201 Using Automatically Imported, Prewritten Constants and Methods � � � � � � � � � � � � � � � � �206 The Math Class � � � � � � � � � � � � � � � � � � � �206 Importing Classes that Are Not Imported Automatically � �208 Using the LocalDate Class � � � � � � � � � � � � � �210 Understanding Composition and Nested Classes � � � � � �216 Composition � � � � � � � � � � � � � � � � � � � � �216 Nested Classes � � � � � � � � � � � � � � � � � � � �218 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �220 Key Terms � � � � � � � � � � � � � � � � � � � � � � �220 Chapter Summary � � � � � � � � � � � � � � � � � � � �220 Exercises � � � � � � � � � � � � � � � � � � � � � � � �224 CHAPTER 5 Making Decis ions � � � � � � � � � � � � 230 Planning Decision-Making Logic � � � � � � � � � � � � � �231 The if and if…else Statements � � � � � � � � � � � � �233 The if Statement � � � � � � � � � � � � � � � � � � �233 Pitfall: Misplacing a Semicolon in an if Statement � � � �234 C o n t e n t s 97070_fm_hr_i-xxiv.indd 7 27/02/18 7:34 pm
viii C o N T E N T s Pitfall: Using the Assignment Operator Instead of the Equivalency Operator � � � � � � � � � � � � � �235 Pitfall: Attempting to Compare Objects Using the Relational Operators � � � � � � � � � � � � � � �236 The if…else Statement � � � � � � � � � � � � � � � �236 Using Multiple Statements in if and if…else Clauses � � �239 Nesting if and if…else Statements � � � � � � � � � � �245 Using Logical AND and OR Operators � � � � � � � � � � �247 The AND Operator � � � � � � � � � � � � � � � � � � �247 The OR Operator � � � � � � � � � � � � � � � � � � �249 Short-Circuit Evaluation � � � � � � � � � � � � � � � �250 Making Accurate and Efficient Decisions� � � � � � � � � �253 Making Accurate Range Checks� � � � � � � � � � � � �253 Making Efficient Range Checks � � � � � � � � � � � � �256 Using && and || Appropriately � � � � � � � � � � � � �256 Using the switch Statement � � � � � � � � � � � � � � �258 Using the Conditional and NOT Operators � � � � � � � � �264 Using the NOT Operator � � � � � � � � � � � � � � � �265 Understanding Operator Precedence � � � � � � � � � � �266 Adding Decisions and Constructors to Instance Methods� � � � � � � � � � � � � � � � � � � � � � � �269 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �272 Key Terms � � � � � � � � � � � � � � � � � � � � � � �273 Chapter Summary � � � � � � � � � � � � � � � � � � � �273 Exercises � � � � � � � � � � � � � � � � � � � � � � � �277 CHAPTER 6 Looping � � � � � � � � � � � � � � � � 283 Learning About the Loop Structure � � � � � � � � � � � �284 Creating while Loops � � � � � � � � � � � � � � � � � �285 Writing a Definite while Loop � � � � � � � � � � � � �285 Pitfall: Failing to Alter the Loop Control Variable Within the Loop Body � � � � � � � � � � � � � � � � �287 Pitfall: Unintentionally Creating a Loop with an Empty Body� � � � � � � � � � � � � � � � � � � �288 Altering a Definite Loop’s Control Variable � � � � � � � �289 Writing an Indefinite while Loop � � � � � � � � � � � �290 Validating Data � � � � � � � � � � � � � � � � � � � �292 Using Shortcut Arithmetic Operators � � � � � � � � � � �296 Creating a for Loop� � � � � � � � � � � � � � � � � � �300 Unconventional for Loops � � � � � � � � � � � � � � �302 Learning How and When to Use a do…while Loop � � � � �306 Learning About Nested Loops � � � � � � � � � � � � � �308 Improving Loop Performance � � � � � � � � � � � � � � �313 Avoiding Unnecessary Operations � � � � � � � � � � � �314 97070_fm_hr_i-xxiv.indd 8 27/02/18 7:34 pm
ix Considering the Order of Evaluation of Short-Circuit Operators � � � � � � � � � � � � � � � �314 Comparing to Zero � � � � � � � � � � � � � � � � � �315 Employing Loop Fusion � � � � � � � � � � � � � � � �316 A Final Note on Improving Loop Performance � � � � � �317 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �320 Key Terms � � � � � � � � � � � � � � � � � � � � � � �320 Chapter Summary � � � � � � � � � � � � � � � � � � � �320 Exercises � � � � � � � � � � � � � � � � � � � � � � � �324 CHAPTER 7 Characters, Str ings, and the StringBuilder � � � � � � � � � � � � � 330 Understanding String Data Problems � � � � � � � � � � �331 Using Character Class Methods� � � � � � � � � � � � �332 Declaring and Comparing String Objects � � � � � � � � �336 Comparing String Values � � � � � � � � � � � � � � �336 Empty and null Strings � � � � � � � � � � � � � � � �340 Using a Variety of String Methods � � � � � � � � � � � �342 Converting String Objects to Numbers� � � � � � � � �347 Learning About the StringBuilder and StringBuffer Classes � � � � � � � � � � � � � �352 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �358 Key Terms � � � � � � � � � � � � � � � � � � � � � � �359 Chapter Summary � � � � � � � � � � � � � � � � � � � �359 Exercises � � � � � � � � � � � � � � � � � � � � � � � �362 CHAPTER 8 Arrays � � � � � � � � � � � � � � � � � 369 Declaring an Array� � � � � � � � � � � � � � � � � � � �370 Initializing an Array � � � � � � � � � � � � � � � � � � �375 Using Variable Subscripts with an Array � � � � � � � � � �378 Using the Enhanced for Loop � � � � � � � � � � � � �380 Using Part of an Array � � � � � � � � � � � � � � � � �380 Declaring and Using Arrays of Objects � � � � � � � � � �383 Using the Enhanced for Loop with Objects � � � � � � �385 Manipulating Arrays of Strings � � � � � � � � � � � �385 Searching an Array and Using Parallel Arrays � � � � � � �392 Using Parallel Arrays � � � � � � � � � � � � � � � � �393 Searching an Array for a Range Match� � � � � � � � � �395 Passing Arrays to and Returning Arrays from Methods � � �399 Returning an Array from a Method � � � � � � � � � � �402 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �405 Key Terms � � � � � � � � � � � � � � � � � � � � � � �405 Chapter Summary � � � � � � � � � � � � � � � � � � � �405 Exercises � � � � � � � � � � � � � � � � � � � � � � � �409 C o n t e n t s 97070_fm_hr_i-xxiv.indd 9 27/02/18 7:34 pm
x C o N T E N T s CHAPTER 9 Advanced Array Concepts � � � � � � � � 416 Sorting Array Elements Using the Bubble Sort Algorithm � �417 Using the Bubble Sort Algorithm � � � � � � � � � � � �418 Improving Bubble Sort Efficiency � � � � � � � � � � � �420 Sorting Arrays of Objects � � � � � � � � � � � � � � �420 Sorting Array Elements Using the Insertion Sort Algorithm � � � � � � � � � � � � � � � � � � � � �425 Using Two-Dimensional and Other Multidimensional Arrays� �430 Passing a Two-Dimensional Array to a Method � � � � � �433 Using the length Field with a Two-Dimensional Array� � �433 Understanding Jagged Arrays � � � � � � � � � � � � �434 Using Other Multidimensional Arrays � � � � � � � � � �435 Using the Arrays Class � � � � � � � � � � � � � � � � �438 Using the ArrayList Class � � � � � � � � � � � � � � �446 Creating Enumerations � � � � � � � � � � � � � � � � � �449 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �456 Key Terms � � � � � � � � � � � � � � � � � � � � � � �456 Chapter Summary � � � � � � � � � � � � � � � � � � � �456 Exercises � � � � � � � � � � � � � � � � � � � � � � � �460 CHAPTER 10 Introduct ion to Inher i tance � � � � � � � � 467 Learning About the Concept of Inheritance � � � � � � � � �468 Diagramming Inheritance Using the UML � � � � � � � � �468 Inheritance Terminology � � � � � � � � � � � � � � � �470 Extending Classes � � � � � � � � � � � � � � � � � � � �472 Overriding Superclass Methods � � � � � � � � � � � � � �479 Using the @Override Tag � � � � � � � � � � � � � � �480 Calling Constructors During Inheritance � � � � � � � � � �483 Using Superclass Constructors that Require Arguments � �484 Accessing Superclass Methods � � � � � � � � � � � � � �489 Comparing this and super � � � � � � � � � � � � � �491 Employing Information Hiding � � � � � � � � � � � � � � �493 Methods You Cannot Override � � � � � � � � � � � � � �495 A Subclass Cannot Override static Methods in Its Superclass � � � � � � � � � � � � � � � � � � �495 A Subclass Cannot Override final Methods in Its Superclass � � � � � � � � � � � � � � � � � � �499 A Subclass Cannot Override Methods in a final Superclass � � � � � � � � � � � � � � � � � �501 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �502 Key Terms � � � � � � � � � � � � � � � � � � � � � � �502 Chapter Summary � � � � � � � � � � � � � � � � � � � �503 Exercises � � � � � � � � � � � � � � � � � � � � � � � �506 97070_fm_hr_i-xxiv.indd 10 27/02/18 7:34 pm
xi CHAPTER 11 Advanced Inher i tance Concepts � � � � � � 513 Creating and Using Abstract Classes � � � � � � � � � � �514 Using Dynamic Method Binding � � � � � � � � � � � � � �523 Using a Superclass as a Method Parameter Type � � � � �525 Creating Arrays of Subclass Objects � � � � � � � � � � �527 Using the Object Class and Its Methods � � � � � � � � �530 Using the toString() Method� � � � � � � � � � � � �532 Using the equals() Method � � � � � � � � � � � � � �535 Using Inheritance to Achieve Good Software Design � � � �540 Creating and Using Interfaces � � � � � � � � � � � � � �541 Creating Interfaces to Store Related Constants � � � � �548 Using Anonymous Inner Classes and Lambda Expressions � �552 Lambda Expressions� � � � � � � � � � � � � � � � � �554 Creating and Using Packages� � � � � � � � � � � � � � �555 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �557 Key Terms � � � � � � � � � � � � � � � � � � � � � � �558 Chapter Summary � � � � � � � � � � � � � � � � � � � �558 Exercises � � � � � � � � � � � � � � � � � � � � � � � �562 CHAPTER 12 Except ion Handl ing � � � � � � � � � � � 570 Learning About Exceptions � � � � � � � � � � � � � � � �571 Trying Code and Catching Exceptions � � � � � � � � � � �576 Using a try Block to Make Programs “Foolproof” � � � �580 Declaring and Initializing Variables in try…catch Blocks � � � � � � � � � � � � � � � � � � � � � � �582 Throwing and Catching Multiple Exceptions � � � � � � � �585 Using the finally Block � � � � � � � � � � � � � � � �591 Understanding the Advantages of Exception Handling � � � �593 Specifying the Exceptions that a Method Can Throw � � � �596 Tracing Exceptions Through the Call Stack� � � � � � � � �600 Creating Your Own Exception Classes � � � � � � � � � �605 Using Assertions � � � � � � � � � � � � � � � � � � � �608 Displaying the Virtual Keyboard � � � � � � � � � � � � � �622 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �625 Key Terms � � � � � � � � � � � � � � � � � � � � � � �626 Chapter Summary � � � � � � � � � � � � � � � � � � � �626 Exercises � � � � � � � � � � � � � � � � � � � � � � � �630 CHAPTER 13 F i le Input and Output � � � � � � � � � � 635 Understanding Computer Files � � � � � � � � � � � � � �636 Using the Path and Files Classes � � � � � � � � � � � �638 Creating a Path � � � � � � � � � � � � � � � � � � � �638 Retrieving Information About a Path � � � � � � � � � � �640 C o n t e n t s 97070_fm_hr_i-xxiv.indd 11 27/02/18 7:34 pm
xii C o N T E N T s Converting a Relative Path to an Absolute One � � � � � �641 Checking File Accessibility � � � � � � � � � � � � � � �642 Deleting a Path � � � � � � � � � � � � � � � � � � � �643 Determining File Attributes � � � � � � � � � � � � � � �645 File Organization, Streams, and Buffers � � � � � � � � � �648 Using Java’s IO Classes � � � � � � � � � � � � � � � � �651 Writing to a File � � � � � � � � � � � � � � � � � � � �654 Reading from a File � � � � � � � � � � � � � � � � � �656 Creating and Using Sequential Data Files � � � � � � � � �657 Learning About Random Access Files � � � � � � � � � � �663 Writing Records to a Random Access Data File � � � � � � �667 Reading Records from a Random Access Data File � � � � �673 Accessing a Random Access File Sequentially � � � � � �674 Accessing a Random Access File Randomly � � � � � � �675 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �689 Key Terms � � � � � � � � � � � � � � � � � � � � � � �689 Chapter Summary � � � � � � � � � � � � � � � � � � � �689 Exercises � � � � � � � � � � � � � � � � � � � � � � � �693 CHAPTER 14 Introduct ion to Swing Components � � � � 698 Understanding Swing Components � � � � � � � � � � � �699 Using the JFrame Class � � � � � � � � � � � � � � � � �700 Customizing a JFrame’s Appearance � � � � � � � � � �704 Using the JLabel Class � � � � � � � � � � � � � � � � �708 Changing a JLabel’s Font � � � � � � � � � � � � � � �710 Using a Layout Manager � � � � � � � � � � � � � � � � �712 Extending the JFrame Class � � � � � � � � � � � � � � �715 Adding JTextFields and JButtons to a JFrame � � � � �718 Adding JTextFields � � � � � � � � � � � � � � � � �718 Adding JButtons � � � � � � � � � � � � � � � � � � �720 Learning About Event-Driven Programming � � � � � � � � �724 Preparing Your Class to Accept Event Messages � � � � �725 Telling Your Class to Expect Events to Happen � � � � � �726 Telling Your Class How to Respond to Events � � � � � �726 An Event-Driven Program � � � � � � � � � � � � � � � �727 Using Multiple Event Sources � � � � � � � � � � � � � �728 Using the setEnabled() Method � � � � � � � � � � �730 Understanding Swing Event Listeners � � � � � � � � � � �733 Using the JCheckBox, ButtonGroup, and JComboBox Classes � � � � � � � � � � � � � � � �736 The JCheckBox Class � � � � � � � � � � � � � � � � �736 The ButtonGroup Class � � � � � � � � � � � � � � �740 The JComboBox Class � � � � � � � � � � � � � � � � �741 97070_fm_hr_i-xxiv.indd 12 27/02/18 7:34 pm
xiii Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �748 Key Terms � � � � � � � � � � � � � � � � � � � � � � �749 Chapter Summary � � � � � � � � � � � � � � � � � � � �749 Exercises � � � � � � � � � � � � � � � � � � � � � � � �753 CHAPTER 15 Us ing JavaFX and Scene Bui lder � � � � � 758 What Is JavaFX? � � � � � � � � � � � � � � � � � � � � �759 The Life Cycle of JavaFX Applications � � � � � � � � � � �760 Understanding JavaFX Structure: Stage, Scene, Panes, and Widgets � � � � � � � � � � � � � � � � � �762 Deploying JavaFX Applications � � � � � � � � � � � � � �768 Creating JavaFX Applications Using Scene Builder � � � � �768 Scene Builder Sections � � � � � � � � � � � � � � � �773 Using Widgets as Design Elements in FXML Layouts � � �774 Using CSS to Create Visual Effects � � � � � � � � � � � �778 Creating Animations in JavaFX � � � � � � � � � � � � � �785 Don’t Do It � � � � � � � � � � � � � � � � � � � � � � �790 Key Terms � � � � � � � � � � � � � � � � � � � � � � �790 Chapter Summary � � � � � � � � � � � � � � � � � � � �790 Exercises � � � � � � � � � � � � � � � � � � � � � � � �795 APPENdIx A Work ing wi th the Java Plat form � � � � � � 799 APPENdIx B Data Representat ion � � � � � � � � � � � 804 APPENdIx C Formatt ing Output � � � � � � � � � � � � 810 APPENdIx d Generat ing Random Numbers � � � � � � � 820 APPENdIx E Javadoc � � � � � � � � � � � � � � � � 826 G lossary � � � � � � � � � � � � � � � � 834 Index � � � � � � � � � � � � � � � � � 853 C o n t e n t s 97070_fm_hr_i-xxiv.indd 13 27/02/18 7:34 pm
Preface Java Programming, Ninth Edition, provides the beginning programmer with a guide to developing applications using the Java programming language. Java is popular among professional programmers because it can be used to build visually interesting graphical user interface (GUI) and Web-based applications. Java also provides an excellent environment for the beginning programmer—a student can quickly build useful programs while learning the basics of structured and object-oriented programming techniques. This textbook assumes that you have little or no programming experience. It provides a solid background in good object-oriented programming techniques and introduces terminology using clear, familiar language. The programming examples are business examples; they do not assume a mathematical background beyond high school business math. In addition, the examples illustrate only one or two major points; they do not contain so many features that you become lost following irrelevant and extraneous details. Complete, working programs appear frequently in each chapter; these examples help students make the transition from the theoretical to the practical. The code presented in each chapter also can be downloaded from the publisher’s website, so students easily can run the programs and experiment with changes to them. The student using Java Programming, Ninth Edition, builds applications from the bottom up rather than starting with existing objects. This facilitates a deeper understanding of the concepts used in object-oriented programming and engenders appreciation for the existing objects students use as their knowledge of the language advances. When students complete this book, they will know how to modify and create simple Java programs, and they will have the tools to create more complex examples. They also will have a fundamental knowledge about object-oriented programming, which will serve them well in advanced Java courses or in studying other object-oriented languages such as C++, C#, and Visual Basic. organization and Coverage Java Programming, Ninth Edition, presents Java programming concepts, enforcing good style, logical thinking, and the object-oriented paradigm. Objects are covered right from the beginning, earlier than in many other textbooks. You create your first Java program in Chapter 1. Chapters 2, 3, and 4 increase your understanding about how data, classes, objects, and methods interact in an object-oriented environment. Chapters 5 and 6 explore input and repetition structures, which are the backbone of programming logic and essential to creating useful programs in any language. You learn the special considerations of string and array manipulation in Chapters 7, 8, and 9. xiv 97070_fm_hr_i-xxiv.indd 14 27/02/18 7:34 pm
xv New in This Edition Chapters 10, 11, and 12 thoroughly cover inheritance and exception handling. Inheritance is the object-oriented concept that allows you to develop new objects quickly by adapting the features of existing objects; exception handling is the object-oriented approach to handling errors. Both are important concepts in object-oriented design. Chapter 13 provides information about handling files so you can store and retrieve program output. Chapter 14 introduces GUI Swing components, which are used to create visually pleasing, user-friendly, interactive applications. Chapter 15 introduces JavaFX, which is the newest platform for creating and delivering applications for the desktop and the Internet. Chapter 15 is written by Sandra Lavallee, a professor and Computer and Design Technologies Department chairperson at Lakes Region Community College in Laconia, New Hampshire. New in This Edition The following features are new for the Ninth Edition: • Java 9e: All programs have been tested using Java 9e, the newest edition of Java. • Windows 10: All programs have been tested in Windows 10, and all screen shots have been taken in this environment. • Programming exercises: Each chapter contains several new programming exercises not seen in previous editions. All exercises and their solutions from the previous edition that were replaced in this edition are still available on the Instructor Companion site. • Anonymous inner classes and lambda expressions: These two new topics are introduced in this edition of the book. • JavaFX: This edition includes coverage of JavaFX. Additionally, Java Programming, Ninth Edition, includes the following features: • OBJECTIVES: Each chapter begins with a list of objectives so you know the topics that will be presented in the chapter. In addition to providing a quick reference to topics covered, this feature provides a useful study aid. • YOU DO IT: In each chapter, step-by-step exercises help students create multiple working programs that emphasize the logic a programmer uses in choosing statements to include. These sections provide a means for students to achieve success on their own—even those in online or distance learning classes. • NOTES: These highlighted tips provide additional information—for example, an alternative method of performing a procedure, another term for a concept, background information about a technique, or a common error to avoid. • EMPHASIS ON STUDENT RESEARCH: The student frequently is directed to the Java website to investigate classes and methods. Computer languages evolve, and programming professionals must understand how to find the latest language improvements. This book encourages independent research. P R E FA C E 97070_fm_hr_i-xxiv.indd 15 27/02/18 7:34 pm
xvi New in This EditionP R E FA C E • FIGURES: Each chapter contains many figures. Code figures are most frequently 25 lines or fewer, illustrating one concept at a time. Frequent screen shots show exactly how program output appears. Callouts appear where needed to emphasize a point. • COLOR: The code figures in each chapter contain all Java keywords in blue. This helps students identify keywords more easily, distinguishing them from programmer-selected names. • FILES: More than 200 student files can be downloaded from the publisher’s website. Most files contain the code presented in the figures in each chapter; students can run the code for themselves, view the output, and make changes to the code to observe the effects. Other files include debugging exercises that help students improve their programming skills. • TWO TRUTHS & A LIE: A short quiz reviews each chapter section, with answers provided. This quiz contains three statements based on the preceding section of text—two statements are true, and one is false. Over the years, students have requested answers to problems, but we have hesitated to distribute them in case instructors want to use problems as assignments or test questions. These true-false quizzes provide students with immediate feedback as they read, without “giving away” answers to the multiple-choice questions and programming exercises. • DON’T DO IT: This section at the end of each chapter summarizes common mistakes and pitfalls that plague new programmers while learning the current topic. • KEY TERMS: Each chapter includes a list of newly introduced vocabulary, shown in the order of appearance in the text. The list of key terms provides a short review of the major concepts in the chapter. • SUMMARIES: Following each chapter is a summary that recaps the programming concepts and techniques covered in the chapter. This feature provides a concise means for students to check their understanding of the main points in each chapter. • REVIEW QUESTIONS: Each chapter includes 20 multiple-choice questions that serve as a review of chapter topics. • GAME ZONE: Each chapter provides one or more exercises in which students can create interactive games using the programming techniques learned up to that point; 50 game programs are suggested in the book. The games are fun to create and play; writing them motivates students to master the necessary programming techniques. Students might exchange completed game programs with each other, suggesting improvements and discovering alternate ways to accomplish tasks. • CASES: Each chapter contains two running case problems. These cases represent projects that continue to grow throughout a semester using concepts learned in each new chapter. Two cases allow instructors to assign different cases in alternate semesters or to divide students in a class into two case teams. • GLOSSARY: A glossary contains definitions for all key terms in the book. 97070_fm_hr_i-xxiv.indd 16 27/02/18 7:34 pm
xvii Instructor Companion Site • APPENDICES: This edition includes useful appendices on working with the Java platform, data representation, formatting output, generating random numbers, and creating Javadoc comments. • QUALITY: Every program example, exercise, and game solution was tested by the author and then tested again by a quality assurance team using Java Standard Edition (SE) 9, the most recent version available. Instructor Resources MindTap MindTap activities for Java Programming, Ninth Edition are designed to help students master the skills they need in today’s workforce. Research shows employers need critical thinkers, troubleshooters, and creative problem-solvers to stay relevant in our fast-paced, technology-driven world. MindTap helps you achieve this with assignments and activities that provide hands-on practice and real-life relevance. Students are guided through assignments that help them master basic knowledge and understanding before moving on to more challenging problems. All MindTap activities and assignments are tied to defined unit learning objectives. Hands-on coding labs provide real-life application and practice. Readings and dynamic visualizations support the lecture, while a post-course assessment measures exactly how much a student has learned. MindTap provides the analytics and reporting to easily see where the class stands in terms of progress, engagement, and completion rates. Use the content and learning path as-is, or pick-and-choose how our materials will wrap around yours. You control what the students see and when they see it. Learn more at http://www.cengage.com/mindtap/. The Java Programming MindTap also includes: • Unit Quizzes: Students apply what they have learned in each unit by taking the quizzes provided in the learning path. • Video Lessons: Each unit is accompanied by video lessons that help to explain important unit concepts. These videos were created and narrated by the author. • Interactive Study Aids: Flashcards and crossword puzzles help users review main concepts from the units and coding Snippets allow students to practice key coding concepts. Instructor Companion site The following teaching tools are available for download at the Companion Site for this text. Simply search for this text at www.cengagebrain.com and choose “Instructor Downloads.” An instructor login is required. P R E FA C E 97070_fm_hr_i-xxiv.indd 17 27/02/18 7:34 pm
xviii AcknowledgmentsP R E FA C E • Instructor’s Manual: The Instructor’s Manual that accompanies this textbook includes additional instructional material to assist in class preparation, including items such as Overviews, Chapter Objectives, Teaching Tips, Quick Quizzes, Class Discussion Topics, Additional Projects, Additional Resources, and Key Terms. A sample syllabus also is available. • Test Bank: Cengage Testing Powered by Cognero is a flexible, online system that allows you to: ° Author, edit, and manage test bank content from multiple Cengage solutions. ° Create multiple test versions in an instant. ° Deliver tests from your LMS, your classroom, or wherever you want. • PowerPoint Presentations: This text provides PowerPoint slides to accompany each chapter. Slides can be used to guide classroom presentations, to make available to students for chapter review, or to print as classroom handouts. • Student Files: Files are provided for every figure in the text. Instructors can use the files to customize PowerPoint slides, illustrate quizzes, or create handouts. • Solutions: Solutions to all programming exercises are available. If an input file is needed to run a programming exercise, it is included with the solution file. • Data Files: Data files necessary to complete the steps and projects in the book are available at www.cengagebrain.com, or your instructor will provide the data files to you. Acknowledgments I would like to thank all of the people who helped to make this book a reality, including Natalie Onderdonk, Learning Designer; Michele Stulga, Content Project Manager; and John Freitas, Quality Assurance Tester. I am lucky to work with these professionals who are dedicated to producing high-quality instructional materials. I am also grateful to the reviewers who provided comments and encouragement during this book’s development, including Cliff Brozo, Monroe College; Fred D’Angelo, University of Arizona; Cassandra Henderson, Albany Technical College; Zack Hubbard, Rowan-Cabarrus Community College; and Sandra Lavallee, Lakes Region Community College. Thanks, too, to my husband, Geoff, for his constant support, advice, and encouragement. Finally, this book is dedicated to George Edward Farrell Peterson and Clifford Geoffrey Farrell Peterson. You each had a book dedicated to you earlier, but those books were published before I knew your names. Now you are here, and I love you! Joyce Farrell 97070_fm_hr_i-xxiv.indd 18 27/02/18 7:34 pm
The above is a preview of the first 20 pages. Register to read the complete e-book.