logo资料库

Introduction to Java Programming (brief - 10th edition) .pdf

第1页 / 共802页
第2页 / 共802页
第3页 / 共802页
第4页 / 共802页
第5页 / 共802页
第6页 / 共802页
第7页 / 共802页
第8页 / 共802页
资料共802页,剩余部分请下载后查看
Cover
Title
Copyright
Brief Contents
Contents
Chapter 1 Introduction to Computers, Programs,and Java
1.1 Introduction
1.2 What Is a Computer?
1.3 Programming Languages
1.4 Operating Systems
1.5 Java, the World Wide Web, and Beyond
1.6 The Java Language Specification, API, JDK, and IDE
1.7 A Simple Java Program
1.8 Creating, Compiling, and Executing a Java Program
1.9 Programming Style and Documentation
1.10 Programming Errors
1.11 Developing Java Programs Using NetBeans
1.12 Developing Java Programs Using Eclipse
Chapter 2 Elementary Programming
2.1 Introduction
2.2 Writing a Simple Program
2.3 Reading Input from the Console
2.4 Identifiers
2.5 Variables
2.6 Assignment Statements and Assignment Expressions
2.7 Named Constants
2.8 Naming Conventions
2.9 Numeric Data Types and Operations
2.10 Numeric Literals
2.11 Evaluating Expressions and Operator Precedence
2.12 Case Study: Displaying the Current Time
2.13 Augmented Assignment Operators
2.14 Increment and Decrement Operators
2.15 Numeric Type Conversions
2.16 Software Development Process
2.17 Case Study: Counting Monetary Units
2.18 Common Errors and Pitfalls
Chapter 3 Selections
3.1 Introduction
3.2 boolean Data Type
3.3 if Statements
3.4 Two-Way if-else Statements
3.5 Nested if and Multi-Way if-else Statements
3.6 Common Errors and Pitfalls
3.7 Generating Random Numbers
3.8 Case Study: Computing Body Mass Index
3.9 Case Study: Computing Taxes
3.10 Logical Operators
3.11 Case Study: Determining Leap Year
3.12 Case Study: Lottery
3.13 switch Statements
3.14 Conditional Expressions
3.15 Operator Precedence and Associativity
3.16 Debugging
Chapter 4 Mathematical Functions, Characters,and Strings
4.1 Introduction
4.2 Common Mathematical Functions
4.3 Character Data Type and Operations
4.4 The String Type
4.5 Case Studies
4.6 Formatting Console Output
Chapter 5 Loops
5.1 Introduction
5.2 The while Loop
5.3 The do-while Loop
5.4 The for Loop
5.5 Which Loop to Use?
5.6 Nested Loops
5.7 Minimizing Numeric Errors
5.8 Case Studies
5.9 Keywords break and continue
5.10 Case Study: Checking Palindromes
5.11 Case Study: Displaying Prime Numbers
Chapter 6 Methods
6.1 Introduction
6.2 Defining a Method
6.3 Calling a Method
6.4 void Method Example
6.5 Passing Arguments by Values
6.6 Modularizing Code
6.7 Case Study: Converting Hexadecimals to Decimals
6.8 Overloading Methods
6.9 The Scope of Variables
6.10 Case Study: Generating Random Characters
6.11 Method Abstraction and Stepwise Refinement
Chapter 7 Single-Dimensional Arrays
7.1 Introduction
7.2 Array Basics
7.3 Case Study: Analyzing Numbers
7.4 Case Study: Deck of Cards
7.5 Copying Arrays
7.6 Passing Arrays to Methods
7.7 Returning an Array from a Method
7.8 Case Study: Counting the Occurrences of Each Letter
7.9 Variable-Length Argument Lists
7.10 Searching Arrays
7.11 Sorting Arrays
7.12 The Arrays Class
7.13 Command-Line Arguments
Chapter 8 Multidimensional Arrays
8.1 Introduction
8.2 Two-Dimensional Array Basics
8.3 Processing Two-Dimensional Arrays
8.4 Passing Two-Dimensional Arrays to Methods
8.5 Case Study: Grading a Multiple-Choice Test
8.6 Case Study: Finding the Closest Pair
8.7 Case Study: Sudoku
8.8 Multidimensional Arrays
Chapter 9 Objects and Classes
9.1 Introduction
9.2 Defining Classes for Objects
9.3 Example: Defining Classes and Creating Objects
9.4 Constructing Objects Using Constructors
9.5 Accessing Objects via Reference Variables
9.6 Using Classes from the Java Library
9.7 Static Variables, Constants, and Methods
9.8 Visibility Modifiers
9.9 Data Field Encapsulation
9.10 Passing Objects to Methods
9.11 Array of Objects
9.12 Immutable Objects and Classes
9.13 The Scope of Variables
9.14 The this Reference
Chapter 10 Object-Oriented Thinking
10.1 Introduction
10.2 Class Abstraction and Encapsulation
10.3 Thinking in Objects
10.4 Class Relationships
10.5 Case Study: Designing the Course Class
10.6 Case Study: Designing a Class for Stacks
10.7 Processing Primitive Data Type Values as Objects
10.8 Automatic Conversion between Primitive Types and Wrapper Class Types
10.9 The BigInteger and BigDecimal Classes
10.10 The String Class
10.11 The StringBuilder and StringBuffer Classes
Chapter 11 Inheritance and Polymorphism
11.1 Introduction
11.2 Superclasses and Subclasses
11.3 Using the super Keyword
11.4 Overriding Methods
11.5 Overriding vs. Overloading
11.6 The Object Class and Its toString() Method
11.7 Polymorphism
11.8 Dynamic Binding
11.9 Casting Objects and the instanceof Operator
11.10 The Object’s equals Method
11.11 The ArrayList Class
11.12 Useful Methods for Lists
11.13 Case Study: A Custom Stack Class
11.14 The protected Data and Methods
11.15 Preventing Extending and Overriding
Chapter 12 Exception Handling and Text I/O
12.1 Introduction
12.2 Exception-Handling Overview
12.3 Exception Types
12.4 More on Exception Handling
12.5 The finally Clause
12.6 When to Use Exceptions
12.7 Rethrowing Exceptions
12.8 Chained Exceptions
12.9 Defining Custom Exception Classes
12.10 The File Class
12.11 File Input and Output
12.12 Reading Data from the Web
12.13 Case Study: Web Crawler
Chapter 13 Abstract Classes and Interfaces
13.1 Introduction
13.2 Abstract Classes
13.3 Case Study: the Abstract Number Class
13.4 Case Study: Calendar and GregorianCalendar
13.5 Interfaces
13.6 The Comparable Interface
13.7 The Cloneable Interface
13.8 Interfaces vs. Abstract Classes
13.9 Case Study: The Rational Class
13.10 Class Design Guidelines
Chapter 14 JavaFX Basics
14.1 Introduction
14.2 JavaFX vs Swing and AWT
14.3 The Basic Structure of a JavaFX Program
14.4 Panes, UI Controls, and Shapes
14.5 Property Binding
14.6 Common Properties and Methods for Nodes
14.7 The Color Class
14.8 The Font Class
14.9 The Image and ImageView Classes
14.10 Layout Panes
14.11 Shapes
14.12 Case Study: The ClockPane Class
Chapter 15 Event-Driven Programming and Animations
15.1 Introduction
15.2 Events and Event Sources
15.3 Registering Handlers and Handling Events
15.4 Inner Classes
15.5 Anonymous Inner Class Handlers
15.6 Simplifying Event Handling Using Lambda Expressions
15.7 Case Study: Loan Calculator
15.8 Mouse Events
15.9 Key Events
15.10 Listeners for Observable Objects
15.11 Animation
15.12 Case Study: Bouncing Ball
Chapter 16 JavaFX UI Controls and Multimedia
16.1 Introduction
16.2 Labeled and Label
16.3 Button
16.4 CheckBox
16.5 RadioButton
16.6 TextField
16.7 TextArea
16.8 ComboBox
16.9 ListView
16.10 ScrollBar
16.11 Slider
16.12 Case Study: Developing a Tic-Tac-Toe Game
16.13 Video and Audio
16.14 Case Study: National Flags and Anthems
Chapter 17 Binary I/O
17.1 Introduction
17.2 How Is Text I/O Handled in Java?
17.3 Text I/O vs. Binary I/O
17.4 Binary I/O Classes
17.5 Case Study: Copying Files
17.6 Object I/O
17.7 Random-Access Files
Chapter 18 Recursion
18.1 Introduction
18.2 Case Study: Computing Factorials
18.3 Case Study: Computing Fibonacci Numbers
18.4 Problem Solving Using Recursion
18.5 Recursive Helper Methods
18.6 Case Study: Finding the Directory Size
18.7 Case Study: Tower of Hanoi
18.8 Case Study: Fractals
18.9 Recursion vs. Iteration
18.10 Tail Recursion
Appendix A Java Keywords
Appendix B The ASCII Character Set
Appendix C Operator Precedence Chart
Appendix D Java Modifiers
Appendix E Special Floating-Point Values
Appendix F Number Systems
Appendix G Bitwise Operations
Appendix H Regular Expressions
Appendix I Enumerated Types
Index
Symbols
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
ONLINE ACCESS Thank you for purchasing a new copy of Introduction to Java Programming, Brief Version, Tenth Edition. Your textbook includes six months of prepaid access to the book’s Companion Website. Your textbook includes six months of prepaid access to the book’s Companion Website. This prepaid subscription provides you with full access to the following student support areas: • Video Notes are step-by-step video tutorials specifically designed to enhance the programming concepts presented in this textbook Use a coin to scratch off the coating and reveal your student access code. Do not use a knife or other sharp object as it may damage the code. To access the Introduction to Java Programming, Brief Version, Tenth Edition, Companion Website for the first time, you will need to register online using a computer with an Internet connection and a web browser. The process takes just a couple of minutes and only needs to be completed once. 1. Go to www.pearsonglobaleditions.com/Liang 2. Click on Companion Website. 3. Click on the Register button. 4. On the registration page, enter your student access code* found beneath the scratch-off panel. Do not type the dashes. You can use lower - or uppercase. 5. Follow the on-screen instructions. If you need help at any time during the online registration process, simply click the Need Help? icon. 6. Once your personal Login Name and Password are confirmed, you can begin using the Introduction to Introduction to Java Programming, Brief Version Companion Website! To log in after you have registered: You only need to register for this Companion Website once. After that, you can log in any time at www.pearsonglobaleditions.com/Liang by providing your Login Name and Password when prompted. *Important: The access code can only be used once. This subscription is valid for six months upon activation and is not transferable. If this access code has already been revealed, it may no longer be valid. If this is the case, you can purchase a subscription by going to www.pearsonglobaleditions.com/Liang and following the on-screen instructions.
IntroductIon to Java ® ProgrammIng BrIef versIon Tenth Edition Global Edition Y. daniel Liang Armstrong Atlantic State University Global Edition contributions by ming-Jyh tsai Fu Jen Catholic University Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo A01_LIAN8564_10_GE_FM.indd 1 05/09/14 10:16 am
To Samantha, Michael, and Michelle Editorial Director, ECS: Marcia Horton Head of Learning Assets Acquisition, Global Editions: Laura Dent Executive Editor: Tracy Johnson (Dunkelberger) Editorial Assistant: Jenah Blitz-Stoehr Project Director, Global Editions: Shona Mullen Assistant Project Editor, Global Editions: Paromita Banerjee Director of Marketing: Christy Lesko Marketing Manager: Yez Alayan Marketing Assistant: Jon Bryant Director of Program Management: Erin Gregg Senior Manufacturing Controller, Global Editions: Trudy Kimber Program Management-Team Lead: Scott Disanno Program Manager: Carole Snyder Project Management-Team Lead: Laura Burgess Project Manager: Robert Engelhardt Procurement Specialist: Linda Sager Cover Designer: Lumina Datamatics Ltd Permissions Supervisor: Michael Joyce Permissions Administrator: Jenell Forschler Director, Image Asset Services: Annie Atherton Manager, Visual Research: Karen Sanatar Cover Art: © Ints Vikmanis/Shutterstock Media Project Manager: Renata Butera Media Producer, Global Editions: Pallavi Pandit Full-Service Project Management: Laserwords Private Ltd. Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on the appropriate page within text. Microsoft® and Windows® are registered trademarks of the Microsoft Corporation in the U.S.A. and other countries. Screen shots and icons reprinted with permission from the Microsoft Corporation. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation. Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsonglobaleditions.com © Pearson Education Limited 2015 The rights of Y. Daniel Liang to be identified as the author of this work have been asserted by them in accordance with the Copyright, Designs and Patents Act 1988. Authorized adaptation from the United States edition, entitled to Introduction to Java Programming, Brief Version, 10th Edition, ISBN 978-0-13-359220-7 by Y. Daniel Liang, published by Pearson Education © 2015. All rights reserved. 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 or otherwise, without either the prior written permission of the publisher or a license permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6–10 Kirby Street, London EC1N 8TS. All trademarks used herein are the property of their respective owners. The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affiliation with or endorsement of this book by such owners. ISBN 10: 1-292-07856-1 ISBN 13: 978-1-292-07856-4 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library 10 9 8 7 6 5 4 3 2 1 15 14 13 12 11 Typeset in 10/12 Times LT Std by Laserwords Private Ltd Printed and bound by Courier Kendallville in United States of America A01_LIAN8564_10_GE_FM.indd 2 05/09/14 10:16 am
Preface Dear Reader, Many of you have provided feedback on earlier editions of this book, and your comments and suggestions have greatly improved the book. This edition has been substantially enhanced in presentation, organization, examples, exercises, and supplements. The new edition: n Replaces Swing with JavaFX. JavaFX is a new framework for developing Java GUI pro- what is new? grams. JavaFX greatly simplifies GUI programming and is easier to learn than Swing. n Introduces exception handling, abstract classes, and interfaces before GUI programming to enable the GUI chapters to be skipped completely if the instructor chooses not to cover GUI. n Covers introductions to objects and strings earlier in Chapter 4 to enable students to use objects and strings to develop interesting programs early. n Includes many new interesting examples and exercises to stimulate student interests. More than 100 additional programming exercises are provided to instructors only on the Com- panion Website. Please visit www.pearsonglobaleditions.com/Liang for a complete list of new features as well as correlations to the previous edition. The book is fundamentals first by introducing basic programming concepts and techniques before designing custom classes. The fundamental concepts and techniques of selection statements, loops, methods, and arrays are the foundation for programming. Building this strong foundation prepares students to learn object-oriented programming and advanced Java programming. This book teaches programming in a problem-driven way that focuses on problem solving rather than syntax. We make introductory programming interesting by using thought-provok- ing problems in a broad context. The central thread of early chapters is on problem solving. Appropriate syntax and library are introduced to enable readers to write programs for solving the problems. To support the teaching of programming in a problem-driven way, the book provides a wide variety of problems at various levels of difficulty to motivate students. To appeal to students in all majors, the problems cover many application areas, including math, science, business, financial, gaming, animation, and multimedia. The book is widely used in the introductory programming courses in the universities around the world. The book is a brief version of Introduction to Java Programming, Comprehensive Version, Tenth Edition, Global Edition. This version is designed for an introductory pro- gramming course, commonly known as CS1. It contains the first eighteen chapters in the comprehensive version and covers fundamentals of programming, object-oriented program- ming, GUI programming, exception handling, I/O, and recursion. The comprehensive ver- sion has additional twenty-four chapters that cover data structures, algorithms, concurrency, parallel programming, networking, internationalization, advanced GUI, database, and Web programming. The first thirteen chapters of this book are appropriate for preparing the AP Computer Science exam. The best way to teach programming is by example, and the only way to learn program- ming is by doing. Basic concepts are explained by example and a large number of exercises with various levels of difficulty are provided for students to practice. For our programming courses, we assign programming exercises after each lecture. fundamentals-first problem-driven brief version comprehensive version AP Computer Science examples and exercises 3 A01_LIAN8564_10_GE_FM.indd 3 05/09/14 10:16 am
4 Preface Our goal is to produce a text that teaches problem solving and programming in a broad context using a wide variety of interesting examples. If you have any comments on and sug- gestions for improving the book, please email me. Sincerely, Y. Daniel Liang y.daniel.liang@gmail.com www.cs.armstrong.edu/liang www.pearsonglobaleditions.com/Liang ACM/IEEE Curricular 2013 and ABET Course Assessment The new ACM/IEEE Computer Science Curricular 2013 defines the Body of Knowledge organ- ized into 18 Knowledge Areas. To help instructors design the courses based on this book, we provide sample syllabi to identify the Knowledge Areas and Knowledge Units. The sample syl- labi are for a three semester course sequence and serve as an example for institutional customi- zation. The sample syllabi are available to instructors at www.pearsonglobaleditions.com/Liang. Many of our users are from the ABET-accredited programs. A key component of the ABET accreditation is to identify the weakness through continuous course assessment against the course outcomes. We provide sample course outcomes for the courses and sample exams for measuring course outcomes on the instructor Website accessible from www.pearsonglobaleditions.com/Liang. What’s New in This Edition? This edition is completely revised in every detail to enhance clarity, presentation, content, examples, and exercises. The major improvements are as follows: n Updated to Java 8. n Since Swing is replaced by JavaFX, all GUI examples and exercises are revised using JavaFX. n Lambda expressions are used to simplify coding in JavaFX and threads. n More than 100 additional programming exercises with solutions are provided to the instructor on the Companion Website. These exercises are not printed in the text. n Math methods are introduced earlier in Chapter 4 to enable students to write code using math functions. n Strings are introduced earlier in Chapter 4 to enable students to use objects and strings to develop interesting programs early. n The GUI chapters are moved to after abstract classes and interfaces so that these chapters can be easily skipped if the instructor chooses not to cover GUI. n Chapters 4, 14, 15, and 16 are brand new chapters. A01_LIAN8564_10_GE_FM.indd 4 05/09/14 10:16 am
Preface 5 Pedagogical Features The book uses the following elements to help students get the most from the material: n The Objectives at the beginning of each chapter list what students should learn from the chapter. This will help them determine whether they have met the objectives after complet- ing the chapter. n The Introduction opens the discussion with representative problems to give the reader an overview of what to expect from the chapter. n Key Points highlight the important concepts covered in each section. n Check Points provide review questions to help students track their progress as they read through the chapter and evaluate their learning. n Problems and Case Studies, carefully chosen and presented in an easy-to-follow style, teach problem solving and programming concepts. The book uses many small, simple, and stimulating examples to demonstrate important ideas. n The Chapter Summary reviews the important subjects that students should understand and remember. It helps them reinforce the key concepts they have learned in the chapter. n Quizzes are accessible online, grouped by sections, for students to do self-test on pro- gramming concepts and techniques. n Programming Exercises are grouped by sections to provide students with opportunities to apply the new skills they have learned on their own. The level of difficulty is rated as easy (no asterisk), moderate (*), hard (**), or challenging (***). The trick of learning pro- gramming is practice, practice, and practice. To that end, the book provides a great many exercises. Additionally, more than 100 programming exercises with solutions are provided to the instructors on the Companion Website. These exercises are not printed in the text. n Notes, Tips, Cautions, and Design Guides are inserted throughout the text to offer valu- able advice and insight on important aspects of program development. Note Provides additional information on the subject and reinforces important concepts. Tip Teaches good programming style and practice. Caution Helps students steer away from the pitfalls of programming errors. Design Guide Provides guidelines for designing programs. Flexible Chapter Orderings The book is designed to provide flexible chapter orderings to enable GUI, exception handling, and recursion to be covered earlier or later. The diagram on the next page shows the chapter dependencies. A01_LIAN8564_10_GE_FM.indd 5 05/09/14 10:16 am
Part II: Object-Oriented Programming Part III: GUI Programming Chapter 9 Objects and Classes Chapter 14 JavaFX Basics Chapter 15 Event-Driven Programming and Animations Chapter 16 JavaFX Controls and Multimedia Chapter 10 Thinking in Objects Chapter 11 Inheritance and Polymorphism Chapter 12 Exception Handling and Text I/O Chapter 13 Abstract Classes and Interfaces Chapter 17 Binary I/O 6 Preface Part I: Fundamentals of Programming Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Elementary Programming Chapter 3 Selections Chapter 4 Mathematical Functions, Characters, and Strings Chapter 5 Loops Chapter 6 Methods Chapter 7 Single-Dimensional Arrays Chapter 8 Multidimensional Arrays Chapter 18 Recursion Organization of the Book The chapters in this brief version can be grouped into three parts that, taken together, form a solid introduction to Java programming. Because knowledge is cumulative, the early chapters provide the conceptual basis for understanding programming and guide students through simple examples and exercises; subsequent chapters progressively present Java program- ming in detail, culminating with the development of comprehensive Java applications. The appendixes contain a mixed bag of topics, including an introduction to number systems, bitwise operations, regular expressions, and enumerated types. Part I: Fundamentals of Programming (Chapters 1–8, 18) The first part of the book is a stepping stone, preparing you to embark on the journey of learning Java. You will begin to learn about Java (Chapter 1) and fundamental programming techniques with primitive data types, variables, constants, assignments, expressions, and operators ( Chapter 2), selection statements (Chapter 3), mathematical functions, characters, and strings (Chapter 4), loops (Chapter 5), methods (Chapter 6), and arrays (Chapters 7–8). After Chapter 7, you can jump to Chapter 18 to learn how to write recursive methods for solving inherently recursive problems. Part II: Object-Oriented Programming (Chapters 9–13, and 17) This part introduces object-oriented programming. Java is an object-oriented programming language that uses abstraction, encapsulation, inheritance, and polymorphism to provide great flexibility, modularity, and reusability in developing software. You will learn program- ming with objects and classes (Chapters 9–10), class inheritance (Chapter 11), polymorphism A01_LIAN8564_10_GE_FM.indd 6 05/09/14 10:16 am
分享到:
收藏