Full Introduction To Java Programming, Comprehensive Version 10th Edition - Ebook PDF Ebook All Chapters
Full Introduction To Java Programming, Comprehensive Version 10th Edition - Ebook PDF Ebook All Chapters
com
https://ebooksecure.com/download/introduction-to-
java-programming-comprehensive-version-ebook-pdf/
https://ebooksecure.com/product/ebook-pdf-introduction-to-java-
programming-and-data-structures-comprehensive-version-11/
ebooksecure.com
https://ebooksecure.com/product/ebook-pdf-introduction-to-java-
programming-and-data-structures-comprehensive-version-11th-global-
edition/
ebooksecure.com
https://ebooksecure.com/product/ebook-pdf-introduction-to-java-
programming-brief-version-global-edition-11th-edition/
ebooksecure.com
https://ebooksecure.com/product/ebook-pdf-human-resources-management-
in-canada-13th-canadian/
ebooksecure.com
(eBook PDF) Principles of Microeconomics by Betsey
Stevenson
https://ebooksecure.com/product/ebook-pdf-principles-of-
microeconomics-by-betsey-stevenson/
ebooksecure.com
https://ebooksecure.com/product/ebook-pdf-cfin-5th-edition-by-scott-
besley/
ebooksecure.com
https://ebooksecure.com/product/ebook-pdf-brief-c-late-objects-3rd-
edition/
ebooksecure.com
https://ebooksecure.com/product/ebook-pdf-encyclopedia-of-ocean-
sciences-3rd-edition/
ebooksecure.com
https://ebooksecure.com/product/research-methods-in-accounting-4th-
edition-ebook-pdf/
ebooksecure.com
(eBook PDF) Business Ethics Concepts and Cases 8th Edition
https://ebooksecure.com/product/ebook-pdf-business-ethics-concepts-
and-cases-8th-edition/
ebooksecure.com
INTRODUCTION TO
JAVA
®
PROGRAMMING
COMPREHENSIVE VERSION
Tenth Edition
Y. Daniel Liang
Armstrong Atlantic State 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
To Samantha, Michael, and Michelle
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.
Copyright © 2015, 2013, 2011 Pearson Education, Inc., publishing as Prentice Hall, 1 Lake Street, Upper Saddle
River, New Jersey, 07458. All rights reserved. Printed in the United States of America. This publication is protected
by Copyright, and permission should be obtained from the publisher prior to any prohibited reproduction, storage in
a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording,
or likewise. To obtain permission(s) to use material from this work, please submit a written request to Pearson
Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax
your request to 201-236-3290.
Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks.
Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations
have been printed in initial caps or all caps.
10 9 8 7 6 5 4 3 2 1
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:
■ 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.
■ 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.
■ Covers introductions to objects and strings earlier in Chapter 4 to enable students to use
objects and strings to develop interesting programs early.
■ 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.pearsonhighered.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 fundamentals-first
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 solv- problem-driven
ing rather than syntax. We make introductory programming interesting by using thought-
provoking 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 seamlessly integrates programming, data structures, and algorithms into one text. data structures
It employs a practical approach to teach data structures. We first introduce how to use various
data structures to develop efficient algorithms, and then show how to implement these data
structures. Through implementation, students gain a deep understanding on the efficiency of
data structures and on how and when to use certain data structures. Finally we design and
implement custom data structures for trees and graphs.
The book is widely used in the introductory programming, data structures, and algorithms
courses in the universities around the world. This comprehensive version covers fundamentals comprehensive version
of programming, object-oriented programming, GUI programming, data structures, algorithms,
concurrency, networking, database, and Web programming. It is designed to prepare students
to become proficient Java programmers. A brief version (Introduction to Java Programming, brief version
Brief Version, Tenth Edition) is available for a first course on programming, commonly known AP Computer Science
as CS1. The brief version contains the first 18 chapters of the comprehensive version. The first
13 chapters 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- examples and exercises
ming is by doing. Basic concepts are explained by example and a large number of exercises
iii
iv Preface
with various levels of difficulty are provided for students to practice. For our programming
courses, we assign programming exercises after each lecture.
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.pearsonhighered.com/liang
Pedagogical Features
The book uses the following elements to help students get the most from the material:
■ 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 completing
the chapter.
■ The Introduction opens the discussion with representative problems to give the reader an
overview of what to expect from the chapter.
■ Key Points highlight the important concepts covered in each section.
■ Check Points provide review questions to help students track their progress as they read
through the chapter and evaluate their learning.
■ 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.
■ The Chapter Summary reviews the important subjects that students should under-
stand and remember. It helps them reinforce the key concepts they have learned in the
chapter.
■ Quizzes are accessible online, grouped by sections, for students to do self-test on pro-
gramming concepts and techniques.
■ 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
programming 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.
■ 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.
Part III: GUI Programming (Chapters 14–16 and Bonus Chapter 34)
JavaFX is a new framework for developing Java GUI programs. It is not only useful for
developing GUI programs, but also an excellent pedagogical tool for learning object-oriented
programming. This part introduces Java GUI programming using JavaFX in Chapters 14–16.
Major topics include GUI basics (Chapter 14), container panes (Chapter 14), drawing shapes
(Chapter 14), event-driven programming (Chapter 15), animations (Chapter 15), and GUI
controls (Chapter 16), and playing audio and video (Chapter 16). You will learn the architec-
ture of JavaFX GUI programming and use the controls, shapes, panes, image, and video to
develop useful applications. Chapter 34 covers advanced features in JavaFX.
Part IV: Data Structures and Algorithms (Chapters 18–29 and Bonus Chapters 40–41)
This part covers the main subjects in a typical data structures and algorithms course. Chapter 18
introduces recursion to write methods for solving inherently recursive problems. Chapter 19
presents how generics can improve software reliability. Chapters 20 and 21 introduce the
Java Collection Framework, which defines a set of useful API for data structures. Chapter 22
discusses measuring algorithm efficiency in order to choose an appropriate algorithm for
applications. Chapter 23 describes classic sorting algorithms. You will learn how to implement
several classic data structures lists, queues, and priority queues in Chapter 24. Chapters 25 and
26 introduce binary search trees and AVL trees. Chapter 27 presents hashing and implement-
ing maps and sets using hashing. Chapters 28 and 29 introduce graph applications. The 2-4
trees, B-trees, and red-black trees are covered in Bonus Chapters 40–41.
Part V: Advanced Java Programming (Chapters 30–33 and Bonus Chapters 35–39, 42)
This part of the book is devoted to advanced Java programming. Chapter 30 treats the use
of multithreading to make programs more responsive and interactive and introduces parallel
programming. Chapter 31 discusses how to write programs that talk with each other from
different hosts over the Internet. Chapter 32 introduces the use of Java to develop database
viii Preface
projects. Chapter 33 introduces modern Web application development using JavaServer Faces.
Chapter 35 delves into advanced Java database programming. Chapter 36 covers the use of
internationalization support to develop projects for international audiences. Chapters 37 and
38 introduce how to use Java servlets and JavaServer Pages to generate dynamic content from
Web servers. Chapter 39 discusses Web services. Chapter 42 introduces testing Java programs
using JUnit.
Appendixes
This part of the book covers a mixed bag of topics. Appendix A lists Java keywords.
Appendix B gives tables of ASCII characters and their associated codes in decimal and in
hex. Appendix C shows the operator precedence. Appendix D summarizes Java modifiers and
their usage. Appendix E discusses special floating-point values. Appendix F introduces num-
ber systems and conversions among binary, decimal, and hex numbers. Finally, Appendix G
introduces bitwise operations. Appendix H introduces regular expressions. Appendix I covers
enumerated types.
VideoNotes
We are excited about the new VideoNotes feature that is found in this new edition. These VideoNote
videos provide additional help by presenting examples of key topics and showing how to
solve problems completely, from design through coding. VideoNotes are available from
www.pearsonhighered.com/liang.
Visit https://testbankfan.com
now to explore a rich
collection of testbank or
solution manual and enjoy
exciting offers!
x Preface
Algorithm Animations
Animation We have provided numerous animations for algorithms. These are valuable pedagogical tools
to demonstrate how algorithms work. Algorithm animations can be accessed from the Com-
panion Website.
Acknowledgments
I would like to thank Armstrong Atlantic State University for enabling me to teach what I
write and for supporting me in writing what I teach. Teaching is the source of inspiration for
continuing to improve the book. I am grateful to the instructors and students who have offered
comments, suggestions, bug reports, and praise.
This book has been greatly enhanced thanks to outstanding reviews for this and previous
editions. The reviewers are: Elizabeth Adams (James Madison University), Syed Ahmed (North
Georgia College and State University), Omar Aldawud (Illinois Institute of Technology), Stefan
Andrei (Lamar University), Yang Ang (University of Wollongong, Australia), Kevin Bierre
(Rochester Institute of Technology), David Champion (DeVry Institute), James Chegwidden
(Tarrant County College), Anup Dargar (University of North Dakota), Charles Dierbach (Towson
University), Frank Ducrest (University of Louisiana at Lafayette), Erica Eddy (University of
Wisconsin at Parkside), Deena Engel (NewYork University), Henry A. Etlinger (Rochester Institute
of Technology), James Ten Eyck (Marist College), Myers Foreman (Lamar University), Olac
Fuentes (University of Texas at El Paso), Edward F. Gehringer (North Carolina State University),
Harold Grossman (Clemson University), Barbara Guillot (Louisiana State University), Stuart
Hansen (University of Wisconsin, Parkside), Dan Harvey (Southern Oregon University), Ron
Hofman (Red River College, Canada), Stephen Hughes (Roanoke College), Vladan Jovanovic
(Georgia Southern University), Edwin Kay (Lehigh University), Larry King (University of
Texas at Dallas), Nana Kofi (Langara College, Canada), George Koutsogiannakis (Illinois
Institute of Technology), Roger Kraft (Purdue University at Calumet), Norman Krumpe (Miami
University), Hong Lin (DeVry Institute), Dan Lipsa (Armstrong Atlantic State University),
James Madison (Rensselaer Polytechnic Institute), Frank Malinowski (Darton College),
Tim Margush (University of Akron), Debbie Masada (Sun Microsystems), Blayne Mayfield
(Oklahoma State University), John McGrath (J.P. McGrath Consulting), Hugh McGuire (Grand
Valley State), Shyamal Mitra (University of Texas at Austin), Michel Mitri (James Madison
University), Kenrick Mock (University of Alaska Anchorage), Frank Murgolo (California
State University, Long Beach), Jun Ni (University of Iowa), Benjamin Nystuen (University of
Colorado at Colorado Springs), Maureen Opkins (CA State University, Long Beach), Gavin
Osborne (University of Saskatchewan), Kevin Parker (Idaho State University), Dale Parson
(Kutztown University), Mark Pendergast (Florida Gulf Coast University), Richard Povinelli
(Marquette University), Roger Priebe (University of Texas at Austin), Mary Ann Pumphrey (De
Anza Junior College), Pat Roth (Southern Polytechnic State University), Amr Sabry (Indiana
University), Ben Setzer (Kennesaw State University), Carolyn Schauble (Colorado State
University), David Scuse (University of Manitoba), Ashraf Shirani (San Jose State University),
Daniel Spiegel (Kutztown University), Joslyn A. Smith (Florida Atlantic University), Lixin
Tao (Pace University), Ronald F. Taylor (Wright State University), Russ Tront (Simon Fraser
University), Deborah Trytten (University of Oklahoma), Michael Verdicchio (Citadel), Kent
Vidrine (George Washington University), and Bahram Zartoshty (California State University
at Northridge).
It is a great pleasure, honor, and privilege to work with Pearson. I would like to thank Tracy
Johnson and her colleagues Marcia Horton, Yez Alayan, Carole Snyder, Scott Disanno, Bob
Engelhardt, Haseen Khan, and their colleagues for organizing, producing, and promoting this
project.
As always, I am indebted to my wife, Samantha, for her love, support, and encouragement.
BRIEF CONTENTS
1 Introduction to Computers, Programs, 29 Weighted Graphs and Applications 1061
and Java 1 30 Multithreading and Parallel Programming 1097
2 Elementary Programming 33 31 Networking 1139
3 Selections 75 32 Java Database Programming 1173
4 Mathematical Functions, Characters, 33 JavaServer Faces 1213
and Strings 119
5 Loops 157
6 Methods 203
7 Single-Dimensional Arrays 245 Chapters 34–42 are bonus Web chapters
8 Multidimensional Arrays 287 34 Advanced JavaFX 34-1
9 Objects and Classes 321 35 Advanced Database Programming 35-1
10 Object-Oriented Thinking 365 36 Internationalization 36-1
11 Inheritance and Polymorphism 409 37 Servlets 37-1
12 Exception Handling and Text I/O 449 38 JavaServer Pages 38-1
13 Abstract Classes and Interfaces 495 39 Web Services 39-1
14 JavaFX Basics 535 40 2-4 Trees and B-Trees 40-1
15 Event-Driven Programming 41 Red-Black Trees 41-1
and Animations 585 42 Testing Using JUnit 42-1
16 JavaFX UI Controls and Multimedia 629
17 Binary I/O 677
18 Recursion 705 Appendixes
19 Generics 737 A Java Keywords 1263
20 Lists, Stacks, Queues, B The ASCII Character Set 1266
and Priority Queues 761
C Operator Precedence Chart 1268
21 Sets and Maps 797
D Java Modifiers 1270
22 Developing Efficient Algorithms 821
E Special Floating-Point Values 1272
23 Sorting 861
F Number Systems 1273
24 Implementing Lists, Stacks, Queues,
and Priority Queues 895 G Bitwise Operatoirns 1277
25 Binary Search Trees 929 H Regular Expressions 1278
26 AVL Trees 965 I Enumerated Types 1283
27 Hashing 985
28 Graphs and Applications 1015 Index 1289
xi
CONTENTS
Chapter 1 Introduction to Computers, Programs,
and Java 1
1.1 Introduction 2
1.2 What Is a Computer? 2
1.3 Programming Languages 7
1.4 Operating Systems 9
1.5 Java, the World Wide Web, and Beyond 10
1.6 The Java Language Specification, API, JDK, and IDE 11
1.7 A Simple Java Program 12
1.8 Creating, Compiling, and Executing a Java Program 15
1.9 Programming Style and Documentation 18
1.10 Programming Errors 20
1.11 Developing Java Programs Using NetBeans 23
1.12 Developing Java Programs Using Eclipse 25
Chapter 3 Selections 75
3.1 Introduction 76
3.2 boolean Data Type 76
3.3 if Statements 78
3.4 Two-Way if-else Statements 80
3.5 Nested if and Multi-Way if-else Statements 81
3.6 Common Errors and Pitfalls 83
3.7 Generating Random Numbers 87
3.8 Case Study: Computing Body Mass Index 89
3.9 Case Study: Computing Taxes 90
3.10 Logical Operators 93
3.11 Case Study: Determining Leap Year 97
3.12 Case Study: Lottery 98
3.13 switch Statements 100
3.14 Conditional Expressions 103
xii
xiii
3.15 Operator Precedence and Associativity 104
3.16 Debugging 106
Language: English
A BRITISH ARMY,
AS IT WAS,—IS,—AND OUGHT TO BE:
ILLUSTRATED BY
WITH OBSERVATIONS
UPON
1840.
TO
Sir,
THE AUTHOR.