Java Programming: From Problem Analysis to Program Design

Why Rent from Knetbooks?

Because Knetbooks knows college students. Our rental program is designed to save you time and money. Whether you need a textbook for a semester, quarter or even a summer session, we have an option for you. Simply select a rental period, enter your information and your book will be on its way!

Java Programming: From Problem Analysis to Program Design is the ground-breaking, definitive text for the CS[subscript 1] Java course. This text was written and designed for the student. Success with Java will come easily through the numerous clear and concise examples. Drs. Malik and Nair drill down to the most important facet of an introduction to programming course: How does one analyze and solve the problem? Book jacket.

D.S. Malik is a Professor of Mathematics and Computer Science at Creighton University P.S. Nair is the Director of Computer Science at Creighton University

Preface

xxvii

An Overview of Computers and Programming Languages

1

(20)

Introduction

2

(1)

An Overview of the History of Computers

2

(1)

Elements of a Computer System

3

(3)

Hardware

3

(2)

Software

5

(1)

The Language of a Computer

6

(1)

The Evolution of Programming Languages

7

(1)

Processing a Java Program

8

(3)

Programming with the Problem Analysis--Coding--Execution Cycle

11

(4)

Programming Methodologies

15

(2)

Structured Programming

15

(1)

Object--Oriented Programming

16

(1)

Quick Review

17

(1)

Exercises

18

(3)

Basic Elements of Java

21

(76)

The Basics of a Java Program

22

(3)

Special Symbols

23

(1)

Word Symbols

24

(1)

Identifiers

24

(1)

Data Types

25

(5)

Primitive Data Types

26

(4)

Arithmetic Operators and Operator Precedence

30

(3)

Order of Precedence

32

(1)

Expressions

33

(3)

Mixed Expressions

34

(2)

Type Conversion (Casting)

36

(1)

The class String

37

(3)

Parsing Numeric Strings

38

(2)

Input

40

(14)

Allocating Memory with Named Constants and Variables

40

(2)

Putting Data into Variables

42

(2)

Saving and Using the Value of an Expression

44

(1)

Declaring and Initializing Variables

45

(1)

Input (Read) Statement

45

(9)

Increment and Decrement Operators

54

(2)

Strings and the Operator +

56

(2)

Output

58

(7)

The Method flush

64

(1)

Packages, Classes, Methods, and the import Statement

65

(1)

Creating a Java Application Program

66

(5)

Programming Style and Form

71

(4)

Syntax

71

(2)

Documentation

73

(2)

More on Assignment Statements

75

(2)

Programming Example: Convert Length

77

(4)

Programming Example: Make Change

81

(4)

Quick Review

85

(3)

Exercises

88

(7)

Programming Exercises

95

(2)

Introduction to Objects and Input/Output

97

(50)

Objects and Reference Variables

98

(4)

The Operator new

99

(3)

Using Predefined Classes and Methods in a Program

102

(2)

The Dot Between Class (Object) Name and Class Member: A Precaution

103

(1)

The class String

104

(9)

Input/Output

113

(13)

Using Dialog Boxes for Input/Output

113

(7)

Tokenizing a String

120

(4)

Formatting the Output of Decimal Numbers

124

(2)

File Input/Output

126

(4)

Inputting (Reading) Data from a File

127

(1)

Storing (Writing) Output to a File

128

(2)

Programming Example: Movie Ticket Sale and Donation to Charity

130

(6)

Programming Example: Student Grade

136

(3)

Quick Review

139

(1)

Exercises

140

(3)

Programming Exercises

143

(4)

Control Structures I

147

(52)

Control Structures

148

(1)

Relational Operators

149

(2)

Relational Operators and Primitive Data Types

151

(1)

Comparing Strings

152

(4)

Logical (Boolean) Operators and Logical Expressions

156

(2)

Order of Precedence

158

(6)

Short-Circuit Evaluation

162

(1)

The boolean Data Type and Logical (Boolean) Expressions

163

(1)

Selection: if and if...else

164

(13)

One-Way Selection

164

(3)

Two-Way Selection

167

(4)

Compound (Block of) Statements

171

(1)

Multiple Selections: Nested if

