Fundamentals of Computer Science using Java [Hughes 2002-01-11].pdf

(2797 KB) Pobierz
435843101 UNPDF
Fundamentals of
Computer Science
Using Java
David Hughes
Brock University
435843101.004.png 435843101.005.png 435843101.006.png
World Headquarters
Jones and Bartlett Publishers
40 Tall Pine Drive
Sudbury, MA 01776
978-443-5000
info@jbpub.com
www.jbpub.com
Jones and Bartlett Publishers
Canada
2406 Nikanna Road
Mississauga, ON L5C 2W6
CANADA
Jones and Bartlett Publishers
International
Barb House, Barb Mews
London W6 7PA
UK
Copyright © 2002 by Jones and Bartlett Publishers, Inc.
Library of Congress Cataloging-in-Publication Data
Hughes, David (David John Frederick), 1952-
Fundamentals of computer science using Java / David Hughes.
p. cm.
ISBN 0-7637-1761-4
1. Computer science. 2. Java (Computer program language) I. Title.
QA76.H789 2001
005.2'76—dc21
2001029710
8888
All rights reserved. No part of the material protected by this copyright notice may be reproduced or utilized in any
form, electronic or mechanical, including photocopying, recording, or any information storage or retrieval system,
without written permission from the copyright owner.
Chief Executive Officer: Clayton Jones
Chief Operating Officer: Don W. Jones, Jr.
Executive V.P. and Publisher: Robert W. Holland, Jr.
V.P., Design and Production: Anne Spencer
V.P., Manufacturing and Inventory Control: Therese Bräuer
Editor-in-Chief: J. Michael Stranz
Production Manager: Amy Rose
Marketing Manager: Nathan Schultz
Associate Production Editor: Tara McCormick
Editorial Assistant: Theresa DiDonato
Cover Design: Kristin Ohlin
Composition: Northeast Compositors, Inc.
Text Design: Mary McKeon
Printing and Binding: Courier Westford
Cover Printing: John Pow Company, Inc.
This book was typeset in Quark 4.1 on a Macintosh G4. The font families used were Adobe Garamond, Univers, and
Prestige Elite. The first printing was printed on 50# Courier Opaque.
Printed in the United States of America
06 05 04 03 02 10 9 8 7 6 5 4 3 2 1
435843101.007.png
Preface
Why this Book
In the summer of 1996, our Computer Science department made the decision to use
Java as the core language for our Computer Science program, beginning that fall.
Although there were many Java books available, we soon discovered that most were
“trade” or “hobby” books, not designed for university courses and certainly not intended
to serve as introductions to Computer Science. It became clear to us that someone
needed to write a “Fundamentals of Computer Science Using Java” book, and I thought,
“why not me?” And now, after years of researching, testing, and writing, I can provide
the book that we searched for years ago: a truly Java-based introduction to Computer
Science.
In a first course in Computer Science, the primary goal is to teach the fundamentals
of the field. Basic concepts are introduced with the help of a programming language
that is often viewed as simply a medium through which algorithms are expressed. From
that perspective, it does not matter which language is used in an introductory course,
because any would suffice. In practice, however, the language can have a profound
impact on the students’ learning experience. First, the style of the language constrains
the way and the order in which topics can be introduced. Further, the language taught
in the first course must support the rest of the curriculum. For these reasons and more,
a language-defined text is an important component in an introductory course.
Object-oriented languages in particular are useful in introductory textbooks and are
certainly appropriate at this time. Having an object-oriented language as the core pro-
gramming language supports many courses at the higher level (e.g., software engineering,
user interfaces, databases). The question is, then, which object-oriented language?
v
435843101.001.png
PREFACE
Our decision to use Java was based on a number of factors. First, we recognized Java
as a pure object-oriented language, as opposed to C++, which is a hybrid, and thus does
not allow the programmer to fall back into procedural habits. Further, it has a relatively
clear and common syntax that can be understood without having to learn a large class
hierarchy. Finally, Java has compilers available on a great many platforms that are inex-
pensive, not overly resource hungry, and the code is platform-independent. All of these
things make Java ideal for a first university course.
The approach taken in this book is what might best be called an “object-based”
approach. It is my belief that students need to master the skill of method writing before
they can craft meaningful classes. Objects occur right from the start. The student’s code,
however, is written as a client of another class, and thereby makes use of objects through
the delegation model rather than the inheritance model.
The text introduces methods as early as possible and then introduces the control
structures and types necessary for writing methods. When classes are fully introduced,
the students are completely capable of writing the methods for a class and are familiar
with writing one class as a client of another. They soon master writing a class as a sup-
plier. Once classes are available, the text introduces object-oriented software develop-
ment using classes for the decomposition. Responsibility-based design is also introduced
using CRC cards as the design methodology.
The pedagogical approach applied to this text is grounded in the idea that the learn-
ing process can be facilitated through the use of examples. Each new topic is introduced
through a number of complete program examples. Examples are kept as simple as possi-
ble to illustrate important concepts. At the same time, the examples are realistic, and
allow for meaningful use of new constructs. Students can often use the examples as a
starting point for coding of assignment problems.
What is Covered and What is Not
Java, like any programming language, is fairly large and this book does not attempt to
provide complete coverage of all Java topics. As an object-oriented language, Java has
many standard class libraries and many other APIs, and therefore it would not be possi-
ble to provide complete coverage of the language even if I so wished.
The first decision I made was to exclude inheritance. This might seem like heresy,
however, I stand by this decision and believe it is appropriate to exclude inheritance
from an introductory course. In my experience, students have trouble understanding
the true meaning of inheritance, and this often leads them to use inheritance as simply a
mechanism for code borrowing. This is very evident in the structure of many books that
introduce Computer Science in an object-oriented language. In an attempt to make the
first programs interesting, these texts can overuse subclassing. Code reuse through dele-
gation is a much simpler, and often more desirable, approach. In a first course, I prefer
to foster in my students a clear understanding of the basic principles, and I leave inheri-
vi
435843101.002.png
PREFACE
tance and subclassing for a later course. In our program, inheritance and polymorphism
are introduced in the second year.
One possible objection to excluding inheritance is that without it we cannot write
applets. This is a small loss, as it would be nice if the student’s programs could be
demonstrated using a web browser. The level of programming necessary for writing
applets, however, is really too advanced for an introductory course, since it requires the
use of graphical user interfaces to do anything reasonable. To allow interesting first pro-
grams, the class library TurtleGraphics is used. This class library supports the turtle
graphics model introduced in the programming language Logo.
The AWT and Swing are also not covered in this book. GUI programming requires
an event model for programming that allows apparent non-linear flow of control. This is
confusing for first-year students. Instead, the I/O class library BasicIO is used. This I/O
class library provides a class for prompted input via the dialog box ASCIIPrompter and
provides output to the scrollable window ASCIIDisplayer.
Even though inheritance is not covered, classes definitely are. Classes are the funda-
mental decomposition mechanism in object-oriented design. Of course, without inheri-
tance the design model is incomplete; however, designing with inheritance is difficult
and better learned when a student’s programming skills are more mature.
Exceptions are also a difficult concept for beginning students to grasp because they
introduce a second path of execution. Since Java requires that any exception (other than
RunTimeException) be caught or thrown by the method, code dealing with exceptions
obscures the expression of the algorithm. The most common occurrence of exceptions is
in I/O. To remove the need to deal with exceptions too early, the BasicIO library does
not throw exceptions.
Use of the Book
At Brock, the material presented here forms the substance of a half-year (twelve-week)
course meeting three hours per week. The lectures are supplemented by a one-hour tuto-
rial, which is primarily a question and answer period, and a two-hour laboratory where the
students work on programming assignments. The primary goal of our course is to intro-
duce basic computer science concepts, while introducing language concepts as needed.
Chapter 1 includes a brief history of computing and computing technology, and then
describes the basic hardware and software organization of computer systems. The mate-
rial in Appendix A may be used to supplement this coverage, or can be introduced at a
later time for a clearer understanding of the low-level execution of programs. Chapter 1
also provides a preview to the software development process, and the phases included in
this chapter are repeated in the Case Studies of later chapters.
Chapter 2 begins the coverage of Java. It introduces the Java syntax notation so that
students will be able to read the syntax descriptions that define the language. Turtle
Graphics are used to enhance the early examples. Programs are written as clients of
vii
435843101.003.png
Zgłoś jeśli naruszono regulamin