Objects First with Java
Michael Kolling, David J. Barnes
Objects First with Java
Michael Kolling, David J. Barnes
- Producent: Pearson
- Rok produkcji: 2011
- ISBN: 9780132835541
- Ilość stron: 560
- Oprawa: brak formatu
Niedostępna
Opis: Objects First with Java - Michael Kolling, David J. Barnes
Objects First with Java: A Practical Introduction Using BlueJ, 5e, is ideal for introductory courses in Java/Introduction to Programming and Object-Oriented Programming and for beginning programmers. This is the only introductory programming textbook that uses the BlueJ integrated development environment (IDE) to teach introductory and object-oriented programming principles using Java. Its close integration with the BlueJ development environment allows this book to focus on key aspects of object-oriented software development from day one. BlueJ's clear visualization of classes and objects means that readers can immediately appreciate the differences between them, and gain a much better understanding of the nature of an object than they would from simply reading source code. Unlike traditional textbooks, the chapters are not ordered by language features but by software development concepts. Language features are introduced as a response to the problems to be solved. A large number of different, interesting projects are used to provide variety and avoid the monotony of a running problem. This book takes an "objects first" approach to teaching the traditionally difficult concepts of objects in a manipulative visual form. Throughout, the emphasis is on developing a practical approach to programming, with students encouraged to add code to existing programs rather than working with an intimidating sheet of blank paper. This textbook is printed in four-color to aid pedagogy and reader learning.Foreword xiii Preface xiv List of projects discussed in detail in this book xxiii Acknowledgments xxv Part 1 Foundations of object orientation 1 Chapter 1 Objects and classes 3 1.1 Objects and classes 3 1.2 Creating objects 4 1.3 Calling methods 5 1.4 Parameters 6 1.5 Data types 7 1.6 Multiple instances 8 1.7 State 8 1.8 What is in an object? 9 1.9 Java code 10 1.10 Object interaction 12 1.11 Source code 12 1.12 Another example 14 1.13 Return values 14 1.14 Objects as parameters 14 1.15 Summary 16 Chapter 2 Understanding class definitions 18 2.1 Ticket machines 18 2.1.1 Exploring the behavior of a naive ticket machine 19 2.2 Examining a class definition 20 2.3 The class header 22 2.3.1 Keywords 23 2.4 Fields, constructors, and methods 23 2.4.1 Fields 24 2.4.2 Constructors 27 2.5 Parameters: receiving data 28 2.5.1 Choosing variable names 30 2.6 Assignment 30 2.7 Methods 31 2.8 Accessor and mutator methods 33 2.9 Printing from methods 36 2.10 Method summary 38 2.11 Summary of the naive ticket machine 38 2.12 Reflecting on the design of the ticket machine 39 2.13 Making choices: the conditional statement 42 2.14 A further conditional-statement example 44 2.15 Scope highlighting 45 2.16 Local variables 46 2.17 Fields, parameters, and local variables 48 2.18 Summary of the better ticket machine 49 2.19 Self-review exercises 50 2.20 Reviewing a familiar example 51 2.21 Calling methods 54 2.22 Experimenting with expressions: the Code Pad 55 2.23 Summary 58 Chapter 3 Object interaction 62 3.1 The clock example 62 3.2 Abstraction and modularization 63 3.3 Abstraction in software 64 3.4 Modularization in the clock example 64 3.5 Implementing the clock display 65 3.6 Class diagrams versus object diagrams 66 3.7 Primitive types and object types 67 3.8 The ClockDisplay source code 67 3.8.1 Class NumberDisplay 68 3.8.2 String concatenation 72 3.8.3 The modulo operator 73 3.8.4 Class ClockDisplay 73 3.9 Objects creating objects 77 3.10 Multiple constructors 78 3.11 Method calls 79 3.11.1 Internal method calls 79 3.11.2 External method calls 79 3.11.3 Summary of the clock display 81 3.12 Another example of object interaction 81 3.12.1 The mail-system example 82 3.12.2 The this keyword 83 3.13 Using a debugger 85 3.13.1 Setting breakpoints 85 3.13.2 Single stepping 87 3.13.3 Stepping into methods 88 3.14 Method calling revisited 88 3.15 Summary 89 Chapter 4 Grouping objects 92 4.1 Building on themes from Chapter 3 92 4.2 The collection abstraction 93 4.3 An organizer for music files 94 4.4 Using a library class 95 4.4.1 Importing a library class 97 4.4.2 Diamond notation 98 4.4.3 Key methods of ArrayList 98 4.5 Object structures with collections 98 4.6 Generic classes 100 4.7 Numbering within collections 101 4.7.1 The effect of removal on numbering 102 4.7.2 The general utility of numbering with collections 103 4.8 Playing the music files 104 4.8.1 Summary of the music organizer 106 4.9 Processing a whole collection 106 4.9.1 The for-each loop 107 4.9.2 Selective processing of a collection 109 4.9.3 A limitation of using strings 111 4.9.4 Summary of the for-each loop 111 4.10 Indefinite iteration 112 4.10.1 The while loop 112 4.10.2 Iterating with an index variable 114 4.10.3 Searching a collection 115 4.10.4 Some non-collection examples 118 4.11 Improving structure-the Track class 119 4.12 The Iterator type 122 4.12.1 Index access versus iterators 124 4.12.2 Removing elements 125 4.13 Summary of the music-organizer project 126 4.14 Another example: An auction system 128 4.14.1 Getting started with the project 129 4.14.2 The null keyword 130 4.14.3 The Lot class 130 4.14.4 The Auction class 131 4.14.5 Anonymous objects 134 4.14.6 Chaining method calls 135 4.14.7 Using collections 136 4.15 Flexible-collection summary 138 4.16 Fixed-size collections 139 4.16.1 A log-file analyzer 139 4.16.2 Declaring array variables 142 4.16.3 Creating array objects 142 4.16.4 Using array objects 144 4.16.5 Analyzing the log file 144 4.16.6 The for loop 145 4.16.7 Arrays and the for-each loop 147 4.16.8 The for loop and iterators 148 4.17 Summary 150 Chapter 5 More-sophisticated behavior 153 5.1 Documentation for library classes 154 5.2 The TechSupport system 155 5.2.1 Exploring the TechSupport system 155 5.2.2 Reading the code 157 5.3 Reading class documentation 160 5.3.1 Interfaces versus implementation 162 5.3.2 Using library-class methods 163 5.3.3 Checking string equality 165 5.4 Adding random behavior 166 5.4.1 The Random class 166 5.4.2 Random numbers with limited range 167 5.4.3 Generating random responses 168 5.4.4 Reading documentation for parameterized classes 171 5.5 Packages and import 171 5.6 Using maps for associations 172 5.6.1 The concept of a map 173 5.6.2 Using a HashMap 173 5.6.3 Using a map for the TechSupport system 175 5.7 Using sets 177 5.8 Dividing strings 178 5.9 Finishing the TechSupport system 179 5.10 Writing class documentation 181 5.10.1 Using javadoc in BlueJ 182 5.10.2 Elements of class documentation 182 5.11 Public versus private 183 5.11.1 Information hiding 184 5.11.2 Private methods and public fields 185 5.12 Learning about classes from their interfaces 186 5.12.1 The scribble demo 186 5.12.2 Code completion 189 5.12.3 The bouncing-balls demo 190 5.13 Class variables and constants 190 5.13.1 The static keyword 191 5.13.2 Constants 192 5.14 Summary 193 Chapter 6 Designing classes 196 6.1 Introduction 197 6.2 The world-of-zuul game example 198 6.3 Introduction to coupling and cohesion 200 6.4 Code duplication 6.5 Making extensions 204 6.5.1 The task 205 6.5.2 Finding the relevant source code 205 6.6 Coupling 207 6.6.1 Using encapsulation to reduce coupling 207 6.7 Responsibility-driven design 212 6.7.1 Responsibilities and coupling 212 6.8 Localizing change 214 6.9 Implicit coupling 215 6.10 Thinking ahead 218 6.11 Cohesion 219 6.11.1 Cohesion of methods 219 6.11.2 Cohesion of classes 220 6.11.3 Cohesion for readability 221 6.11.4 Cohesion for reuse 221 6.12 Refactoring 222 6.12.1 Refactoring and testing 223 6.12.2 An example of refactoring 223 6.13 Refactoring for language independence 226 6.13.1 Enumerated types 227 6.13.2 Further decoupling of the command interface 229 6.14 Design guidelines 231 6.15 Executing without BlueJ 232 6.15.1 Class methods 232 6.15.2 The main method 233 6.15.3 Limitations in class methods 234 6.16 Summary 234 Chapter 7 Well-behaved objects 236 7.1 Introduction 236 7.2 Testing and debugging 237 7.3 Unit testing within BlueJ 237 7.3.1 Using inspectors 243 7.3.2 Positive versus negative testing 245 7.4 Test automation 245 7.4.1 Regression testing 245 7.4.2 Automated testing using JUnit 246 7.4.3 Recording a test 248 7.4.4 Fixtures 251 7.5 Debugging 252 7.6 Commenting and style 254 7.7 Manual walkthroughs 255 7.7.1 A high-level walkthrough 255 7.7.2 Checking state with a walkthrough 257 7.7.3 Verbal walkthroughs 260 7.8 Print statements 260 7.8.1 Turning debugging information on or off 262 7.9 Debuggers 263 7.10 Choosing a debugging strategy 265 7.11 Putting the techniques into practice 265 7.12 Summary 265 Part 2 Application structures 267 Chapter 8 Improving structure with inheritance 269 8.1 The network example 269 8.1.1 The network project: classes and objects 270 8.1.2 Network source code 273 8.1.3 Discussion of the network application 282 8.2 Using inheritance 282 8.3 Inheritance hierarchies 284 8.4 Inheritance in Java 285 8.4.1 Inheritance and access rights 286 8.4.2 Inheritance and initialization 286 8.5 Network: adding other post types 288 8.6 Advantages of inheritance (so far) 290 8.7 Subtyping 291 8.7.1 Subclasses and subtypes 293 8.7.2 Subtyping and assignment 293 8.7.3 Subtyping and parameter passing 295 8.7.4 Polymorphic variables 295 8.7.5 Casting 296 8.8 The Object class 297 8.9 Autoboxing and wrapper classes 298 8.10 The collection hierarchy 299 8.11 Summary 299 Chapter 9 More about inheritance 302 9.1 The problem: network's display method 302 9.2 Static type and dynamic type 304 9.2.1 Calling display from NewsFeed 305 9.3 Overriding 307 9.4 Dynamic method lookup 309 9.5 Super call in methods 311 9.6 Method polymorphism 313 9.7 Object methods: toString 313 9.8 Object equality: equals and hashCode 316 9.9 Protected access 318 9.10 The instanceof operator 320 9.11 Another example of inheritance with overriding 321 9.12 Summary 323 Chapter 10 Further abstraction techniques 326 10.1 Simulations 326 10.2 The foxes-and-rabbits simulation 327 10.2.1 The foxes-and-rabbits project 328 10.2.2 The Rabbit class 331 10.2.3 The Fox class 334 10.2.4 The Simulator class: setup 337 10.2.5 The Simulator class: a simulation step 341 10.2.6 Taking steps to improve the simulation 342 10.3 Abstract classes 342 10.3.1 The Animal superclass 343 10.3.2 Abstract methods 344 10.3.3 Abstract classes 346 10.4 More abstract methods 348 10.5 Multiple inheritance 351 10.5.1 An Actor class 351 10.5.2 Flexibility through abstraction 353 10.5.3 Selective drawing 353 10.5.4 Drawable actors: multiple inheritance 354 10.6 Interfaces 354 10.6.1 An Actor interface 355 10.6.2 Multiple inheritance of interfaces 356 10.6.3 Interfaces as types 357 10.6.4 Interfaces as specifications 358 10.6.5 Library support through abstract classes and interfaces 359 10.7 A further example of interfaces 359 10.8 The Class class 361 10.9 Abstract class or interface? 362 10.10 Event-driven simulations 362 10.11 Summary of inheritance 363 10.12 Summary 364 Chapter 11 Building graphical user interfaces 367 11.1 Introduction 367 11.2 Components, layout, and event handling 368 11.3 AWT and Swing 368 11.4 The ImageViewer example 369 11.4.1 First experiments: creating a frame 369 11.4.2 Adding simple components 372 11.4.3 An alternative structure 373 11.4.4 Adding menus 374 11.4.5 Event handling 375 11.4.6 Centralized receipt of events 376 11.4.7 Inner classes 378 11.4.8 Anonymous inner classes 380 11.4.9 Summary of key GUI elements 382 11.5 ImageViewer 1.0: the first complete version 383 11.5.1 Image-processing classes 383 11.5.2 Adding the image 384 11.5.3 Layout 386 11.5.4 Nested containers 389 11.5.5 Image filters 391 11.5.6 Dialogs 394 11.5.7 Summary of layout management 396 11.6 ImageViewer 2.0: improving program structure 396 11.7 ImageViewer 3.0: more interface components 402 11.7.1 Buttons 402 11.7.2 Borders 405 11.8 Further extensions 406 11.9 Another example: MusicPlayer 408 11.10 Summary 411 Chapter 12 Handling errors 413 12.1 The address-book project 414 12.2 Defensive programming 418 12.2.1 Client-server interaction 418 12.2.2 Parameter checking 420 12.3 Server-error reporting 421 12.3.1 Notifying the user 422 12.3.2 Notifying the client object 422 12.4 Exception-throwing principles 425 12.4.1 Throwing an exception 426 12.4.2 Checked and unchecked exceptions 426 12.4.3 The effect of an exception 428 12.4.4 Using unchecked exceptions 429 12.4.5 Preventing object creation 430 12.5 Exception handling 431 12.5.1 Checked exceptions: the throws clause 432 12.5.2 Anticipating exceptions: the try statement 432 12.5.3 Throwing and catching multiple exceptions 434 12.5.4 Multi-catch Java 7 436 12.5.5 Propagating an exception 436 12.5.6 The finally clause 437 12.6 Defining new exception classes 438 12.7 Using assertions 440 12.7.1 Internal consistency checks 440 12.7.2 The assert statement 440 12.7.3 Guidelines for using assertions 442 12.7.4 Assertions and the BlueJ unit testing framework 443 12.8 Error recovery and avoidance 443 12.8.1 Error recovery 443 12.8.2 Error avoidance 445 12.9 File-based input/output 446 12.9.1 Readers, writers, and streams 447 12.9.2 The File class and Path interface 447 12.9.3 File output 448 12.9.4 The try-with-resource statement 450 12.9.5 Text input 452 12.9.6 Scanner: parsing input 455 12.9.7 Object serialization 457 12.10 Summary 458 Chapter 13 Designing applications 460 13.1 Analysis and design 460 13.1.1 The verb/noun method 461 13.1.2 The cinema booking example 461 13.1.3 Discovering classes 461 13.1.4 Using CRC cards 463 13.1.5 Scenarios 463 13.2 Class design 467 13.2.1 Designing class interfaces 467 13.2.2 User interface design 468 13.3 Documentation 469 13.4 Cooperation 469 13.5 Prototyping 470 13.6 Software growth 470 13.6.1 Waterfall model 471 13.6.2 Iterative development 471 13.7 Using design patterns 472 13.7.1 Structure of a pattern 473 13.7.2 Decorator 474 13.7.3 Singleton 474 13.7.4 Factory method 475 13.7.5 Observer 476 13.7.6 Pattern summary 477 13.8 Summary 478 Chapter 14 A case study 480 14.1 The case study 480 14.1.1 The problem description 480 14.2 Analysis and design 481 14.2.1 Discovering classes 481 14.2.2 Using CRC cards 482 14.2.3 Scenarios 483 14.3 Class design 485 14.3.1 Designing class interfaces 485 14.3.2 Collaborators 485 14.3.3 The outline implementation 486 14.3.4 Testing 490 14.3.5 Some remaining issues 490 14.4 Iterative development 491 14.4.1 Development steps 491 14.4.2 A first stage 492 14.4.3 Testing the first stage 496 14.4.4 A later stage of development 496 14.4.5 Further ideas for development 498 14.4.6 Reuse 499 14.5 Another example 499 14.6 Taking things further 499 Appendices A Working with a BlueJ project 500 B Java data types 503 C Operators 507 D Java control structures 510 E Running Java without BlueJ 517 F Using the debugger 520 G Unit unit-testing tools 524 H Teamwork tools 526 I Javadoc 528 J Program style guide 531 K Important library classes 535 Index 539
Szczegóły: Objects First with Java - Michael Kolling, David J. Barnes
Tytuł: Objects First with Java
Autor: Michael Kolling, David J. Barnes
Producent: Pearson
ISBN: 9780132835541
Rok produkcji: 2011
Ilość stron: 560
Oprawa: brak formatu
Waga: 0.8 kg