172

(3)

Comparing if. . .else Statements with a Series of if Statements

175

(1)

The Conditional Operator (? :)

176

(1)

switch Structures

177

(6)

Programming Example: Cable Company Billing

183

(6)

Quick Review

189

(1)

Exercises

190

(6)

Programming Exercises

196

(3)

Control Structures II

199

(64)

Why Is Repetition Needed?

200

(1)

The while Looping (Repetition) Structure

201

(14)

Counter--Controlled while Loops

204

(3)

Sentinel--Controlled while Loops

207

(6)

Flag--Controlled while Loops

213

(1)

EOF--Controlled while Loops

213

(2)

Programming Example: Checking Account Balance

215

(9)

Programming Example: Fibonacci Number

224

(5)

The for Looping (Repetition) Structure

229

(6)

Programming Example: Classify Numbers

235

(4)

The do...while Looping (Repetition) Structure

239

(4)

break and continue Statements

243

(2)

Nested Control Structures

245

(2)

Quick Review

247

(2)

Exercises

249

(11)

Programming Exercises

260

(3)

Graphical User Interface (GUI) and Object-Oriented Design (OOD)

263

(50)

Graphical User Interface (GUI) Components

264

(26)

Creating a Window

268

(1)

JFrame

269

(5)

Getting Access to the Content Pane

274

(1)

JLabel

275

(4)

JTextField

279

(3)

JButton

282

(8)

Programming Example: Temperature Conversion

290

(8)

Object-Oriented Design

298

(7)

A Simplified OOD Methodology

299

(6)

Implementing Classes and Operations

305

(2)

The class IntClass

306

(1)

Quick Review

307

(1)

Exercises

308

(3)

Programming Exercises

311

(2)

User-Defined Methods

313

(80)

Predefined Methods

314

(5)

User-Defined Methods

319

(9)

Value-Returning Methods

320

(3)

The return Statement

323

(3)

Final Program

326

(2)

Programming Example: Palindrome Number

328

(4)

Flow of Execution

332

(1)

Programming Example: Largest Number

332

(2)

Void Methods

334

(7)

Void Methods without Parameters

334

(7)

Primitive Data Type Variables as Parameters

341

(2)

Reference Variables as Parameters

343

(16)

Parameters and Memory Allocation

347

(1)

Reference Variables of the String Type as Parameters: A Precaution

347

(3)

The class StringBuffer

350

(9)

Scope of an Identifier within a Class

359

(2)

Method Overloading: An Introduction

361

(1)

Programming Example: Data Comparison

362

(12)

Quick Review

374

(2)

Exercises

376

(9)

Programming Exercises

385

(8)

User-Defined Classes and ADTs

393

(74)

Classes

394

(24)

Constructors

397

(1)

Unified Modeling Language Diagrams

398

(1)

Variable Declaration and Object Instantiation

399

(2)

Accessing Class Members

401

(1)

Built-in Operations on Classes

402

(1)

The Assignment Operator and Classes: A Precaution

402

(3)

Class Scope

405

(1)

Methods and Classes

405

(1)

Definitions of the Constructors and Methods of the class Clock

405

(13)

The Copy Constructor

418

(1)

Classes and the Method toString

419

(2)

Static Members of a Class

421

(2)

static Variables (Data Members) of a Class

422

(1)

Finalizers

423

(1)

Creating Your Own Packages

423

(5)

Multiple File Programs

425

(3)

The Reference this

428

(5)

Cascaded Method Calls

429

(4)

Inner Classes

433

(1)

Abstract Data Types

433

(1)

Programming Example: Candy Machine

434

(23)

Quick Review

457

(2)

Exercises

459

(5)

Programming Exercises

464

(3)

Arrays

467

(56)

Why Do We Need Arrays?

468

(1)

Arrays

469

(19)

Alternate Ways to Declare an Array

471

(1)

Accessing Array Components

471

(4)

Specifying Array Size During Program Execution

475

(1)

Arrays and the Instance Variable length

475

(1)

Processing One-Dimensional Arrays

476

(4)

Array Initialization During Declaration

480

(1)

Array Index Out of Bounds Exception

