An Introduction to Object Oriented Programming With Java 5th Edition by Thomas Wu 0073523305 9780073523309 download
An Introduction to Object Oriented Programming With Java 5th Edition by Thomas Wu 0073523305 9780073523309 download
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-
wu-0073523305-9780073523309-17244/
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-
wu-0073523305-9780073523309-15256/
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-
wu-0073523305-9780073523309-15330/
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-wu-
isbn-9780073523309-0073523305-15552/
https://ebookball.com/product/an-introduction-to-object-oriented-
programming-with-java-5th-edition-by-thomas-wu-
isbn-0073523305-9780073523309-16398/
Object Oriented Programming with Java 1st edition By Hanumanth Ladwa
ISBN 8450193403 9788450193403
https://ebookball.com/product/object-oriented-programming-with-
java-1st-edition-by-hanumanth-ladwa-
isbn-8450193403-9788450193403-25034/
https://ebookball.com/product/microsoft-visual-c-2017-an-
introduction-to-object-oriented-programming-7th-edition-by-joyce-
farrell-isbn-1337102100-9781337102100-25038/
https://ebookball.com/product/beginning-c-3-0-an-introduction-to-
object-oriented-programming-1st-edition-by-jack-
purdum-0470261293-978-0470261293-15238/
https://ebookball.com/product/object-oriented-programming-using-
c-an-introduction-1st-edition-by-pardoe-
king-0333692411-978-0333692417-15282/
Object Oriented Programming and Java 1st Edition by Danny Poo, Derek
Kiong 9813083964 9789813083967
https://ebookball.com/product/object-oriented-programming-and-
java-1st-edition-by-danny-poo-derek-
kiong-9813083964-9789813083967-15306/
www.allitebooks.com
An Introduction to Object-Oriented
TM
Programming with Java
Fifth Edition
C.Thomas Wu
Naval Postgraduate School
www.allitebooks.com
AN INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING WITH JAVA™, FIFTH EDITION
Published by McGraw-Hill, a business unit of The McGraw-Hill Companies, Inc., 1221 Avenue of the
Americas, New York, NY 10020. Copyright © 2010 by The McGraw-Hill Companies, Inc. All rights reserved.
Previous editions © 2006, 2004, and 2001. 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
ISBN 978–0–07–352330–9
MHID 0–07–352330–5
All credits appearing on page or at the end of the book are considered to be an extension of the copyright page.
Wu, C. Thomas.
An introduction to object-oriented programming with Java / C. Thomas Wu (Otani).—5th ed.
p. cm.
Includes index.
ISBN 978–0–07–352330–9— ISBN 0–07–352330–5 (hard copy : alk. paper) 1. Object-oriented
programming (Computer science) 2. Java (Computer program language) I. Title.
QA76.64.W78 2010
005.1'17—dc22
2008053612
www.mhhe.com
www.allitebooks.com
To my family
www.allitebooks.com
www.allitebooks.com
Contents
Preface xi
www.allitebooks.com
vi Contents
3 Numerical Data 85
3.1 Variables 86
3.2 Arithmetic Expressions 94
3.3 Constants 99
3.4 Displaying Numerical Values 101
3.5 Getting Numerical Input 107
3.6 The Math Class 113
3.7 Random Number Generation 117
3.8 The GregorianCalendar Class 120
3.9 Sample Development 125
3.10 Numerical Representation (Optional) 136
www.allitebooks.com
Contents vii
www.allitebooks.com
viii Contents
www.allitebooks.com
Contents ix
www.allitebooks.com
x Contents
Index 963
Preface
xi
xii Preface
most difficult and require significant effort. For some of the three-star exer-
cises, students must find or study additional information beyond those pre-
sented in the book. Please keep in mind that the level of difficulties is only a
general guideline. One student may find some level-three exercises much eas-
ier than level-two exercises, for example.
Book Organization
There are 16 chapters in this book, numbered from 0 to 15. 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. 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 15 to suit their needs. We first show the dependency relation-
ships 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 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.
Many topics from the early part
of the chapter can be introduced
as early as after Chapter 2.
Preface xiii
how the output can be aligned nicely. The optional last section 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 is briefly explained in this chapter. An important ap-
plication of string processing is pattern matching. We describe pattern match-
ing and regular expression in this chapter. We introduce the Pattern and
Matcher classes and show how they are used in pattern matching. One section
is added to discuss the application of string processing in bioinformatics.
• 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.
Preface xv
• 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.
xvi 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
obvious; but to practice the incremental development, let’s put down the outline of pro-
gram flow explicitly. We can express the program flow as having three tasks: Level-by-level Organization for
1. Get the user’s first, middle, and last names.
program
tasks
2. Extract the initials to formulate the monogram.
Programming Exercises
3. Output the monogram. Level 1 Programming Exercises ★
Having identified the three major tasks of the program, we will now identify the
classes we can use to implement the three tasks. First, we need an5.object In the RollDice program, we created three Die objects and rolled them once.
to handle
the input. At this point, we have learned about only the Scanner class, soRewrite it program so you will create only one Die object and roll it three
we will usethe
here. Second, we need an object to display the result. Again, we will use System.out,
times. as
it is the only one we know at this point for displaying a string value. For the string
6. Write a program that computes the total ticket sales of a concert. There are
three types of seatings: A, B, and C. The program accepts the number of
tickets sold and the price of a ticket for each of the three types of seats. The
total sales are computed as follows:
totalSales = numberOfA_Seats * pricePerA_Seat +
numberOfB_Seats * pricePerB_Seat +
numberOfC_Seats * pricePerC_Seat;
Write this program, using only one class, the main class of the program.
Development Exercises
7. Define a new class named Temperature. The class has two accessors—to-
For the following exercises, use the incremental development methodology to
Fahrenheit and toCelsius—that return the temperature in the specified unit
implement the program. For each exercise, identify the program tasks, create
a design document with class descriptions, and draw the program diagram. and two mutators—setFahrenheit and setCelsius—that assign the temperature
Map out the development steps at the start. Present any design alternatives and in the specified unit. Maintain the temperature internally in degrees Fahrenheit.
justify your selection. Be sure to perform adequate testing at the end of each Using this class, write a program that inputs temperature in degrees
development step. Fahrenheit and outputs the temperature in equivalent degrees Celsius.
11. In the sample development, we developed the user module of the keyless
entry system. For this exercise, implement the administrative module that
allows the system administrator to add and delete Resident objects and
modify information on existing Resident objects. The module will also allow
the user to open a list from a file and save the list to a file. Is it proper to
implement the administrative module by using one class? Wouldn’t it be
a better design if we used multiple classes with each class doing a single,
well-defined task?
12. Write an application that maintains the membership lists of five social clubs
in a dormitory. The five social clubs are the Computer Science Club, Biology
Club, Billiard Club, No Sleep Club, and Wine Tasting Club. Use the Dorm
Development Exercises
class to manage the membership lists. Members of the social clubs are give students an opportunity
Resident objects of the dorm. Use a separate file to store the membership
list for each club. Allow the user to add, delete, and modify members of to practice incremental
each club.
development.
Preface xvii
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.
/*
Chapter 2 Sample Program: Displaying a Window
File: Ch2Sample1.java
*/
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[] args) {
JFrame myWindow;
myWindow = new JFrame();
myWindow.setSize(300, 200);
myWindow.setTitle("My First Java Program");
myWindow.setVisible(true);
}
}
Good practices on
object-oriented design
are discussed
User module Dorm Resident
throughout the book
and illustrated through
numerous sample Door
programs.
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).
xviii 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
temp
0 1 2 3 4 5
temp
Note: The old array will eventually
get returned to the system via
garbage collection.
0 1 2 3 4 5
Figure 10.16 How a new array that is 150 percent of the original array is created. The size of the
original array is 4.
Preface xix
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");
}
www.allitebooks.com
xx Preface
Acknowledgments
I would like to thank my good friends at McGraw-Hill’s editorial and production
departments. Needless to say, without their help, this book would not have seen the
light of the day. I thank especially Raghu Srinivasan and Lorraine Buczek for their
infinite patience.
External reviewers are indispensable in maintaining the accuracy and improv-
ing the quality of presentation. Numerous professors have participated as reviewers
over the course of five editions, and I thank them all again for their comments, sug-
gestions, and encouragement. I especially thank the reviewers of the Comprehen-
sive edition for their valuable input towards the revision of this fifth edition text.
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
people, we continue to publish the book under my former name. For those who
care to find out a little about my personal history, they can do so by visiting
www.mhhe.com/wu
Introduction to
Computers and
Programming Languages
O b j e c t i v e s
1
2 Chapter 0 Introduction to Computers and Programming Languages
I n t r o d u c t i o n
One innovation of their machine was the use of binary numbers. (We discuss binary
numbers in Sec. 0.2.) At around the same time, Howard Aiken of Harvard University
was working on the Automatic Sequence-Controlled Calculator, known more com-
MARK I monly as MARK I, with support from IBM and the U.S. Navy. MARK I was very
similar to the Analytical Engine in design and was described as “Babbage’s dream
come true.”
MARK I was an electromechanical computer based on relays. Mechanical
relays were not fast enough, and MARK I was quickly replaced by machines based
ENIAC I on electronic vacuum tubes. The first completely electronic computer, ENIAC I
(Electronic Numerical Integrator And Calculator), was built at the University of
Pennsylvania under the supervision of John W. Mauchly and J. Presper Eckert.
Their work was influenced by the work of John Atanasoff.
ENIAC I was programmed laboriously by plugging wires into a control
panel that resembled an old telephone switchboard. Programming took an enor-
mous amount of the engineers’ time, and even making a simple change to a pro-
gram was a time-consuming effort. While programming activities were going on,
the expensive computer sat idle. To improve its productivity, John von Neumann
of Princeton University proposed storing programs in the computer’s memory.
stored program This stored program scheme not only improved computation speed but also al-
lowed far more flexible ways of writing programs. For example, because a pro-
gram is stored in the memory, the computer can change the program instructions
to alter the sequence of the execution, thereby making it possible to get different
results from a single program.
generations of We characterized these early computers with vacuum tubes as first-generation
computers computers. Second-generation computers, with transistors replacing the vacuum
tubes, started appearing in the late 1950s. Improvements in memory devices also
increased processing speed further. In the early 1960s, transistors were replaced by
integrated circuits, and third-generation computers emerged. A single integrated
circuit of this period incorporated hundreds of transistors and made the construction
of minicomputers possible. Minicomputers are small enough to be placed on desk-
tops in individual offices and labs. The early computers, on the other hand, were so
huge that they easily occupied the whole basement of a large building.
Advancement of integrated circuits was phenomenal. Large-scale integrated
circuits, commonly known as computer chips or silicon chips, packed the power
equivalent to thousands of transistors and made the notion of a “computer on a sin-
gle chip” a reality. With large-scale integrated circuits, microcomputers emerged in
the mid-1970s. The machines we call personal computers today are descendants of
the microcomputers of the 1970s. The computer chips used in today’s personal
computers pack the power equivalent to several millions of transistors. Personal
computers are fourth-generation computers.
Early microcomputers were isolated, stand-alone machines. The word per-
sonal describes a machine as a personal device intended to be used by an individual.
However, it did not take long to realize there was a need to share computer resources.
For example, early microcomputers required a dedicated printer. Wouldn’t it make
more sense to have many computers share a single printer? Wouldn’t it also make
sense to share data among computers, instead of duplicating the same data on
4 Chapter 0 Introduction to Computers and Programming Languages
If you want to learn more about the history of computing, there is a wealth of information
available on the Web.You can start your exploration from
www.yahoo.com/Computers_and_Internet/History
For more information on the pioneers of computers, visit
en.wikipedia.org/wiki/category:Computer_pioneers
Binary Numbers
To understand the binary number system, let’s first review the decimal number sys-
tem in which we use 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. To represent a number in
the decimal system, we use a sequence of one or more of these digits. The value that
each digit in the sequence represents depends on its position. For example, consider
the numbers 234 and 324. The digit 2 in the first number represents 200, whereas
the digit 2 in the second number represents 20. A position in a sequence has a
value that is an integral power of 10. The following diagram illustrates how the
0.2 Computer Architecture 5
Output
Devices
RAM
Communication
CPU
Devices
Storage
Devices
Input
Devices
Monitor
Printer (output device)
(output device)
Main Unit (housing
CPU, RAM, storage
devices, and
communication
devices)
Keyboard
(input device) Mouse
(input device)
••• • •••
104 103 102 101 100 10⫺1 10⫺2 10⫺3
Position Values
The value of a decimal number (represented as a sequence of digits) is the sum
of the digits, multiplied by their position values, as illustrated:
2 4 8 • 7
⫽ 2 ⫻ 100 ⫹ 4 ⫻ 10 ⫹ 8 ⫻ 1 ⫹ 7 ⫻ 1兾10
base-2
In the decimal number system, we have 10 symbols, and the position values
numbers are integral powers of 10. We say that 10 is the base or radix of the decimal number
system. The binary number system works the same as the decimal number system
binary number but uses 2 as its base. The binary number system has two digits (0 and 1) called bits,
and position values are integral powers of 2. The following diagram illustrates how
bits the values of positions are determined in the binary system:
Binary
Point
••• • •••
24 23 22 21 20 2⫺1 2⫺2 2⫺3
Position Values
1 0 1 • 1
22 21 20 2⫺1
binary-to-
decimal ⫽ 1 ⫻ 22 ⫹ 0 ⫻ 21 ⫹ 1 ⫻ 20 ⫹ 1 ⫻ 2⫺1
conversion
⫽ 1 ⫻ 4 ⫹ 0 ⫻ 2 ⫹ 1 ⫻ 1 ⫹ 1 ⫻ 1兾2
⫽4 ⫹0 ⫹1 ⫹ 1兾2 ⫽ 5.5
The binary system is more suitable for computers than the decimal system be-
cause it is far easier to design an electrical device that can distinguish two states
(bits 0 and 1) than 10 states (digits 0 through 9). For example, we can represent 1 by
turning the switch on and 0 by turning the switch off. In a real computer, 0 is repre-
sented by electrical voltage below a certain level and 1 by electrical voltage at or
above this level.
When you pay closer attention to the on/off switch on computers and other
electronic devices, you should notice an icon like this
RAM
RAM Random access memory or RAM is a repository for both program instructions and
data manipulated by the program during execution. RAM is divided into cells,
byte with each cell having a unique address. Typically, each cell consists of 4 bytes (B),
and a single byte (1 B) in turn consists of 8 bits. Each bit, which can be either on
or off, represents a single binary digit. RAM is measured by the number of bytes
it contains. For example, 128 kilobytes (KB) of RAM contains 128 ⫻ 1024 ⫽
131,072 B because 1 KB is equal to 210 ⫽ 1024 B. Notice that 1 K is not equal to
103, although 103 ⫽ 1000 is a close approximation to 210 ⫽ 1024. The first IBM
PC introduced in 1981 came with 16 KB of RAM, and the first Macintosh com-
puter introduced in 1984 came with 128 KB of RAM. In contrast, a typical PC
today has anywhere from 1GB (gigabytes) to 4GB of RAM. Given that 1GB is equal
to 1024 MB (megabytes) and 1 MB is equal to 1024 KB, we know that 2GB means
2 ⫻ 1024 MB ⫽ 2048 MB ⫽ 2048 ⫻ 1024 KB ⫽ 2,097,152 KB ⫽ 2,097,152 ⫻
1024 B ⫽ 2,147,483,648 B.
8 Chapter 0 Introduction to Computers and Programming Languages
CPU
CPU The central processing unit or CPU is the brain of a computer. The CPU is the com-
ponent that executes program instructions by fetching an instruction (stored in
RAM), executing it, fetching the next instruction, executing it, and so on until it en-
register counters an instruction to stop. The CPU contains a small number of registers, which
are high-speed devices for storing data or instructions temporarily. The CPU also
contains the arithmetic-logic unit (ALU), which performs arithmetic operations such
as addition and subtraction and logical operations such as comparing two numbers.
clock speed CPUs are characterized by their clock speeds. For example, in the Intel
Pentium 200, the CPU has a clock speed of 200 megahertz (MHz). The hertz is a
unit of frequency equal to 1 cycle per second. A cycle is a period of time between
two on states or off states. So 200 MHz equals 200,000,000 cycles per second. The
fastest CPU for commercially available personal computers was around 200 MHz
in 1997 when the first edition of this textbook was published. But by the beginning
of 1998, many vendors started selling 300-MHz machines. And in a mere 6 months,
by the middle of 1998, the top-of-the-line personal computers were 400-MHz ma-
chines. As of this writing in late 2008, we see computers with 2.93-GHz (2930-
MHz) CPU being advertized and sold. The increase of the CPU speed in the last two
decades is truly astonishing. The clock speed of the Intel 8080, the CPU introduced
in 1974 that started the PC revolution, was a mere 2 MHz. In contrast, the clock
speed of the Intel Pentium 4 introduced in 2001 was 2 GHz (2000 MHz). Table 0.1
lists some of the Intel processors.
I/O Devices
I/O devices Input/output or I/O devices allow communication between the user and the CPU.
Input devices such as keyboards and mice are used to enter data, programs, and
commands in the CPU. Output devices such as monitors and printers are used to
display or print information. Other I/O devices include scanners, bar code readers,
magnetic strip readers, digital video cameras, and musical instrument digital inter-
face (MIDI) devices.
Storage Devices
Storage devices such as disk and tape drives are used to store data and programs. Sec-
nonvolatile and
volatile
ondary storage devices are called nonvolatile memory, while RAM is called volatile
memory memory. Volatile means the data stored in a device will be lost when the power to the
device is turned off. Being nonvolatile and much cheaper than RAM, secondary stor-
age is an ideal medium for permanent storage of large volumes of data. A secondary
storage device cannot replace RAM, though, because secondary storage is far slower
in data access (getting data out and writing data in) compared to RAM.
The most common storage device today for personal computers is a disk
drive. There are two kinds of disks: hard and floppy (also known as diskettes). Hard
disks provide much faster performance and larger capacity, but are normally not re-
movable; that is, a single hard disk is permanently attached to a disk drive. Floppy
disks, on the other hand, are removable, but their performance is far slower and
their capacity far smaller than those of hard disks. As the standard floppy disks can
0.2 Computer Architecture 9
A table of Intel processors. For some CPUs, several types with different
Table 0.1 clock speeds are possible. In such case, only the fastest clock speed is
shown. For more information on Intel CPUs, visit http://www.intel.com.
Table CPU
Date
Introduced
Clock Speed
(MHz)
4004 11/15/71 0.108
8008 4/1/72 0.200
1970s 8080 4/1/74 2
8088 6/1/79 8
80286 2/1/82 12
1980s 80386SX 6/16/88 16
80486DX 4/10/89 25
Pentium 3/22/93 66
Pentium Pro 11/1/95 200
1990s Pentium II 5/7/97 300
Pentium II Xeon 6/29/98 400
Pentium III 10/25/99 733
store only up to approximately 1.44 MB, they are becoming less useful in today’s
world of multimegabyte image and sound files. They are fast becoming obsolete,
and hardly anybody uses them anymore. Removable storage media with much
higher capacity such as zip disks (capable of holding 100 to 250 MB of data) re-
placed floppy disks in late 1990s. Computer technology moves so quickly that zip
disks themselves are already becoming obsolete. The most common form of
portable storage medium today (2008) is a compact USB flash drive, also known as
a thumb drive, whose capacity ranges from 125 MB to 16 GB.
Hard disks can store a huge amount of data, typically ranging from 160 GB
(gigabyte; 1 GB ⫽ 1024 MB) to 500 GB for a standard desktop PC in 2008. Portable
and removable hard disk drives, with performance and capacity that rival those of
nonremovable hard disks, are also available, but their use is not widespread.
Compact disks (CDs) are commonly used for storing massive amounts of data,
approximately 700 MB. Many software packages we buy today—computer games,
www.allitebooks.com
10 Chapter 0 Introduction to Computers and Programming Languages
word processors, and others—come with a single CD. Before the CD became a popu-
lar storage device for computers, some software packages came with more than
20 floppy diskettes. Because of the massive storage capacity of the CD, most computer
vendors eliminated printed manuals altogether by putting the manuals on the CD.
Communication Devices
communication A communication device connects the personal computer to an internet. The tradi-
device tional communication device for computers at home and in small offices was the
modem. A modem, which stands for modulator-demodulator, is a device that con-
verts analog signals to digital and digital signals to analog. By using a modem, a
computer can send to and receive data from another computer over the phone line.
The most critical characteristic of a modem is its transmission speed, which is mea-
sured in bits per second (bps). A typical speed for a modem is 56,000 bps, commonly
called a 56K modem. Under an ideal condition (no line noise or congestion), a 56K
modem can transfer a 1 MB file in about 21⁄2 minutes. Frequently, though, the actual
transfer rate is much lower than the possible maximum. So-called DSL and cable
modems are not truly modems because they transfer data strictly in digital mode,
which allows for much faster connection speeds of 144K or above. High-speed satel-
lite connection to the Internet is also available today.
A communication device for connecting a computer to a LAN is a network
interface card (NIC). A NIC can transfer data at a much faster rate than the fastest
modem. For instance, a type of NIC called 10BaseT can transfer data at the rate of
10 Mbps over the network. Traditional networks are connected, or wired, by the cables.
Increasingly, networks are connected wirelessly, where data are carried over radio
waves. Wireless networking is called WiFi or 802.11 networking. Today you will find
wireless networking almost universally available at airports, hotels, and universities.
assembly One level above machine language is assembly language, which allows
language “higher-level” symbolic programming. Instead of writing programs as a sequence
of bits, assembly language allows programmers to write programs by using sym-
bolic operation codes. For example, instead of 10110011, we use MV to move the
contents of a memory cell into a register. We also can use symbolic, or mnemonic,
names for registers and memory cells. A program written in assembly language
might look like this:
MV 0, SUM
assembly code MV NUM, AC
ADD SUM, AC
STO SUM, TOT
Since programs written in assembly language are not recognized by the CPU,
assembler we use an assembler to translate programs written in assembly language into
machine-language equivalents. Compared to writing programs in machine lan-
guage, writing programs in assembly language is much faster, but not fast enough
for writing complex programs.
high-level High-level languages were developed to enable programmers to write pro-
languages grams faster than when using assembly languages. For example, FORTRAN
(FORmula TRANslator), a programming language intended for mathematical com-
putation, allows programmers to express numerical equations directly as
high-level code X = (Y + Z) / 2
0.4 Java
Java Java is a new object-oriented language that is receiving wide attention from both indus-
try and academia. Java was developed by James Gosling and his team at Sun Microsys-
tems in California. The language was based on C and C++ and was originally intended
for writing programs that control consumer appliances such as toasters, microwave
ovens, and others. The language was first called Oak, named after the oak tree outside
of Gosling’s office, but the name was already taken, so the team renamed it Java.
Java is often described as a Web programming language because of its use in
applet writing programs called applets that run within a Web browser. That is, you need a
Web browser to execute Java applets. Applets allow more dynamic and flexible dis-
semination of information on the Internet, and this feature alone makes Java an at-
tractive language to learn. However, we are not limited to writing applets in Java. We
application can write Java applications also. A Java application is a complete stand-alone
program that does not require a Web browser. A Java application is analogous to a
program we write in other programming languages. In this book, we describe Java
applications only because our objective is to teach the fundamentals of object-oriented
programming that are applicable to all object-oriented programming languages.
We chose Java for this textbook mainly for its clean design. The language de-
signers of Java took a minimalist approach; they included only features that are in-
dispensable and eliminated features that they considered excessive or redundant.
This minimalist approach makes Java a much easier language to learn than other
object-oriented programming languages. Java is an ideal vehicle for teaching the
fundamentals of object-oriented programming. All the sample programs in this
book are tested against the newest version, Java 6.0.
S u m m a r y
K e y C o n c e p t s
network binary numbers
LAN binary-to-decimal conversion
WAN machine language
internets and Internet assembly language
CPU assembler
RAM high-level language
I/O devices compiler
communication devices Java
C h a p t e r 0 E x e r c i s e s
Review Exercises
1. Visit your school’s computer lab or a computer store, and identify the
different components of the computers you see. Do you notice any unique
input or output devices?
2. Visit your school’s computer lab and find out the CPU speed, RAM size, and
hard disk capacity of its computers.
3. Convert these binary numbers to decimal numbers.
a. 1010
b. 110011
c. 110.01
d. 111111
14 Chapter 0 Introduction to Computers and Programming Languages
repeat 20 times [
let X = next input;
add X to sum;
]
printout sum;
As my dear Lucy says, I really am, and always have been, a most
extraordinarily lucky man.
THE END
TRANSCRIBER NOTES
Misspelled words and printer errors have been corrected. Some
words are hyphenated by the author for emphasis.
Inconsistencies in punctuation have been maintained.
*** END OF THE PROJECT GUTENBERG EBOOK THE SACK OF
MONTE CARLO: AN ADVENTURE OF TO-DAY ***
ebookball.com