Java Programming From Problem Analysis to Program Design 4th Edition D. S. Malik - Download the ebook today and own the complete content
Java Programming From Problem Analysis to Program Design 4th Edition D. S. Malik - Download the ebook today and own the complete content
com
https://ebookgate.com/product/java-programming-from-problem-
analysis-to-program-design-4th-edition-d-s-malik/
OR CLICK BUTTON
DOWLOAD EBOOK
https://ebookgate.com/product/c-programming-from-problem-analysis-to-
program-design-5th-5th-edition-d-s-malik/
ebookgate.com
https://ebookgate.com/product/c-programming-from-problem-analysis-to-
program-design-4th-edition-barbara-doyle/
ebookgate.com
https://ebookgate.com/product/c-programming-program-design-including-
data-structures-7th-edition-d-s-malik/
ebookgate.com
https://ebookgate.com/product/c-programming-program-design-including-
data-structures-6th-edition-d-s-malik/
ebookgate.com
C Programming Program design including data structures
5ed. Edition Malik D.S.
https://ebookgate.com/product/c-programming-program-design-including-
data-structures-5ed-edition-malik-d-s/
ebookgate.com
https://ebookgate.com/product/java-foundations-introduction-to-
program-design-and-data-structures-2nd-edition-john-lewis/
ebookgate.com
https://ebookgate.com/product/data-structures-problem-solving-using-
java-4th-edition-mark-allen-weiss/
ebookgate.com
https://ebookgate.com/product/introduction-to-analysis-4th-edition-
edward-d-gaughan/
ebookgate.com
https://ebookgate.com/product/java-how-to-program-fourth-edition-
harvey-m-deitel/
ebookgate.com
Apago PDF Enhancer
JAVA PROGRAMMING
FROM PROBLEM ANALYSIS TO PROGRAM DESIGN
FOURTH EDITION
D.S. MALIK
Australia Brazil Japan Korea Mexico Singapore Spain United Kingdom United States
Java Programming: From Problem Analysis
For product information and technology assistance, contact us at
to Program Design, Fourth Edition
Cengage Learning Customer & Sales Support, 1-800-354-9706
D.S. Malik For permission to use material from this text or product, submit
all requests online at cengage.com/permissions
Executive Editor: Marie Lee
Further permissions questions can be emailed to
Acquisitions Editor: Amy Jollymore [email protected]
Senior Product Manager: Alyssa Pratt
Editorial Assistant: Julia Leroux-Lindsey
ISBN-13: 978-1-4390-3566-5
Marketing Manager: Bryant Chrzan
ISBN-10: 1-4390-3566-0
Senior Content Project Manager: Catherine
Course Technology
DiMassa
20 Channel Center
Art Director: Marissa Falco Boston, MA 02210
Compositor: Integra USA
Cengage Learning is a leading provider of customized
learning solutions with office locations around the globe,
including Singapore, the United Kingdom, Australia, Mexico,
ª 2010 Course Technology, Cengage Brazil, and Japan. Locate your local office at:
Learning international.cengage.com/region
ALL RIGHTS RESERVED. No part of this work Cengage Learning products are represented in Canada
covered by the copyright herein may be by Nelson Education, Ltd.
reproduced, transmitted, stored or used in any
form or by any means graphic, electronic, or For your lifelong learning solutions, visit
mechanical, including but not limited to course.cengage.com
photocopying, recording, scanning, digitizing, Visit our corporate website at cengage.com
Apago Some
taping, Web distribution, information PDF Enhancer
of the product names and company names used in this
networks, or information storage and retrieval book have been used for identification purposes only and may
systems, except as permitted under Section be trademarks or registered trademarks of their respective
107 or 108 of the 1976 United States Copyright manufacturers and sellers.
Act, without the prior written permission of
the publisher. Any fictional data related to persons or companies or URLs used
throughout this book is intended for instructional purposes only.
At the time this book was printed, any such data was fictional
and not belonging to any real persons or companies.
Course Technology, a part of Cengage Learning, reserves the
right to revise this publication and make changes from time to
time in its content without notice.
The programs in this book are for instructional purposes only.
They have been tested with care, but are not guaranteed for any
particular intent beyond educational purposes. The author and
the publisher do not offer any warranties or representations, nor
do they accept any liabilities with respect to the programs.
Printed in Canada
1 2 3 4 5 6 7 12 11 10 09
TO
Apago My Daughter
PDF Enhancer
Shelly Malik
This page intentionally left blank
PREFACE xix
9. Arrays 509
INDEX 973
TABLE OF CONTENTS
Preface xix
Exercises 23
Data Types 32
Primitive Data Types 32
Expressions 40
Mixed Expressions 41
class String 45
Strings and the Operator + 46
Input 48
Allocating Memory with Named Constants and Variables 48
Putting Data into Variables 51
Declaring and Initializing Variables 55
Input (Read) Statement 56
Reading a Single Character 61
Quick Review 91
Exercises 94
Input/Output 123
Formatting Output with printf 123
Using Dialog Boxes for Input/Output 132
Formatting the Output Using the String Method format 139
Exercises 158
Exercises 279
Programming Exercises 289
Exercises 346
Exercises 410
ARRAYS 509
9 Why Do We Need Arrays? 510
Arrays 511
Alternate Ways to Declare an Array 513
Accessing Array Elements 513
Specifying Array Size during Program Execution 515
Array Initialization during Declaration 516
Arrays and the Instance Variable length 516
Processing One-Dimensional Arrays 517
Array Index Out of Bounds Exception 522
Declaring Arrays as Formal Parameters to Methods 522
Assignment Operator, Relational Operators, and Arrays:
A Precaution 523
Arrays as Parameters to Methods 525
Table of Contents | xiii
Exercises 581
Exercises 663
Exercises 728
Exercises 816
RECURSION 823
13 Recursive Definitions 824
Direct and Indirect Recursion 826
Infinite Recursion 827
Designing Recursive Methods 827
Problem Solving Using Recursion 828
Tower of Hanoi: Analysis 837
xvi | Java Programming: From Problem Analysis to Program Design, Fourth Edition
Exercises 847
Exercises 887
Packages and
Apago PDFUser-Defined
EnhancerClasses 924
PrimitiveType Classes 924
Class: IntClass 924
Class: LongClass 928
Class: CharClass 929
Class: FloatClass 929
Class: DoubleClass 930
Class: BooleanClass 931
Using PrimitiveType Classes in a Program 932
Chapter 2 950
xviii | Java Programming: From Problem Analysis to Program Design, Fourth Edition
Chapter 3 952
Chapter 4 953
Chapter 5 955
Chapter 6 957
Chapter 7 962
Chapter 8 963
Chapter 9 965
Chapter 10 966
Chapter 11 968
Chapter 12 970
Chapter 13 971
Chapter 14 971
Apago
INDEX PDF Enhancer 973
P REFACE TO THE F OURTH E DITION
Welcome to Java Programming: From Problem Analysis to Program Design, Fourth Edition. Designed
for a first Computer Science (CS1) Java course, this text will provide a breath of fresh air to you
and your students. The CS1 course serves as the cornerstone of the Computer Science curricu-
lum. My primary goal is to motivate and excite all programming students, regardless of their level.
Motivation breeds excitement for learning. Motivation and excitement are critical factors that
lead to the success of the programming student. This text is the culmination and development of
my classroom notes throughout more than fifty semesters of teaching successful programming.
Warning: This text can be expected to create a serious reduction in the demand for program-
ming help during your office hours. Other side effects include significantly diminished student
dependency on others while learning to program.
The primary focus in writing this text is on student learning. Therefore, in addition to clear
explanations, we address the key issues that otherwise impede student learning. For example, a
common question that arises naturally during an early programming assignment is: ‘‘How
Apago PDF Enhancer
many variables and what kinds are needed in this program?’’ We illustrate this important and
crucial step by helping students learn why variables are needed and how data in a variable is
manipulated. Next students learn that the analysis of the problem will spill the number and
types of the variables. Once students grasp this key concept, control structures, (selection and
loops) become easier to learn. The second major impediment in learning programming is
parameter passing. We pay special attention to this topic. First students learn how to use
predefined methods and how actual and formal parameters relate. Next students learn about
user-defined methods. They see visual diagrams that help them learn how methods are called
and how formal parameters affect actual parameters. Once students have a clear understanding
of these two key concepts, they readily assimilate advanced topics.
The topics are introduced at a pace that is conducive to learning. The writing style is friendly,
engaging, and straightforward. It parallels the learning style of the contemporary CS1 student.
Before introducing a key concept, the student learns why the concept is needed, and then sees
examples illustrating the concept. Special attention is paid to topics that are essential in
mastering the Java programming language and in acquiring a foundation for further study
of computer science.
Other important topics include debugging techniques and techniques for avoiding program-
ming bugs. When a beginner compiles his/her first program and sees that the number of
errors exceeds the length of this first program, he/she becomes frustrated by the plethora of
errors, only some of which can be interpreted. To ease this frustration and help students learn
to produce correct programs, debugging and bug avoidance techniques are presented system-
atically throughout the text.
xx | Java Programming: From Problem Analysis to Program Design, Fourth Edition
Approach
Once conceived as a Web programming language, Java slowly but surely found its way into
classrooms where it now serves as a first programming language in computer science curricula
(CS1). Java is a combination of traditional style programming—programming with a non-
graphical user interface—and modern style programming with a graphical user interface
(GUI). This book introduces you to both styles of programming. After giving a brief
description of each chapter, we discuss how to read this book.
Chapter 1 briefly reviews the history of computers and programming languages. The reader
can quickly skim and become familiar with some of the hardware and software components of
the computer. This chapter also gives an example of a Java program and describes how a Java
Preface to the Fourth Edition | xxi
Chapter 13 introduces recursion. Several examples illustrate how recursive methods execute.
Chapter 14 discusses a binary search algorithm as well as bubble sort, selection sort, insertion
sort, and quick sort algorithms. The sorting algorithms: bubble sort and quick sort are
provided on the Web site and the CD accompanying this book.
Appendix A lists the reserved words in Java. Appendix B shows the precedence and
associativity of the Java operators. Appendix C lists the ASCII (American Standard Code
for Information Interchange) portion of the Unicode character set as well as the EBCDIC
(Extended Binary Code Decimal Interchange) character set.
Appendix D contains additional topics in Java. The topics covered are converting a base 10
number to binary (base 2) number and vice versa, converting a number from base 2 to base 8
(base 16) and vice versa, how to compile and execute a Java program using command line
statements, how to create Java style documentation of the user-defined classes, how to create
packages, how to use user-defined classes in a Java program, and enum type. Appendix E
gives answers to all the odd-numbered exercises in the text.
Figure 1 shows a chapter dependency diagram for this book. Solid arrows indicate that the
chapter at the beginning of the arrow is required before studying the chapter at the end of the
arrow. A dotted arrow indicates that the chapter at the beginning of the arrow is not essential
to studying the chapter at the end of the dotted arrow.
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6 Chapter 7
Chapter 8 Chapter 9
Apago PDF Enhancer
Chapter 10 Chapter 14
Chapter 11
Chapter 12
Chapter 13
A dotted arrow means that the chapter is not essential to studying the following chapter.
All source code and solutions have been written, compiled, and quality assurance tested
with Java 6.0 and the version of Java 7.0 available at the time the book was being typeset.
FEATURES OF THE BOOK
Four-color
interior design
shows
accurate code
and related
comments.
More than
Apago PDF Enhancer
250 visual
diagrams,
both
extensive and
exhaustive,
illustrate difficult
concepts.
Apago PDF Enhancer
Numbered Examples
illustrate the key
concepts with their
relevant code. The
programming code in
these examples is
followed by a Sample
Run. An explanation
then follows that
describes what each
line in the code does.
Apago PDF Enhancer
Notes highlight
important facts
about the concepts
introduced in the
chapter.
Programming
Examples are
complete programs
featured in each
chapter. These
examples include the
accurate, concrete
stages of Input,
Output, Problem
Analysis and Algorithm
Design, and a
Complete Program
Listing.
Exercises further
reinforce learning
and ensure that
students have, in
fact, mastered the
material.
Programming
Exercises challenge
students to write
Java programs with
a specified
outcome.
The following supplemental materials are available when this book is used in a classroom
setting.
All instructor teaching tools, outlined below, are available with this book on a single
CD-ROM.
Ò
ExamView
Apago PDF Enhancer
This textbook is accompanied by ExamView, a powerful testing software package that
allows instructors to create and administer printed, computer (LAN-based), and Internet
exams. ExamView includes hundreds of questions that correspond to the topics covered in
this text, enabling students to generate detailed study guides that include page references for
further review. These computer-based and Internet testing components allow students to
take exams at their computers, and save the instructor time because each exam is graded
automatically.
PowerPoint Presentations
This book comes with Microsoft PowerPoint slides for each chapter. These slides are included as
a teaching aid for classroom presentations, either to make available to students on the network
for chapter review, or to be printed for classroom distribution. Instructors can add their own
slides for additional topics that they introduce to the class.
Distance Learning
Cengage Course Technology is proud to present online courses in WebCT and Blackboard
to provide the most complete and dynamic learning experience possible. For more informa-
tion on how to bring distance learning to your course, contact your local Cengage Course
Technology sales representative.
xxxii | Java Programming: From Problem Analysis to Program Design, Fourth Edition
Source Code
The source code is available at http://www.cengage.com/highered/, and is also available on the
Instructor Resources CD-ROM and the CD accompanying this text. The input files needed
to run some of the programs are also included with the source code.
Solution Files
The solution files for all programming exercises are available at http://www.cengage.com/highered/,
and are also available on the Instructor Resources CD-ROM. The input files needed to run some
of the programming exercises are also included with the solution files.
There are many people I must thank who, one way or another, contributed to the success of
this book. First, I would like to thank those who e-mailed numerous comments to improve
on the third edition. I am thankful to Professors S.C. Cheng, Randall Crist, and John N.
Mordeson, for constantly supporting this project.
I owe a great deal to the following reviewers, who patiently read each page of every chapter
of the current version and made critical comments to improve on the book: Atef Bader,
DePaul University; Rajwant Gill, Anne Arundel Community College; Gerard Gordon,
DePaul University; Jenneth Honeycutt, Fayetteville Technical Community College; and
John Mill, Spokane Falls Community College. Additionally, I would like to thank the
reviewers of the proposal package: Lemond Hall, South Georgia Technical College and
Hemand Pendharkar, Worcester State College. The reviewers will recognize that their
suggestions have not been overlooked and, in fact, made this a better book.
Next, I express thanks to Amy Jollymore, Acquisitions Editor, for recognizing the importance
Apago PDF Enhancer
and uniqueness of this project. All this would not have been possible without the careful
planning of Senior Product Manager Alyssa Pratt. I extend my sincere thanks to Alyssa, as well
as to Senior Content Project Manager, Catherine DiMassa. I also thank Tintu Thomas of
Integra Software Services for assisting us in keeping the project on schedule. I would like to
thank Chris Scriver and Serge Palladino of the QA department of Course Technology for
patiently and carefully proofreading the text, testing the code, and discovering typos and erros.
I am thankful to my parents for their blessings.
Finally, I am thankful to the support of my wife Sadhana, and especially my daughter Shelly,
to whom this book is dedicated. They cheered me up whenever I was overwhelmed during
the writing of this book.
We welcome any comments concerning the text. Comments may be forwarded to the
following e-mail address: [email protected].
D.S. Malik
This page intentionally left blank
1
A N OApago
VERVIEW OF C
PDF Enhancer
OMPUTERS
AND P ROGRAMMING L ANGUAGES
IN THIS CHAPTER, YOU WILL:
Introduction
Terms such as ‘‘the Internet,’’ which was unfamiliar just a few years ago, are now
common. Elementary school students regularly ‘‘surf’’ the Internet and use computers
to design their classroom projects. Many people use the Internet to look up information
and to communicate with others. These Internet activities are all made possible by the
availability of different software, also known as computer programs. Software is devel-
oped by using programming languages. The programming language Java is especially well
suited for developing software to accomplish specific tasks. Our main objective is to teach
you how to write programs in the Java programming language. Before you begin
programming, it is useful if you understand some of the basic terminology and different
components of a computer. We begin with an overview of the history of computers.
Hollerith founded the Tabulating Machine Company, which later became the computer
and technology corporation known as IBM. 1
The first computer-like machine was the Mark I. It was built, in 1944, jointly by IBM and
Harvard University under the leadership of Howard Aiken. Punched cards were used to
feed data into the machine. Mark I was 52 feet long, weighed 50 tons, and had 750,000
parts. In 1946, ENIAC (Electronic Numerical Integrator and Calculator) was built at the
University of Pennsylvania. It contained 18,000 vacuum tubes and weighed some 30 tons.
The computers that we know today use the design rules given by John von Neumann in
the late 1940s. His design included components such as arithmetic logic unit, control
unit, memory, and input/output devices. These components are described in the next
section. Von Neumann computer design makes it possible to store the programming
instruction and the data in the same memory space. In 1951, the UNIVAC (Universal
Automatic Computer) was built and sold to the U.S. Census Bureau.
In 1956, the invention of the transistors resulted in smaller, faster, more reliable, and more
energy-efficient computers. This era also saw the emergence of the software development
industry with the introduction of FORTRAN and COBOL, two early programming
languages. In the next major technological advancement, transistors were replaced by tiny
integrated circuits or ‘‘chips.’’ Chips are smaller and cheaper than transistors and can
contain thousands of circuits on a single chip. They give computers tremendous proces-
sing speed.
Apago PDF Enhancer
In 1970, the microprocessor, an entire CPU on a single chip, was invented. In 1977,
Stephen Wozniak and Steven Jobs designed and built the first Apple computer in their
garage. In 1981, IBM introduced its personal computer (PC). In the 1980s, clones of the
IBM PC made the personal computer even more affordable. By the mid-1990s, people
from many walks of life were able to afford them. Computers continue to become faster
and less expensive as technology advances.
Modern-day computers are very powerful, reliable, and easy to use. They can accept
spoken-word instructions and imitate human reasoning through artificial intelligence.
Expert systems assist doctors in making diagnoses. Mobile computing applications are
growing significantly. Using hand-held devices, delivery drivers can access global posi-
tioning satellites (GPS) to verify customer locations for pickups and deliveries. Cell
phones can check your e-mail, make airline reservations, see how stocks are performing,
and access your bank accounts.
Although there are several categories of computers, such as mainframe, midsize, and
micro, all computers share some basic elements.
In today’s market, personal computers are sold with descriptions such as a Pentium 4
Processor 2.80 GHz, 1 GB RAM, 100 GB HD, VX750 17’’ Silver Flat CRT Color Monitor,
preloaded with software such as an operating system, games, encyclopedias, and application
software such as word processors or money management programs. These descriptions
represent two categories: hardware and software. Items such as ‘‘Pentium 4 Processor 2.80
GHz, 1 GB RAM, 100 GB HD, VX750 17’’ Silver Flat CRT Color Monitor’’ fall into the
hardware category; items such as ‘‘operating system, games, encyclopedias, and application
software’’ fall into the software category. Let’s look at the hardware first.
Hardware
Major hardware components include the central processing unit (CPU); main memory
(MM), also called random access memory (RAM); input/output devices; and secondary
storage. Some examples of input devices are the keyboard, mouse, and secondary storage.
Examples of output devices are the monitor, printer, and secondary storage.
(a) (b)
Main memory, or random access memory (RAM), is connected directly to the CPU.
All programs must be loaded into main memory before they can be executed. Similarly,
Elements of a Computer System | 5
all data must be brought into main memory before a program can manipulate it. When
the computer is turned off, everything in main memory is lost. 1
Main memory is an ordered sequence of cells, called memory cells. Each cell has a unique
location in main memory, called the address of the cell. These addresses help you access
the information stored in the cell. Figure 1-1(b) shows main memory with some data.
Today’s computers come with main memory consisting of millions to billions of cells.
Although Figure 1-1(b) shows data stored in cells, the content of a cell can be either a
programming instruction or data. Moreover, this figure shows the data as numbers and
letters. However, as explained later in this chapter, main memory stores everything as
sequences of 0s and 1s. The memory addresses are also expressed as sequences of 0s and 1s.
SECONDARY STORAGE
Because programs and data must be stored in main memory before processing, and
because everything in main memory is lost when the computer is turned off, information
stored in main memory must be transferred to some other device for longer-term storage.
A device that stores longer-term information (unless the device becomes unusable or you
change the information by rewriting it) is called secondary storage. To be able to
transfer information from main memory to secondary storage, these components must be
connected directly to each other. Examples of secondary storage are hard disks, floppy
disks, flash memory, ZIP disks, CD-ROMs, and tapes.
Apago PDF Enhancer
INPUT/OUTPUT DEVICES
For a computer to perform a useful task, it must be able to take in data and programs and
display the results of the manipulation of the data. The devices that feed data and
programs into computers are called input devices. The keyboard, mouse, and secondary
storage are examples of input devices. The devices that the computer uses to display and
store results are called output devices. A monitor, printer, and secondary storage are
examples of output devices. Figure 1-2 shows some input and output devices.
Software
Software consists of programs written to perform specific tasks. For example, you use
word-processing programs to write letters, papers, and books. The two types of programs
are system programs and application programs.
System programs control the computer. The system program that loads first when
you turn on your PC is called the operating system. Without an operating system,
the computer is useless. The operating system monitors the overall activity of the
computer and provides services, such as memory management, input/output activities,
and storage management. The operating system has a special program that organizes
secondary storage so that you can access information conveniently. The operating
system is the program that runs the application programs. Application programs
perform specific tasks. Word processors, spreadsheets, and games are examples of
application programs. Both operating systems and application programs are written in
programming languages.
Language of a Computer
When you press A on your keyboard, the computer displays A on the screen, but what is
actually stored inside the computer’s main memory? What is the language of the
Apago PDF Enhancer
computer? How does it store whatever you type on the keyboard?
Remember that a computer is an electronic device. Electrical signals move along channels
inside the computer. There are two types of electrical signals: analog and digital. Analog
signals are continuous waveforms used to represent things, such as sound. Audio tapes,
for example, store data in analog signals. Digital signals represent information with a
sequence of 0s and 1s. A 0 represents a low voltage, and a 1 represents a high voltage.
Digital signals are more reliable carriers of information than analog signals and can be
copied from one device to another with exact precision. You might have noticed that
when you make a copy of an audio tape, the sound quality of the copy is not as good as
that on the original tape. Computers use digital signals.
Because digital signals are processed inside a computer, the language of a computer, called
machine language, is a sequence of 0s and 1s. The digit 0 or 1 is called a binary digit,
or bit. Sometimes a sequence of 0s and 1s is referred to as a binary code or a binary
number.
Bit: A binary digit 0 or 1.
A sequence of eight bits is called a byte. Moreover, 210 ¼ 1024 bytes and is called a
kilobyte (KB). Table 1-1 summarizes the terms used to describe the various numbers of
bytes.
Language of a Computer | 7
The number system that we use in our daily life is called the decimal system or base 10.
Because everything inside a computer is represented as a sequence of 0s and 1s, that
is, binary numbers, the number system that a computer uses is called binary or base 2.
We indicated in the preceding paragraph that the number 1000001 is the binary
representation of 65. Appendix D describes how to convert a number from base 10 to
base 2 and vice versa. Appendix D also describes how to convert a number between
base 2 and base 16 (hexadecimal) and between base 2 and base 8 (octal).
Inside the computer, every character is represented as a sequence of eight bits, that is, as a
byte. Because ASCII is a seven-bit code, you must add 0 to the left of the ASCII
encoding of a character. Hence, inside the computer, the character A is represented as
01000001, and the character 3 is represented as 00110011.
8 | Chapter 1: An Overview of Computers and Programming Languages
To represent the weekly wages equation in machine language, the programmer had to
remember the machine language codes for various operations. Also, to manipulate data,
the programmer had to remember the locations of the data in main memory. Remem-
bering specific codes made programming difficult and error prone.
Assembly languages were developed to make the programmer’s job easier. In assembly
language, an instruction is an easy-to-remember form called a mnemonic. Table 1-2
shows some examples of instructions in assembly language and their corresponding
machine language code.
Evolution of Programming Languages | 9
STOR 100010
MULT 100110
ADD 100101
SUB 100011
Using assembly language instructions, you can write the equation to calculate the weekly
wages as follows:
LOAD rate
MULT hours
STOR wages
As you can see, it is much easier to write instructions in assembly language. However, a
computer cannot execute assembly language instructions directly. The instructions first
have to be translated into machine language. A program called an assembler translates
the assembly language instructions into machine language.
Apago PDF Enhancer
Assembler: A program that translates a program written in assembly language into an
equivalent program in machine language.
Moving from machine language to assembly language made programming easier, but a
programmer was still forced to think in terms of individual machine instructions. The
next step toward making programming easier was to devise high-level languages that
were closer to spoken languages, such as English and Spanish. Basic, FORTRAN,
COBOL, Pascal, C, C++, and Java are all high-level languages. You will learn the
high-level language Java in this book.
In Java, you write the weekly wages equation as follows:
wages = rate * hours;
Recall that the computer understands only machine language. Moreover, different types
of CPUs use different machine languages. To make Java programs machine indepen-
dent, that is, able to run on many different types of computer platforms, the designers of
Java introduced a hypothetical computer called the Java Virtual Machine (JVM). In
fact, bytecode is the machine language for the JVM.
In languages such as C and C++, the compiler translates the source code directly into the
machine language of your computer’s CPU. For such languages, a different compiler is
needed for each type of CPU. Therefore, programs in these languages are not easily
portable from one type of machine to another. The source code must be recompiled for
each type of CPU. To make Java programs machine independent and easily portable, and
to allow them to run on a Web browser, the designers of Java introduced the Java Virtual
Machine (JVM) and bytecode as the (machine) language of this machine. It is easier to
translate a bytecode into a particular type of CPU. This concept is covered further in the
following section, Processing a Java Program.
Recall that a computer can understand only machine language. Therefore, in order to run
this program successfully, the code must first be translated into the machine language. In
this section we review the steps required to execute programs written in Java.
To process a program written in Java, you carry out the following steps, as illustrated in
Figure 1-3.
1. You use a text editor, such as Notepad, to create (that is, type) a
program in Java following the rules, or syntax, of the language. This
program is called the source program. The program must be saved in
a text file named ClassName.java, where ClassName is the name of
the Java class contained in the file. For example, in the Java program
Exploring the Variety of Random
Documents with Different Content
The magazine relates further, that “scarcity at
home” had induced a greater number of Highlanders
than usual to come to Edinburgh for “harvest work.”
The harvest, however, was not ready. They had
nothing to live upon. “Contributions were set on foot,
to give them two meals a day at the poorhouse; and,
on the evening of September 21, after a sermon
suitable to the occasion by Mr. Whitefield, a
collection was made for them, in the Orphan Hospital
Park, which amounted to £60 11s. 4d. sterling, of
which half a guinea was given by Mr. Whitefield
himself” (p. 465).
“Not many mighty, not many noble, are called; but some
come to hear at Tottenham Court. Shuter, the player,
always makes one of the auditory, and, as I hear, is much
impressed, and brings others with him.”
The good Countess, from this, was led to take an
interest in Shuter’s welfare. In a letter, to Lady Fanny
Shirley, she says:—
“I have had a visit from Shuter, the comedian, whom I
saw in the street, and asked to call on me. He was
wonderfully astonished when I announced my name. We
had much conversation; but he cannot give up his
profession for another more reputable. He spoke of
Mr. Whitefield with great affection, and with admiration of
his talents. He promised to come some other time, when he
had more leisure for conversation. Poor fellow! I think he is
not far from the kingdom.” 419
“July 9, 1757.
“The next morning, I set out for Port Arlington, and left
my persecutors to His mercy, who out of persecutors has
often made preachers.” 433
ebookgate.com