480

(1)

The Assignment Operator, the Relational Operators, and Arrays: A Precaution

481

(3)

Arrays as Parameters to Methods

484

(4)

Parallel Arrays

488

(1)

Arrays of Objects

489

(2)

Two--Dimensional Arrays

491

(15)

Accessing Array Components

494

(2)

Two--Dimensional Array Initialization During Declaration

496

(2)

Processing Two--Dimensional Arrays

498

(6)

Passing Two--Dimensional Arrays as Parameters to Methods

504

(2)

Multidimensional Arrays

506

(2)

Programming Example: Text Processing

508

(4)

Quick Review

512

(1)

Exercises

513

(4)

Programming Exercises

517

(6)

Applications of Arrays and Strings

523

(58)

List Processing

524

(18)

Searching

524

(3)

Sorting a List: Selection Sort

527

(7)

Sequential Search on an Ordered List

534

(3)

Binary Search

537

(5)

The class Vector

542

(7)

Primitive Data Types and the class Vector

548

(1)

Programming Example: Election Results

549

(16)

The class String (Revisited)

565

(5)

Programming Example: Pig Latin Strings

570

(6)

Quick Review

576

(1)

Exercises

576

(3)

Programming Exercises

579

(2)

Inheritance and Composition

581

(88)

Inheritance

582

(20)

Using Methods of the Superclass in a Subclass

584

(6)

Constructors of the Superclass and Subclass

590

(8)

Protected Members of a Class

598

(4)

The class Object

602

(2)

Java Stream Classes

604

(1)

Objects of Superclasses and Subclasses

605

(6)

The Operator instanceof

606

(5)

Abstract Methods and Classes

611

(3)

Interfaces

614

(2)

Composition

616

(5)

Programming Example: Grade Report

621

(36)

Quick Review

657

(1)

Exercises

658

(6)

Programming Exercises

664

(5)

Handling Exceptions and Events

669

(52)

The Java Exception Hierarchy

670

(4)

Java's Exception Classes

674

(3)

Checked and Unchecked Exceptions

677

(1)

Handling Exceptions within a Program

678

(14)

try/catch/finally Block

678

(6)

The class Exception and the Operator instanceof

684

(4)

Rethrowing and Throwing an Exception

688

(4)

The Method printStackTrace

692

(3)

Exception--Handling Techniques

695

(1)

Terminate the Program

695

(1)

Fix the Error and Continue

695

(1)

Log the Error and Continue

695

(1)

Creating Your Own Exception Classes

696

(2)

Event Handling

698

(6)

Programming Example: Calculator

704

(9)

Quick Review

713

(2)

Exercises

715

(4)

Programming Exercises

719

(2)

Advanced GUIs and Graphics

721

(92)

Applets

725

(4)

class Font

729

(3)

class Color

732

(6)

class Graphics

738

(10)

Converting an Application Program to an Applet

745

(3)

Additional GUI Components

748

(30)

JTextArea

749

(5)

JCheckBox

754

(7)

JRadioButton

761

(5)

JComboBox

766

(5)

JList

771

(7)

Layout Managers

778

(6)

FlowLayout

778

(4)

BorderLayout

782

(2)

Menus

784

(2)

Key and Mouse Events

786

(11)

Key Events

787

(3)

Mouse Events

790

(7)

Programming Example: Java Kiosk

797

(8)

Quick Review

805

(1)

Exercises

806

(2)

Programming Exercises

808

(5)

Recursion

813

(38)

Recursive Definitions

814

(3)

Direct and Indirect Recursion

816

(1)

Infinite Recursion

816

(1)

Problem Solving Using Recursion

817

(12)

Recursion or Iteration?

829

(1)

Programming Example: Converting a Number from Decimal to Binary

830

(4)

Programming Example: Sierpinski Gasket

834

(7)

Quick Review

841

(1)

Exercises

842

(2)

Programming Exercises

844

(7)

APPENDIX A Java Reserved Words

851

(2)

APPENDIX B Operator Precedence

853

(2)

APPENDIX C Character Sets

855

(4)

ASCII (American Standard Code for Information Interchange), the first 128 characters of the Unicode character set