1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Java Programming Fifth Edition _ www.bit.ly/taiho123

908 1,5K 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 908
Dung lượng 16,32 MB

Nội dung

www.traintelco.com Java Programming Fifth Edition Joyce Farrell Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States www.traintelco.com Java Programming, Fifth Edition Joyce Farrell Executive Editor: Marie Lee Acquisitions Editor: Amy Jollymore Managing Editor: Tricia Coia Development Editor: Dan Seiter Editorial Assistant: Julia Leroux-Lindsey Marketing Manager: Bryant Chrzan Content Project Manager: Heather Furrow Art Director: Marissa Falco Cover Designer: Bruce Bond Cover Photo: TBD © 2010 Course Technology, Cengage Learning ALL RIGHTS RESERVED No part of this work covered by the copyright herein may be reproduced, transmitted, stored, or used in any form or by any means— graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act—without the prior written permission of the publisher For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706 For permission to use material from this text or product, submit all requests online at www.cengage.com/permissions Further permissions questions can be e-mailed to permissionrequest@cengage.com Microsoft® is a registered trademark of the Microsoft Corporation Manufacturing Coordinator: Julio Esperas ISBN-13: 978-0-3245-9951-0 Proofreader: Andrea Schein ISBN-10: 0-3245-9951-X Indexer: Elizabeth Cunningham Compositor: International Typesetting and Composition Course Technology 25 Thomson Place Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan Locate your local office at: www.international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd To learn more about Course Technology, visit www.cengage.com/coursetechnology Purchase any of our products at your local bookstore or at our preferred online store www.ichapters.com Some of the product names and company names used in this book have been used for identification purposes only and may be trademarks or registered trademarks of their respective manufacturers and sellers Course Technology, a part of Cengage Learning, reserves the right to revise this publication and make changes from time to time in its content without notice Printed in Canada 12 11 10 09 08 www.traintelco.com BRIEF CONTENTS PREFACE xix READ THIS BEFORE YOU BEGIN xxv CHAPTER CREATING YOUR FIRST JAVA CLASSES CHAPTER USING DATA WITHIN A PROGRAM 43 CHAPTER USING METHODS, CLASSES, AND OBJECTS 89 CHAPTER MORE OBJECT CONCEPTS 135 CHAPTER MAKING DECISIONS 187 CHAPTER LOOPING 233 CHAPTER CHARACTERS, STRINGS, AND THE STRINGBUILDER 273 CHAPTER ARRAYS 309 CHAPTER INTRODUCTION TO INHERITANCE 369 CHAPTER 10 ADVANCED INHERITANCE CONCEPTS 413 CHAPTER 11 EXCEPTION HANDLING 461 CHAPTER 12 FILE INPUT AND OUTPUT 525 CHAPTER 13 INTRODUCTION TO SWING COMPONENTS 587 CHAPTER 14 ADVANCED GUI TOPICS 641 CHAPTER 15 GRAPHICS 709 CHAPTER 16 APPLETS, IMAGES, AND SOUND 763 APPENDIX A WORKING WITH THE JAVA PLATFORM 807 APPENDIX B LEARNING ABOUT ASCII AND UNICODE 815 APPENDIX C FORMATTING OUTPUT 821 APPENDIX D GENERATING RANDOM NUMBERS 833 APPENDIX E JAVADOC 839 GLOSSARY 847 INDEX 867 iii www.traintelco.com This page intentionally left blank www.traintelco.com CONTENTS PREFACE xix READ THIS BEFORE YOU BEGIN xxv CHAPTER CREATING YOUR FIRST JAVA CLASSES LEARNING ABOUT PROGRAMMING INTRODUCING OBJECT-ORIENTED PROGRAMMING CONCEPTS Procedural Programming Object-Oriented Programming Understanding Objects, Classes, and Encapsulation Understanding Inheritance and Polymorphism 4 LEARNING ABOUT JAVA Java Program Types ANALYZING A JAVA APPLICATION THAT USES CONSOLE OUTPUT Understanding the Statement That Prints the Output Understanding the First Class Understanding the main() Method 10 10 11 14 ADDING COMMENTS TO A JAVA CLASS 16 SAVING, COMPILING, RUNNING, AND MODIFYING A JAVA APPLICATION Saving a Java Class Compiling a Java Class Running a Java Application Modifying a Java Class 18 18 18 19 19 CREATING A JAVA APPLICATION USING GUI OUTPUT 21 CORRECTING ERRORS AND FINDING HELP 23 YOU DO IT Your First Application Adding Comments to a Class Modifying a Class Creating a Dialog Box 26 26 27 28 29 DON’T DO IT 30 KEY TERMS 31 CHAPTER SUMMARY 34 REVIEW QUESTIONS 35 EXERCISES 37 v www.traintelco.com CONTENTS DEBUGGING EXERCISES 39 GAME ZONE 39 TOUGH QUESTIONS 40 UP FOR DISCUSSION 41 CHAPTER USING DATA WITHIN A PROGRAM 43 USING CONSTANTS AND VARIABLES Declaring Variables Declaring Named Constants Pitfall: Forgetting That a Variable Holds One Value at a Time 44 45 46 48 LEARNING ABOUT THE int DATA TYPE 48 DISPLAYING DATA 50 WRITING ARITHMETIC STATEMENTS Writing Arithmetic Statements Efficiently 51 53 USING THE BOOLEAN DATA TYPE 54 LEARNING ABOUT FLOATING-POINT DATA TYPES 55 UNDERSTANDING NUMERIC-TYPE CONVERSION 56 WORKING WITH THE char DATA TYPE 58 USING THE Scanner CLASS FOR KEYBOARD INPUT Pitfall: Using nextLine() Following One of the Other Scanner Input Methods 61 USING THE JOptionPane CLASS FOR GUI INPUT Using Input Dialog Boxes Using Confirm Dialog Boxes 66 66 70 YOU DO IT Working with Numeric Values Accepting User Data Performing Arithmetic Experimenting with Java Programs 72 72 73 74 75 DON’T DO IT 76 KEY TERMS 77 CHAPTER SUMMARY 80 REVIEW QUESTIONS 81 EXERCISES 83 DEBUGGING EXERCISES 86 GAME ZONE 86 TOUGH QUESTIONS 86 UP FOR DISCUSSION 87 vi www.traintelco.com 63 CONTENTS CHAPTER USING METHODS, CLASSES, AND OBJECTS 89 CREATING METHODS WITH ZERO, ONE, AND MULTIPLE PARAMETERS Creating Methods That Require a Single Parameter Creating Methods That Require Multiple Parameters 90 94 97 CREATING METHODS THAT RETURN VALUES Calling a Method from Another Method 99 101 LEARNING ABOUT CLASS CONCEPTS 102 CREATING A CLASS 104 CREATING INSTANCE METHODS IN A CLASS 106 DECLARING OBJECTS AND USING THEIR METHODS Understanding Data Hiding 109 110 ORGANIZING CLASSES 112 AN INTRODUCTION TO USING CONSTRUCTORS 114 UNDERSTANDING THAT CLASSES ARE DATA TYPES 116 YOU DO IT Creating a Static Method That Requires No Arguments and Returns No Values Calling a Static Method from Another Class Creating a Static Method That Accepts Arguments and Returns Values Creating a Class That Contains Instance Fields and Methods Creating a Class That Instantiates Objects of Another Class Adding a Constructor to a Class Creating a More Complete Class 118 118 119 120 122 123 124 124 DON’T DO IT 125 KEY TERMS 125 CHAPTER SUMMARY 127 REVIEW QUESTIONS 128 EXERCISES 131 DEBUGGING EXERCISES 133 GAME ZONE 133 TOUGH QUESTIONS 134 UP FOR DISCUSSION 134 CHAPTER MORE OBJECT CONCEPTS 135 UNDERSTANDING BLOCKS AND SCOPE 136 OVERLOADING A METHOD 142 LEARNING ABOUT AMBIGUITY 144 SENDING ARGUMENTS TO CONSTRUCTORS 147 OVERLOADING CONSTRUCTORS 148 vii www.traintelco.com CONTENTS LEARNING ABOUT THE this REFERENCE Using the this Reference to Make Overloaded Constructors More Efficient 149 152 USING static VARIABLES 154 USING CONSTANT FIELDS 156 USING AUTOMATICALLY IMPORTED, PREWRITTEN CONSTANTS AND METHODS 157 USING AN EXPLICITLY IMPORTED PREWRITTEN CLASS AND ITS METHODS 160 UNDERSTANDING COMPOSITION 164 A BRIEF LOOK AT NESTED AND INNER CLASSES 166 YOU DO IT Demonstrating Scope Overloading Methods Creating a Constructor That Requires an Argument Using an Explicitly Imported Prewritten Class Creating an Interactive Application with a Timer 168 168 170 171 172 174 DON’T DO IT 176 KEY TERMS 176 CHAPTER SUMMARY 177 REVIEW QUESTIONS 178 EXERCISES 181 DEBUGGING EXERCISES 184 GAME ZONE 184 TOUGH QUESTIONS 185 UP FOR DISCUSSION 185 CHAPTER MAKING DECISIONS 187 UNDERSTANDING DECISION MAKING 188 MAKING DECISIONS WITH THE if AND if else STRUCTURES Pitfall: Misplacing a Semicolon in an if Statement Pitfall: Using the Assignment Operator Instead of the Equivalency Operator Pitfall: Attempting to Compare Objects Using the Relational Operators The if else Structure 190 191 192 192 193 USING MULTIPLE STATEMENTS IN AN if OR if else STRUCTURE 194 NESTING if AND if else STATEMENTS 197 USING LOGICAL AND and OR OPERATORS 199 MAKING ACCURATE AND EFFICIENT DECISIONS Using AND and OR Appropriately 202 205 USING THE switch STATEMENT 206 USING THE CONDITIONAL AND NOT OPERATORS Using the NOT Operator 209 210 UNDERSTANDING PRECEDENCE 211 viii www.traintelco.com CONTENTS YOU DO IT Using an if else Creating an Event Class to Use in a Decision-Making Application Writing an Application that Uses the Event class Using the switch Statement 213 213 215 216 218 DON’T DO IT 219 KEY TERMS 220 CHAPTER SUMMARY 221 REVIEW QUESTIONS 221 EXERCISES 224 DEBUGGING EXERCISES 229 GAME ZONE 229 TOUGH QUESTIONS 231 UP FOR DISCUSSION 232 CHAPTER LOOPING 233 LEARNING ABOUT THE LOOP STRUCTURE 234 USING A while LOOP TO CREATE A DEFINITE LOOP 235 USING A while LOOP TO CREATE AN INDEFINITE LOOP 239 USING SHORTCUT ARITHMETIC OPERATORS 243 USING A for LOOP 246 LEARNING HOW AND WHEN TO USE A while LOOP 248 LEARNING ABOUT NESTED LOOPS 250 IMPROVING LOOP PERFORMANCE Avoiding Unnecessary Operations Considering the Order of Evaluation of Short-Circuit Operators Comparing to Zero Employing Loop Fusion 252 253 253 254 255 YOU DO IT Writing a Loop to Validate Data Entries Working with Prefix and Postfix Increment Operators Working with Definite Loops Working with Nested Loops 256 256 257 259 260 DON’T DO IT 261 KEY TERMS 262 CHAPTER SUMMARY 263 REVIEW QUESTIONS 264 EXERCISES 267 DEBUGGING EXERCISES 269 ix www.traintelco.com INDEX Note: Boldface page numbers indicate definitions & (ampersand), 199, 212 (angle brackets), 30 * (asterisk), 17, 160 @ (at sign), 424, 840 \ (backslash), 17, 528 , (comma), 22, 485, 830 { } (curly braces), 14, 24, 26, 30, 122, 234, 236, 249, 467 $ (dollar sign), 12, 829 = (equal sign), 45, 190 ! (exclamation point), 211 / (forward slash), 17, 30, 528 ( ) (parentheses), 11, 13, 15, 30, 235, 311 % (percent sign), 826 (period), 436, 830 + (plus sign), 50, 284, 377 # (pound sign), 830 ; (semicolon), 30, 125, 161, 191, 219 [ ] (square brackets), 30 _ (underscore), 12 | (vertical bar), 200–201, 212 A abs() method, 159 abstract classes, 390, 414–417, 433, 437–441 abstract keyword, 415 accelerators, 685 access modifiers final access modifier, 96, 97 inheritance and, 383–384 package access modifier, 91, 384 private access modifier, 91, 105, 435, 845 protected access modifier, 91, 384, 435, 845 public access modifier, 12–14, 18, 91, 104, 382–384, 435, 845 AccessACharacter program, 549–551 accessor methods, 108 accumulating, use of the term, 243 AChildClass program, 401–402 acos()method, 159 action keys, 670 actionPerformed() method, 608–610, 625, 626, 652, 656, 668, 673, 712–713, 717, 738–741, 771–772, 778 add and assign operator, 243 add() method, 342, 594–595, 643, 644, 650, 657, 659 addActionListener() method, 608, 612 addItem() method, 619 addition (+) operator, 52–53 addressString variable, 290 addSeparator() method, 684 ADTs (abstract data types), 116 AgeCalculator program, 163 aggregation, 373 aGreeting variable, 279 AirlineDialog class, 70–71 Allman style, 14 ambiguity, 144–147 ampersand (&), 199, 212 AND operator, 247, 253 angle brackets (), 30 Animal class, 416–420, 423–425, 430, 437 anonymous classes, 166 AnyClassName class, 16 AParentClass program, 401 append() method, 291, 296 867 www.traintelco.com applet(s) adding images to, 775–777 described, 9, 764–766 life cycle, 786–790 overview, 763–806 running, 765–766, 783 Applet Viewer, 766 application(s) See also programs analyzing, 10–16 described, 4–5, files, 526 running, 19 writing, 26–28 architectural neutrality, arcs, drawing, 719, 721–722, 735 areas, copying, 725, 741–742 args identifier, 15 argument(s) See also parameters constructors requiring, 171–172 described, 11, 90 dialog boxes and, 67 index, optional, 829–830 inheritance and, 379–380, 398–400 methods that accept, 120–121 methods that require no, 118 passing, 11 sending, to constructors, 147–148 arithmetic operator(s) described, 51–52 shortcut, 243–245 using, 74–75 arithmetic statements, writing, 51–52 array(s) See also elements; subscripts creating, 344–345 declaring, 310–313 described, 310 initializing, 310–313, 345 INDEX array(s) (continued ) manipulating, 326–328 multidimensional, 334–337 of objects, 316–317, 347–350 one-dimensional, 334 overview, 309–368 parallel, 319–320, 346–347 passing, 323–326, 350–351 populating, 313, 344–345 ragged, 336 returning, from methods, 323–326 searching, 318–323 of subclass objects, 420–421 two-dimensional, 334–337 ArrayList class, 341–344 ArrayListDemo program, 342–343 Arrays class, 337–340, 351–353 ArraysDemo program, 338–339 ArraysDemo2 program, 339–340 ascending order, 328 ascent, use of the term, 729 ASCII (American Standard Code for Information Interchange), 815–819 assert statement, 494–497 assertions, 493–498 assignment operator, 45, 192, 219 associativity, 45 asterisk (*), 17, 160 at sign (@), 424, 840 attributes described, 5–7 rendering, 731–732 Automobile class, 6, 7, 429 AWTEvent class, 666, 671–673 B back buffer, 659 backslash ( / ), 17, 528 BankAccount class, 424–426 BankBalance program, 241, 248 BankBalanceVaryingInterest class, 251–252 BaseballPlayer class, 154, 385 BASIC, 192, 311 BasketballPlayer class, 388–390 batch processing, 548 binary data, 540 operators, 51–52 binarySearch() method, 339, 340, 352 blank final, use of the term, 46 blitting, 659 block(s) comments, 17, 30 described, 136–140 inside, 136 line transfer, 659 nested, 136 outside, 136 scope, 168–169 Boolean data type, 44, 54–55 Boolean expressions decisions and, 194–197 loops and, 238–239 Boolean values, 190 Boolean variables, 54 BorderLayout manager, 649–651, 686–688 BoxLayout manager, 657 break keyword, 207–209 bubble sort, 329–331 bubbleSort() method, 333 buffer(s) back, 659 described, 289 file I/O and, 534 BufferedInputStream class, 533, 534 buffering, double, 659 BusinessLetter program, 286 ButtonGroup class, 618–619 byte data type, 44, 48–49 byte keyword, 44 bytecode, 868 www.traintelco.com C C++ (high-level language), 414, 477 Boolean values and, 56 named constants and, 46 punctuation and, 161 syntax rules and, C# (high-level language), 46, 477 calculateAge() method, 121 calculateBonus() method, 102 calculateInterest() method, 142–143, 146–147 calculateInterestUsingDouble() method, 144 calculateInterestUsingInt() method, 144 calculatePay() method, 421 calculateRaise() method, 101 CalendarDemo program, 174 call stack, 486–489 called method, 90, 94 calling method, 90 CallSimpMeth program, 144–145 CallSimpMethAgain program, 146 camel casing, 45 canRead() method, 528 canWrite() method, 528 capacity, use of the term, 341 capacity() method, 289 CardGame class, 433 CardLayout manager, 655–656, 690–691 case keyword, 207–209 case-sensitivity, 23, 30 cast operator, 57 catch blocks, 467–471, 473–475, 481 catch clause, 538, 542 catch keyword, 467 catch or specify requirement, use of the phrase, 483 ceil() method, 159 char data type, 44, 58–61 char keyword, 44 Character class, 275, 276–278 INDEX characters, manipulating, 276–278 See also symbols charAt()method, 276, 283, 291, 339 Checkboard class, 662–663 CheckBoxDemonstration class, 615 check-digit, 111 child classes, creating, 399 ChooseManager program, 213–214, 256–257 ChooseManagerWithValidation class, 256 circle, radius of, 158 class(es) abstract, 390, 414–417, 433, 437–441 adding comments to, 16–17 base, 373 child, 373 clients, 103 concepts, learning about, 102–104 concrete, 414 creating, 1–42, 104–106, 122, 123, 501–503 definitions, derived, 373 described, 5–7 diagrams, 371–372 explicitly imported, 160–163, 172–174 extending, 374–375, 506–507 fragile, 384 identifiers, 12–13 inner, 166–167 instance methods in, creating, 106–108 -level Javadoc comments, 841–842 methods, 154 modifying, 19–20, 28–29 more complete, creating, 124–125 names, 12–13 nested, 166–167 organizing, 112–114 parent, 373 prewritten, 160–163, 172–174 saving, 18 used in a file of objects, 569–570 users, 103 virtual, 414 class keyword, 12, 104 class variables, 154 classpath variable, 810 clean build, 19 clearRect() method, 718 close() method, 549 COBOL, 192, 311 collisions, 437 colors, 647–648, 716, 737 Color class, 647–648 comes into scope, use of the term, 136 comma (,), 22, 485, 830 command prompt, 808–809 comment(s) adding, 16–17, 27–28 block, 17, 30 common mistakes related to, 30 described, 16 errors and, 23–24 Javadoc, 840–844 out, 16–17 CompareAccounts class, 427 CompareLoops program, 254–255 CompareStrings program, 280–281 compareTo() method, 282 comparison operators, 219 comparisonsToMake variable, 331 compiler(s) classes and, 18–19 described, 3, 811 errors, 23–24, 563 inheritance and, 385–386, 436 Component class, 589, 647 ComponentDemo program, 604–605 ComponentEvent class, 666–667 composition, 164–165, 373 compression, of data, 775 ComputeCommission program, 99 computer files, 526 See also files reference, 643, 646 869 www.traintelco.com concatenation, 50, 284 conditional operators, 200, 205–206, 209–211 conditional OR operator, 200, 205–206, 211–212 ConfirmDialog dialog box, 66–71 console applications, output, analyzing applications that use, 10–16 constant(s) automatically imported, 157–160 described, 44–49 fields, using, 156–157 named, 46–47 numeric, 44 prewritten, 157–160 storing, 434–345 constructor(s) adding, 124 arguments and, 398–400 calling, 377–379 default, 114–115, 125, 379 described, 109–110 efficiency of, 152–153 file I/O and, 528, 529–530, 536, 561 inheritance and, 377–379, 397–398 overloading, 148–149, 152–153 requiring arguments, 171–172, 379–380 sending arguments to, 147–148 StringBuilder class, 291 Swing components and, 590, 600, 604 consumed entries, 65 Container class, 648–649, 651 containment hierarchy, 642 content pane, 642–646 convenience class, 534 ConvertStringToInteger program, 287 copyArea() method, 725, 741–742 cos() method, 159 counting, 243 INDEX CreateEmployeeFile class, 541–544 CreateEmployeeFile program, 544 CreateEmployeeObjectFile class, 561–562 CreateEventObject program, 217–218 CreateEventObjectSwitch program, 218–219 CreateTwoMyFrameObjects program, 601 curly braces ({ }), 14, 24, 26, 30, 122, 234, 236, 249, 467 currentTimeMillis() method, 254, 292 Customer class, 380–381 CustomerAccount class, 491 D data See also data files; data types accepting user, 73–75 displaying, 50–51 fields, 105 formatted, 543–545 hiding, 110–111 reading, 543–545, 568–569 using, within programs, 43–88 validating, 241, 256–257 writing formatted, 540–543 data files described, 526 See also files organization, 530–532 streams, 530–532 data types See also specific data types abstract (ADTs), 116 arrays and, 312–313, 324–325, 337, 343 classes as, 116–117 described, 44 dialog boxes and, 67 file I/O and, 540, 553 inheritance and, 371 numeric-type conversion and, 56–57 DataInputStream class, 540, 543 DataOutputStream class, 540, 541 dates, comparing, 564–565 DAYS_IN_WEEK constant, 47 dead code, 101 debugging See also errors described, exception handling and, 497 toString() method and, 424 decimal places, specifying, 828 DecimalFormat class, 830–831 decision(s) accurate and efficient, 200–206 overview, 187–232 structure, 190 DeclareTwoEmployees program, 110, 111 default keyword, 207–209 DemoArray program, 345 DemoArray2 program, 345–346 DemoBlock class, 168–169 DemoBlock program, 169 DemoConstructors program, 378 DemoEntertainment program, 441 DemoIncrement2 program, 258 demoMethod() method, 168–169 DemoOverload class, 170–171 DemoOverload program, 170 DemoStackTrace class, 487 DemoStackTrace2 class, 488–489 DemoStringBuilder class, 296 DemoStringBuilder program, 297 DemoSuper program, 402 DemoVariables class, 72–76 DemoWorkingDogs program, 431–432 descending order, 328 descent, 729 deserialization, 560 Desk class, 102 destroy() method, 774, 786–790 deterministic, use of the term, 834 dialog boxes creating, 29 described, 21–22 870 www.traintelco.com DialogTimer program, 174–175 DialogTimer2 program, 175 DinnerEvent class, 393–395 DinnerEvent program, 396 DinnerEventWithConstructor program, 398, 399 DinnerEventWithConstructorArg program, 399–400 DinnerEventWithInfo program, 396, 398 dir command, 447 direct access files, 548 directories changing, 809–810 described, 526–527 display()method, 164–165, 172, 338, 381, 560, 562 DisplayDog program, 423–425 displayMenu() method, 502, 509–511 displayMetrics()method, 743–744 displayPrice()method, 480–482 displayPricingInfo() method, 395–397 divide and assign operator, 243 DivideEvenly program, 260 DivideEvenly2 program, 260–261 division, 52–53, 464, 823–825 Division class, 464–466 division ( / ) operator, 52–53 while loops, 234, 248–250 Dog class, 6–7, 11, 414–415, 420–421, 423–425, 430–431 dollar sign ($), 12, 829 DOS (Disk Operating System), 5, 161, 447 Double class, 288 double data type, 44 double keyword, 44 double parameter, 144–145 double-precision floating-point numbers, 55 doubleValue() method, 288 draw3DRect() method, 722 drawArc() method, 721–722 drawLine() method, 718–725 drawOval() method, 720–721 INDEX drawPolygon() method, 723–725 drawRect() method, 718 drawRoundRect() method, 719 drawString() method, 714–716, 736–737 dual-alternative if structure, 193 dummy values, 352 duplicate user entries, eliminating, 445–446 dynamic method binding, 418–420 dynamically resizable, use of the term, 341 E elements described, 311 loops and, 314–315 sorting, 328–334 else keyword, 193 EmpData.dat file, 542–53 Employee class, 12, 104–115, 140–141, 147–148, 149, 150, 311, 316–317, 332–333, 371–372, 376–379, 385, 420, 414, 420–421, 560, 563 EmployeeWithTerritory class, 372–375 empNum field, 147–148, 150 empty body, of loops, 238 statements, 191 encapsulation, 5–7 endcap styles, 733 endsWith() method, 283 EnterSmallValue program, 242 EntertainmentDatabase program, 444 EOF values, 534, 543, 544 equal sign (=), 45, 190 equal to (==) operator, 54–55 equals() method, 280, 281–282, 284, 425–428, 444–445 equalsIgnoreCase()method, 282 equivalency operator, 190–191, 192, 274 error(s) See also debugging; exceptions arrays and, 352 compiler, 18–19, 563 correcting, 23–25 data hiding and, 111 dialog boxes and, 71 file I/O and, 563 inheritance and, 384, 385–386, 415, 390, 392, 399, 441–442 logic, 24, 98 user input and, 65 Error class, 463, 464 ErrorTest class, 23–24 escape sequences, 59–60 event(s), 626–627, 785–786 described, 607 -driven programming, 607–614 handlers, 613, 666–671 mouse, 674–679 responding to, 608–611 source of, 607 Event class, 215–218, 346–347, 391–393, 396, 569–576 Event program, 216, 346–347, 392, 396 EventSite class, 122–125, 171–172 EventSite program, 122, 124, 171–172 EventWithConstructor program, 397–398 EventWithConstructorArg program, 398–399 EventWithInfo class, 396 EventWithInfo program, 397 Evergreen class, 373 exception(s) See also errors; exception handling catching, 467–481, 498–499, 504–505 classes that pass on, 502–503 creating, 490–493 described, 462–467 file I/O and, 538, 542–545, 560 runtime, 462 specification, 480–485 871 www.traintelco.com throwing, 471–475, 501–502, 506–507 tracing, 486–489 Exception class, 463–465, 468, 483–484, 509 exception handling See also exceptions advantages of, 478–480 checked, 483 described, 462 overview, 461–524 unchecked, 483 exclamation point (!), 211 executing programs, exists() method, 528, 536 explicit conversion, 57 extends keyword, 374–375, 430 F false keyword, 10, 12, 22 FAQs (frequently asked questions), 25 fault-tolerant applications, 467 field(s) constant, using, 156–157 data, 105 described, 103, 531 instance, 122 key, 551–555 nonstatic, 156 size, specifying, 828–829 file(s) See also file I/O (input/ output); filenames closing, 531 data, writing formatted, 540–543 dates, comparing, 564–565 opening, 531 organization, 530–532 pointers, 549 position pointers, 549 status, examining, 563–564 streams, 530–532 understanding, 526–527 writing to/reading from, 536–540, 559–563, 568–569 INDEX File class, 527–530, 563–564 file I/O (input/output) See also java.io package overview, 525–586 variable filenames and, 545–548 FileDemo class, 529 FileInputStream class, 533, 545 filename(s) changing, 811 extensions, 27, 435–436, 526, 766 variable, 545–548 FileOutputStream, 533, 536, 538, 540–541, 561, 568 fill() method, 338, 352 fill patterns, described, 731 fill3DRect() method, 722 fillArc() method, 722 fillOval() method, 720–721 fillPolygon() method, 724–725 fillRect() method, 718 fills, gradient, 731 final access modifier, 96, 97 final classes, 390–392 final keyword, 156, 389–390 final variable, 46 finally block, 476–478, 554 FindDiscount class, 322–323 FindPrice program, 319–320 First class, 11–14, 18–20, 23–24, 90, 93 FirstDialog class, 21–22 Fish class, 102 float data type, 44 float keyword, 44 floating-point data types, 55–56 floor() method, 159 FlowLayout manager, 598–599, 641–653, 688–689 flowcharts described, 188–189 loops and, 234 folders, 526–527 See also directories font(s) changing, 596–597 graphics and, 717, 726–731, 737, 742–746 height, 729 statistics, 729–730 Font class, 596 for keyword, 246 for loops arrays and, 318, 320, 330, 346, 349–350 described, 246–247 enhanced, 315 strings and, 294 format specifiers, 826 format strings, 826 forward slash ( / ), 17, 30, 528 fully qualified names, 437 fundamental classes, 158 G garbage value, 46 get() method, 111, 162, 172–173, 215, 384 getAvailableFontFamilyNames() method, 726 getComponent() method, 671 getContentPane() method, 643–645 getDate() method, 103 getDefaultToolkit() method, 728–729 getEmpNum() method, 106, 110–111, 150, 317 getFontMetrics() method, 729 getGraphics() method, 717 getItem() method, 617 getMessage() method, 490, getSource() method, 610 getText() method, 596 getTime() method, 103 getWindow() method, 671 glass pane, 642 goes out of scope, use of the term, 136 gradient(s) acyclic/cyclic, 731–732 fills, 731 graphic(s) See also images arcs, 721–722, 735 creating, 717 drawing with, 731–737 lines, 718–725, 734 ovals, 720–721, 734 overview, 709–762 polygons, 723–725, 735–736 rectangles, 722–723, 734 shapes, 718–725, 748–749 Graphics class, 710–717, 738–739 Graphics2D class, 717, 731, 734 greater than (>) operator, 54–55 greater than or equal to (

Ngày đăng: 12/10/2016, 12:29

TỪ KHÓA LIÊN QUAN