• No results found

An Introduction to Object-Oriented Programming with

N/A
N/A
Protected

Academic year: 2021

Share "An Introduction to Object-Oriented Programming with"

Copied!
7
0
0

Loading.... (view fulltext now)

Full text

(1)

An Introduction to

Object-Oriented

Programming with

Java

TM

C. Thomas Wu

Naval Postgraduate School

M l McGraw-Hill

Boston Burr Ridge, IL Dubuque, IA Madison, WI New York San Francisco St. Louis Bangkok Bogota Caracas Lisbon London Madrid Mexico City Milan New Delhi Seoul Singapore Sydney Taipei Toronto

(2)

Contents

Preface xiii

Major Features xiv Supplement Materials xxii Book Organization xxiv Acknowledgments xxvi

Introduction to Computers and Programming Languages 1

0.1 A History of Computers 2 0.2 Computer Architecture 5 0.3 Programming Languages 11 0.4 Java 13

0.5 Exercises 14

Introduction to Object-Oriented Programming and

Software Development 15

1.1 Classes and Objects 16 1.2 Messages and Methods 19 1.3 Class and Instance Data Values 24 1.4 Inheritance 27

1.5 Software Engineering and Software Life Cycle 30 1.6 Having Fun with Java 32

(3)

Contents

Java Programming Basics

2.1 The First Java Application 40 2.2 Program Components 49 2.3 Edit-Compile-Run Cycle 59 2.4 The javabook Package 62

2.5 Sample Program: Displaying Messages 63 2.6 The First Java Applet 68

2.7 Exercises 78

Numerical Data

3.1 Variables 84

3.2 Arithmetic Expressions 92 3.3 Constants 98

3.4 The Math Class 100

3.5 InputBox 103 3.6 OutputBox 106

3.7 Sample Program: Loan Calculator 113 3.8 Numerical Representation (Optional) 125 3.9 Exercises 129

Defining Instantiable Classes

4.1 Defining Instantiable Classes 142 4.2 Instantiable Classes and Constructors 154 4.3 Visibility Modifiers: public and private 159

4.4 Local Variables, Return Values, and Parameter Passing 164 4.5 Loan Calculator Program with an Instantiable Class 170 4.6 Exercises 185

(4)

Contents ix

Processing Input with Applets 195

5.1 Placing GUI Objects on Applets 196

5.2 Adding an Action Listener to an Event Source 204 5.3 Absolute Positioning of GUI Objects 211

5.4 The Button Class 212

5.5 Converting Text to a Numerical Value 215

5.6 Sample Program: Finding Body Mass Index (BMI) 218 5.7 Running an Applet as an Application 229

5.8 Exercises 230

Selection Statements 235

6.1 The if Statement 236

6.2 Boolean Expressions and Variables 244 6.3 Nested-if Statements 249

6.4 ListBox 256

6.5 The switch Statement 260

6.6 Sample Program: Drawing Shapes 266 6.7 Exercises 289

Repetition Statements 297

7.1 The while Statement 298

7.2 Pitfalls in Writing Repetition Statements 302 7.3 The do-while Statement 307

7.4 ResponseBox 312

7.5 The for Statement 317 7.6 Nested for Statements 322 7.7 The Format Class 325 7.8 Loan Tables 332

(5)

X Contents

7.10 (Optional) Recursive Methods 349 7.11 Exercises 353

Characters and Strings 365

8.1 Characters 366 8.2 Strings 369

8.3 Primitive versus Reference Types 381

8.4 StringBuffer 389

8.5 Passing Objects as Parameters 396 8.6 Returning an Object from Methods 401 8.7 Sample Program: Word Play 404 8.8 Exercises 417

425

Arrays

9.1 y.z 9.3 9.4 9.5 9.6 9.7 9.8 Array Basics 426 Arrays of Objects 435

Passing Arrays to Methods 446 MultilnputBox 452

Self-Referencing Pointer 455

Sample Development: The Address Book 462 Two-Dimensional Arrays 480

Exercises 488

File Input and Output 493

10.1 File and FileDialog Objects 494 10.2 Low-Level File I/O 500 10.3 High-Level File I/O 506 10.4 Handling Exceptions 513 10.5 Object I/O 519

(6)

Contents

10.6 Sample Class: Saving an AddressBook Object 522 10.7 Exercises 531

Reusable Classes and Packages 535

11.1 Object Categories 536

11.2 Method Overriding and Overloaded Methods 538 11.3 Sample Classes: Reusable EggyPeggy and HiLo 545 11.4 Package Organization 572

11.5 Exercises 575

GUI Objects and Event-Driven Programming 581

12.1 Placing Buttons on a Frame 583 12.2 Handling Events 589

12.3 Placing TextField Objects on a Frame 596 12.4 Menus 598

12.5 Handling Mouse Events 605 12.6 Other GUI Objects 615

12.7 Sample Program: A Simple Calculator 620 12.8 Exercises 633

Inheritance and Polymorphism 639

13.1 Defining Classes with Inheritance 640

13.2 Using Classes Effectively with Polymorphism 645 13.3 Inheritance and Member Accessibility 648 13.4 Inheritance and Constructor 654

13.5 Abstract Superclasses and Abstract Methods 659 13.6 When and When Not to Use Inheritance 665 13.7 Sample Program: Computing Course Grades 667 13.8 Exercises 689

(7)

XII Contents

Class Roster Maintenance Program 695

14.1 Method Call Sequence Diagram 696 14.2 Problem Statement 697

14.3 Overall Planning 698

14.4 Step 1 Development: Program Shell with Menus 701 14.5 Step 2 Development: Create a New Roster 708 14.6 Step 3 Development: Add Students 716 14.7 Step 4 Development: Delete Students 724 14.8 Step 5 Development: Edit Student Names 728

14.9 Step 6 Development: Edit Test Scores and Compute Grades 734 14.10 Step 7 Development: Display Student Information 741

14.11 Step 8 Development: Storing Grade Rosters 754 14.12 Step 9 Development: Finalize and Improve 764 14.13 The StudentNameDialog Class 765

14.14 The TestScoreDialog Class 775

14.15 Exercises 780

Searching, Sorting, and Recursive Algorithms 785

15.1 Searching 786 15.2 Sorting 792 15.3 Recursion 800

15.4 When Not to Use Recursion 817 15.5 Exercises 819

References

Related documents

F ortunato tel ah menyerangku ribuan kali dan aku bersabar sebisa mungkin. Namun, saat ia menghinaku, aku ber- sumpah akan membalasnya. Tentu saja aku tak mengatakan

Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao Paulo Sydney. Hong Kong Seoul Singapore

Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore

We have reviewed the interim condensed consolidated financial statements, comprising the consoli- dated statement of financial position, the consolidated income statement,

To sum up, it is the aim of the dissertation to shed light on the possibilities to encounter the shortage of skills in the ICT-industry which are to analyze the career choices

However, if an organisation has a culture open to learning, this makes it easier to change human resources development practices, such as devolving responsibility

1 Bernard Beckerman, Shakespeare at the Globe, 1599-1609 (New York: Macmillan, 1962); Roslyn Lander Knutson, The Repertory of Shakespeare’s Company, 1594-1613 (Fayetteville:

There is an important contrast between our results and a recent study in which children with autism saw unnecessary actions on novel objects and showed the same rate of