Download Full A comprehensive introduction to object oriented programming with Java 1st Edition C. Thomas Wu PDF All Chapters
Download Full A comprehensive introduction to object oriented programming with Java 1st Edition C. Thomas Wu PDF All Chapters
com
https://ebookname.com/product/a-comprehensive-introduction-
to-object-oriented-programming-with-java-1st-edition-c-
thomas-wu/
OR CLICK BUTTON
DOWLOAD EBOOK
https://ebookname.com/product/object-oriented-programming-with-c-4th-
edition-e-balagurusamy/
ebookname.com
https://ebookname.com/product/microsoft-visual-c-2010-an-introduction-
to-object-oriented-programming-4th-edition-joyce-farrell/
ebookname.com
https://ebookname.com/product/introduction-to-object-oriented-
programming-an-3rd-edition-timothy-budd/
ebookname.com
https://ebookname.com/product/british-economic-development-in-south-
east-asia-1880-1939-volume-1-agriculture-1st-edition-david-sunderland-
editor/
ebookname.com
Environmental Bioengineering Volume 11 1st Edition
Svetlana Yu. Selivanovskaya Ph.D.
https://ebookname.com/product/environmental-bioengineering-
volume-11-1st-edition-svetlana-yu-selivanovskaya-ph-d/
ebookname.com
https://ebookname.com/product/the-seeders-the-return-of-the-gods-1st-
edition-elena-danaan/
ebookname.com
https://ebookname.com/product/teaching-and-learning-in-further-
education-diversity-and-change-3rd-edition-prue-huddleston/
ebookname.com
https://ebookname.com/product/africa-and-the-international-criminal-
court-1st-edition-gerhard-werle/
ebookname.com
https://ebookname.com/product/politics-of-urban-runoff-nature-
technology-and-the-sustainable-city-1st-edition-andrew-karvonen/
ebookname.com
Advanced Characterization Techniques for Thin Film Solar
Cells 1st Edition Uwe Rau
https://ebookname.com/product/advanced-characterization-techniques-
for-thin-film-solar-cells-1st-edition-uwe-rau/
ebookname.com
wu23399_fm.qxd 1/10/07 11:53 Page i
A Comprehensive Introduction
to Object-Oriented Programming
TM
with Java
C.Thomas Wu
Naval Postgraduate School
wu23399_fm.qxd 1/10/07 11:53 Page ii
Published by McGraw-Hill, a business unit of The McGraw-Hill Companies, Inc., 1221 Avenue of the Americas,
New York, NY 10020. Copyright © 2008 by The McGraw-Hill Companies, Inc. All rights reserved. No part of this
publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system,
without the prior written consent of The McGraw-Hill Companies, Inc., including, but not limited to, in any network
or other electronic storage or transmission, or broadcast for distance learning.
Some ancillaries, including electronic and print components, may not be available to customers outside
the United States.
1 2 3 4 5 6 7 8 9 0 DOC/DOC 0 9 8 7
ISBN 978–0–07–352339–2
MHID 0–07–352339–9
Wu, C. Thomas.
A comprehensive introduction to object-oriented programming with Java / C. Thomas
Wu. – 1st ed.
p. cm.
ISBN 978–0–07–352339–2 — ISBN 0–07–352339–9
1. Object-oriented programming (Computer science) 2. Java (Computer program
language) I. Title.
QA76.64.W77 2008
005.117–dc22
2006048064
www.mhhe.com
wu23399_fm.qxd 1/10/07 11:53 Page iii
To my family
wu23399_fm.qxd 1/10/07 11:53 Page iv
wu23399_fm.qxd 1/10/07 11:53 Page v
Contents
Preface xiii
Key Differences from the Standard Edition xiii
Book Organization xiv
Hallmark Features of the Text xviii
v
wu23399_fm.qxd 1/10/07 11:53 Page vi
vi Contents
3 Numerical Data 81
3.1 Variables 82
3.2 Arithmetic Expressions 90
3.3 Constants 95
3.4 Displaying Numerical Values 97
3.5 Getting Numerical Input 103
3.6 The Math Class 109
3.7 Random Number Generation 113
3.8 The GregorianCalendar Class 115
3.9 Sample Development 120
3.10 Numerical Representation (Optional) 131
Contents vii
viii Contents
Contents ix
x Contents
Contents xi
xii Contents
Appendix A 1099
Appendix B 1107
Appendix C 1133
Appendix D 1155
Index 1163
wu23399_fm.qxd 1/10/07 11:53 Page xiii
Preface
xiv Preface
Book Organization
There are 21 chapters in this book, numbered from 0 to 20. The first 11 chapters
cover the core topics that provide the fundamentals of programming. Chapters 11 to
15 cover intermediate-level topics such as sorting, searching, recursion, inheritance,
polymorphism, and file I/O. And Chapters 16 to 20 cover topics related to data
structures. There are more than enough topics for one semester. After the first
11 chapters (Ch 0 to Ch 10), instructors can mix and match materials from Chapters 11
to 20 to suit their needs. We first show the dependency relationships among the
chapters and then provide a brief summary of each chapter.
Chapter Dependency
For the most part, chapters should be read in sequence, but some variations are
possible, especially with the optional chapters. Here’s a simplified dependency
graph:
0
1
4
5
6
7
8 9 10
*Note: Some examples use arrays,
11 12 13 14* 15 16 but the use of arrays is not an
integral part of the examples.
These examples can be modified
to those that do not use arrays.
17 18 Many topics from the early part
of the chapter can be introduced
19 20 as early as after Chapter 2.
wu23399_fm.qxd 1/10/07 11:54 Page xv
Preface xv
xvi Preface
and the clever (Euclid’s) solutions. We introduce the Formatter class (new to
Java 5.0) and show how the output can be aligned nicely. The optional last sec-
tion of the chapter introduces recursion as another technique for repetition.
The recursive version of a method that finds the greatest common divisor of
two integers is given.
• Chapter 7 is the second part of creating programmer-defined classes. We
introduce new topics related to the creation of programmer-defined classes
and also repeat some of the topics covered in Chapter 4 in more depth. The
key topics covered in this chapter are method overloading, the reserved
word this, class methods and variables, returning an object from a method,
and pass-by-value parameter passing. As in Chapter 4, we provide many
lucid illustrations to make these topics accessible to beginners. We use the
Fraction class to illustrate many of these topics, such as the use of this and
class methods. The complete definition of the Fraction class is presented in
this chapter.
• Chapter 8 teaches exception handling and assertions. The focus of this chap-
ter is the construction of reliable programs. We provide a detailed coverage of
exception handling in this chapter. We introduce an assertion and show how it
can be used to improve the reliability of finished products by catching logical
errors early in the development.
• Chapter 9 covers nonnumerical data types: characters and strings. Both the
String and StringBuffer classes are explained in the chapter. Another string
class named StringBuilder (new to Java 5.) is briefly explained in this chapter.
An important application of string processing is pattern matching. We describe
pattern matching and regular expression in this chapter. We introduce the
Pattern and Matcher classes and show how they are used in pattern matching.
• Chapter 10 teaches arrays. We cover arrays of primitive data types and of ob-
jects. An array is a reference data type in Java, and we show how arrays are
passed to methods. We describe how to process two-dimensional arrays and
explain that a two-dimensional array is really an array of arrays in Java. Lists
and maps are introduced as a more general and flexible way to maintain a col-
lection of data. The use of ArrayList and HashMap classes from the java.util
package is shown in the sample programs. Also, we show how the WordList
helper class used in Chapter 9 sample development program is implemented
with another map class called TreeMap.
• Chapter 11 presents searching and sorting algorithms. Both N2 and Nlog2N
sorting algorithms are covered. The mathematical analysis of searching and
sorting algorithms can be omitted depending on the students’ background.
• Chapter 12 explains the file I/O. Standard classes such as File and JFile-
Chooser are explained. We cover all types of file I/O, from a low-level byte
I/O to a high-level object I/O. We show how the file I/O techniques are used
to implement the helper classes—Dorm and FileManager—in Chapter 8 and 9
sample development programs. The use of the Scanner class for inputting data
from a textfile is also illustrated in this chapter.
wu23399_fm.qxd 1/10/07 11:54 Page xvii
Preface xvii
• Chapter 13 discusses inheritance and polymorphism and how to use them ef-
fectively in program design. The effect of inheritance for member accessibil-
ity and constructors is explained. We also explain the purpose of abstract
classes and abstract methods.
• Chapter 14 covers GUI and event-driven programming. Only the Swing-
based GUI components are covered in this chapter. We show how to use the
JOptionPane class for a very simple GUI-based input and output. GUI com-
ponents introduced in this chapter include JButton, JLabel, ImageIcon,
JTextField, JTextArea, and menu-related classes. We describe the effective use
of nested panels and layout managers. Handling of mouse events is described
and illustrated in the sample programs. Those who do not teach GUI can skip
this chapter altogether. Those who teach GUI can introduce the beginning part
of the chapter as early as after Chapter 2.
• Chapter 15 covers recursion. Because we want to show the examples where
the use of recursion really shines, we did not include any recursive algorithm
(other than those used for explanation purposes) that really should be written
nonrecursively.
• Chapter 16 covers contiguous and noncontiguous memory allocation schemes
and introduces the concept of linked lists. Ample examples are provided to
illustrate the manipulation of linked lists of primitive data types and linked
lists of objects. This chapter lays the necessary foundation for the students to
learn different techniques for implementing the abstract data types covered in
Chapters 18 through 20.
• Chapter 17 covers new Java 5.0 generics in detail. The chapter describes how
generic classes are defined and how the type safety is supported by generics.
A concrete example of using generics is shown by defining a simple linked list
with generic nodes.
• Chapter 18 introduces the concept of abstract data types (ADT) and covers
the List ADT. Key features of the List ADT are explained and two implemen-
tations using an array and a linked list are shown. The iterator pattern to tra-
verse the elements in the List ADT is introduced.
• Chapter 19 covers the Stack ADT. Key features of the Stack ADT are ex-
plained and two implementations using an array and a linked list are shown.
Sample applications that use stacks are described.
• Chapter 20 covers the Queue ADT. Key features of the Stack ADT are ex-
plained and two implementations using an array and a linked list are shown.
A special type of queue called a priority queue is also intoduced in this
chapter.
wu23399_fm.qxd 1/10/07 11:54 Page xviii
xviii Preface
Problem Solving
Problem Statement
We start our development with a problem statement. The problem statement for our
sample programs will be short, ranging from a sentence to a paragraph, but the problem
statement for complex and advanced applications may contain many pages. Here’s the
problem statement for this sample development exercise:
Write an application that asks for the user’s first, middle, and last names and
replies with the user’s initials.
Overall Plan
Our first task is to map out the overall plan for development. We will identify classes nec-
essary for the program and the steps we will follow to implement the program.We begin
with the outline of program logic. For a simple program such as this one, it is kind of obvi-
ous; but to practice the incremental development, let’s put down the outline of program
flow explicitly. We can express the program flow as having three tasks:
Preface xix
Object-Oriented Approach
We take the object-first approach to teaching object-oriented programming with emphasis
on proper object-oriented design.The concept of objects is clearly illustrated from the very
first sample program.
/*
File: Ch2Sample1.java
*/
import javax.swing.*;
class Ch2Sample1 {
JFrame myWindow;
myWindow.setSize(300, 200);
myWindow.setTitle("My First Java Program");
myWindow.setVisible(true);
}
}
Figure 8.8 Program diagrams for the user and administrative modules. Notice the same Dorm and
Resident classes are used in both programs. User and administrative modules will include one or more
classes (at least one is programmer-defined).
wu23399_fm.qxd 1/10/07 11:55 Page xx
xx Preface
Illustrative Diagrams
Illustrative diagrams are used to explain all key concepts of programming such as the
difference between object declaration and creation, the distinction between the primitive
data type and the reference data type, the call-by-value parameter passing, inheritance, and
many others.
number1 alan
number2 turing
number2 turing
:Professor
:Professor
add(1, “gnu”)
myList
0 1 2 3 4
After “cat” “gnu” “ape” “dog” “bee”
myList
0 1 2 3
Before “cat” “ape” “dog” “bee”
throws
add(5, “gnu”) <index-out-of-bounds-exception>
myList
0 1 2 3
“cat” “ape” “dog” “bee” No structural
After
change to the list
Preface xxi
Student Pedagogy
Design Guidelines
provide tips on good
Always define a constructor and initialize data members fully in the
constructor so an object will be created in a valid state. program design.
Things to Remember
boxes provide tips for List the catch blocks in the order of specialized to more general exception classes.
students to remember key At most one catch block is executed, and all other catch blocks are ignored.
concepts.
We didn’t write
because it is not necessary. The simplify method returns a Fraction object, and in
the calling program, all we need is a name we can use to refer to this returned
Fraction object. Don’t forget that the object name (variable) and the actual object
instance are two separate things.
Quick Check
1. What will be displayed on the console window when the following code is exercises at the end of
executed and the user enters abc123 and 14? the sections allow
Scanner scanner = new Scanner(System.in);
students to test their
try { comprehension of
int num1 = scanner.nextInt(); topics.
System.out.println("Input 1 accepted");
int num2 = scanner.nextInt();
System.out.println("Input 2 accepted");
} catch (InputMismatchException e) {
System.out.println("Invalid Entry");
}
wu23399_fm.qxd 1/10/07 11:55 Page xxii
xxii Preface
For Instructors
• Complete set of PowerPoints, including lecture notes and figures.
Preface xxiii
For Students
• Compiler How Tos provide tutorials on how to get up and running on the
most popular compilers to aid students in using IDEs.
• Interactive Quizzes allow students to test what they learn and get immediate
feedback.
• Source code for all example programs in the book.
• Answers to quick check exercises.
• Glossary of key terms.
• Recent News links relevant to computer science.
• Additional Topics such as more on swing and an introduction to data structures.
Acknowledgments
First, I would like to thank the following reviewers for their comments, suggestions,
and encouragement.
xxiv Preface
Reviewers
Ajith, Abraham, Oklahoma State University
Elizabeth Adams, James Madison University
David L. Atkins, University of Oregon
GianMario Besana, DePaul University
Robert P. Burton, Brigham Young University
Michael Buckley, State University of New York, Buffalo
Rama Chakrapani, Tennessee Technological University
Teresa Cole, Boise State University
James Cross, Auburn University
Priscilla Dodds, Georgia Perimeter College
Kossi Delali Edoh, Montclair State University
Christopher Eliot, University of Massachusetts-Amherst
Michael Floeser, Rochester Institute of Technology
Joanne Houlahan, John Hopkins University
Michael N. Huhns, University of South Carolina
Eliot Jacobson, University of California, Santa Barbara
Martin Kendall, Montgomery Community College
Mike Litman, Western Illinois University
Len Myers, California Polytechnic State University, San Luis Obispo
Jun Ni, University of Iowa
Robert Noonan, College of William and Mary
Jason S. O’Neal, Mississippi College
Hal Perkins, University of Washington
Gerald Ross, Lane Community College
William Shea, Kansas State University
Jason John Schwarz, North Carolina State University
Marge Skubic, University of Missouri, Columbia
Bill Sverdlik, Eastern Michigan University
Peter Stanchev, Kettering University
Krishnaprasad Thirunarayan, Wright State University
David Vineyard, Kettering University
Suzanne Westbrook, University of Arizona
Melissa Wiggins, Mississippi College
Zhiguang Xu, Valdosta State University.
The following reviewers have provided feedback on the chapters new to this
comprehensive edition:
Preface xxv
Personal Story
In September, 2001, I changed my name for personal reasons. Prof C. Thomas
Wu is now Prof Thomas W. Otani. To maintain continuity and not to confuse peo-
ple, we continue to publish the book under my former name. For those who
care to find out a little about my personal history can do so by visiting my web-
site (www.drcaffeine.com).
Other documents randomly have
different content
– Hallgasson meg. Pár éve kinoz.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com