D.S. Malik – Java(TM) Programming, 4th Ed. [Course Technology, 2009]
Apago PDF Enhancer Apago PDF Enhancer J AVA P ROGRAMMING F ROM P ROBLEM A NALYSIS TO P ROGRAM D ESIGN F OURTH E DITION D.S. M ALIK Australia Brazil Japan Korea Mexico Singapore Spain United Kingdom United States Apago PDF Enhancer Java Programming: From Problem Analysis to Program Design, Fourth Edition D.S. Malik Executive Editor: Marie Lee Acquisitions Editor: Amy Jollymore Senior Product Manager: Alyssa Pratt Editorial Assistant: Julia Leroux-Lindsey Marketing Manager: Bryant Chrzan Senior Content Project Manager: Catherine DiMassa Art Director: Marissa Falco Compositor: Integra ª 2010 Course Technology, Cengage Learning ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced, transmitted, stored or used in any form or by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the publisher. Printed in Canada 123456712111009 For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706 For permission to use material from this text or product, submit all requests online at cengage.com/permissions Further permissions questions can be emailed to permissionrequest@cengage.com ISBN-13: 978-1-4390-3566-5 ISBN-10: 1-4390-3566-0 Course Technology 20 Channel Center Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit course.cengage.com Visit our corporate website at cengage.com Some of the product names and company names used in this book have been used for identification purposes only and may be trademarks or registered trademarks of their respective manufacturers and sellers. 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. Apago PDF Enhancer TO My Daughter Shelly Malik Apago PDF Enhancer This page intentionally left blank Apago PDF Enhancer PREFACE xix 1. An Overview of Computers and Programming Languages 1 2. Basic Elements of Java 25 3. Introduction to Objects and Input/Output 107 4. Control Structures I: Selection 165 5. Control Structures II: Repetition 225 6. Graphical User Interface (GUI) and Object-Oriented Design (OOD) 295 7. User-Defined Methods 351 8. User-Defined Classes and ADTs 425 9. Arrays 509 10. Inheritance and Polymorphism 591 11. Handling Exceptions and Events 673 12. Advanced GUIs and Graphics 733 13. Recursion 823 14. Searching and Sorting 855 APPENDIX A Java Reserved Words 891 APPENDIX B Operator Precedence 893 APPENDIX C Character Sets 897 APPENDIX D Additional Java Topics 901 APPENDIX E Answers to Odd-Numbered Exercises 949 INDEX 973 B RIEF C ONTENTS Apago PDF Enhancer Preface xix AN OVERVIEW OF COMPUTERS AND PROGRAMMING LANGUAGES 1 Introduction 2 An Overview of the History of Computers 2 Elements of a Computer System 3 Hardware 4 Software 6 Language of a Computer 6 Evolution of Programming Languages 8 Processing a Java Program 10 Internet, World Wide Web, Browser, and Java 13 Programming with the Problem Analysis–Coding– Execution Cycle 13 Programming Methodologies 19 Structured Programming 19 Object-Oriented Programming 19 Quick Review 21 Exercises 23 BASIC ELEMENTS OF JAVA 25 A Java Program 26 Basics of a Java Program 28 Comments 29 Special Symbols 30 Reserved Words (Keywords) 30 Identifiers 31 1 2 T ABLE OF C ONTENTS Apago PDF Enhancer Data Types 32 Primitive Data Types 32 Arithmetic Operators and Operator Precedence 36 Order of Precedence 39 Expressions 40 Mixed Expressions 41 Type Conversion (Casting) 43 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 Increment and Decrement Operators 63 Output 66 Packages, Classes, Methods, and the import Statement 71 Creating a Java Application Program 72 Programming Style and Form 77 Syntax 77 Avoiding Bugs: Consistent, Proper Formatting 81 Debugging—Code Walk-throughs 81 More on Assignment Statements (Optional) 82 Programming Example: Convert Length 84 Programming Example: Make Change 88 Debugging: Understanding Error Messages 91 Quick Review 91 Exercises 94 Programming Exercises 102 Table of Contents | vii Apago PDF Enhancer INTRODUCTION TO OBJECTS AND INPUT/OUTPUT 107 Objects and Reference Variables 108 Using Predefined Classes and Methods in a Program 112 Dot Between Class (Object) Name and Class Member: A Precaution 114 class String 115 Input/Output 123 Formatting Output with printf 123 Using Dialog Boxes for Input/Output 132 Formatting the Output Using the String Method format 139 File Input/Output 142 Storing (Writing) Output to a File 145 Programming Example: Movie Ticket Sale and Donation to Charity 147 Programming Example: Student Grade 153 Debugging—Understanding Error Messages 156 Quick Review 156 Exercises 158 Programming Exercises 161 CONTROL STRUCTURES I: SELECTION 165 Control Structures 166 Relational Operators 168 Relational Operators and Primitive Data Types 169 Logical (Boolean) Operators and Logical Expressions 172 Order of Precedence 174 Short-Circuit Evaluation 178 boolean Data Type and Logical (Boolean) Expressions 179 3 4 viii | Java Programming: From Problem Analysis to Program Design, Fourth Edition Apago PDF Enhancer Selection: if and if .else 180 One-Way Selection 180 Two-Way Selection 183 Compound (Block of) Statements 187 Multiple Selections: Nested if 188 Comparing if .else Statements with a Series of if Statements 191 Conditional Operator (? :) (Optional) 192 switch Structures 193 Avoiding Bugs by Avoiding Partially Understood Concepts and Techniques 199 Programming Example: Cable Company Billing 201 Comparing Strings 207 Strings, the Assignment Operator, and the Operator new 211 Quick Review 213 Exercises 215 Programming Exercises 221 CONTROL STRUCTURES II: REPETITION 225 Why Is Repetition Needed? 226 while Looping (Repetition) Structure 227 Designing while Loops 230 Counter-Controlled while Loops 231 Sentinel-Controlled while Loops 233 Flag-Controlled While Loops 239 EOF-Controlled while Loops 243 More on Expressions in while Statements 247 Programming Example: Fibonacci Number 248 for Looping (Repetition) Structure 254 Programming Example: Classify Numbers 259 do .while Looping (Repetition) Structure 263 Choosing the Right Looping Structure 268 5 Table of Contents | ix . Packages and User-Defined Classes 924 PrimitiveType Classes 924 Class: IntClass 924 Class: LongClass 928 Class: CharClass 929 Class: FloatClass 929 Class: DoubleClass. Exercises 346 Programming Exercises 349 USER-DEFINED METHODS 351 Predefined Methods 352 Using Predefined Methods in a Program 356 User-Defined Methods 359