Foundations of object orientation

Similar documents
Objects First with Java

COURSE OVERVIEW. Successful completion of this course may provide one semester of college credit for computer science.

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

JAVA CONCEPTS Early Objects

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

WA1278 Introduction to Java Using Eclipse

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

C ONTENTS PART I FUNDAMENTALS OF PROGRAMMING 1. and Java 3. Chapter 1 Introduction to Computers, Programs,

Programming in Scala Second Edition

Supporting Materials

"Charting the Course... Java Programming Language. Course Summary

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Murach s Beginning Java with Eclipse

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Contents. iii Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1998, Revision B

This page intentionally left blank

Building Java Programs

Oracle 10g: Java Programming

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

Big Java Late Objects

Java 8 Programming for OO Experienced Developers

Core Java Contents. Duration: 25 Hours (1 Month)

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

JAVA. Duration: 2 Months

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

OracleAS 10g R3: Java Programming

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

LTBP INDUSTRIAL TRAINING INSTITUTE

Chapter 5 Object-Oriented Programming

Introduction to Programming Using Java (98-388)

Designing applications. Main concepts to be covered

MyProgram m i ng Lab. get with the programming. Through the power of practice and immediate personalized

Contents Chapter 1 Introduction to Programming and the Java Language

E Oracle 10g Java Bootcamp I v1

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Programming II (CS300)

Program Correctness and Efficiency. Chapter 2

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10

Syllabus- Java + Android. Java Fundamentals

ощ 'ршорвшэш! цвн-эориэу ощ 'sajbpossv # PIPG DUJ 'ssjmoossv ^ PIPG pipa w н OX ЛЮН VAV

Java Programming. Price $ (inc GST)

Compaq Interview Questions And Answers

Java SE7 Fundamentals

Programming II (CS300)

Agile Model-Driven Development with UML 2.0 SCOTT W. AM BLER. Foreword by Randy Miller UNIFIED 1420 MODELING LANGUAGE. gile 1.

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

CO Java SE 8: Fundamentals

Study Guide to Exam 2

[Course Overview] After completing this module you are ready to: Develop Desktop applications, Networking & Multi-threaded programs in java.

104. Intermediate Java Programming

Objects First with Java A Practical Introduction using BlueJ

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Java Programming Training for Experienced Programmers (5 Days)

Java SE 8 Programming

Logistics. Final Exam on Friday at 3pm in CHEM 102

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

Introduction. Assessment Test. Part I The Programmer s Exam 1

Java SE 8 Programming

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

1 OBJECT-ORIENTED PROGRAMMING 1

CHAPTER 1: INTRODUCING C# 3

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Core Java Syllabus. Pre-requisite / Target Audience: C language skills (Good to Have)

Glossary. Appendix A. Key Terms

Complete Java Contents

Java Programming with Eclipse

Type of Classes Nested Classes Inner Classes Local and Anonymous Inner Classes

Java SE 8 Programmer I and II Syballus( Paper codes : 1z0-808 & 1z0-809)

CHAPTER 1: A GENERAL INTRODUCTION TO PROGRAMMING 1

COURSE DETAILS: CORE AND ADVANCE JAVA Core Java

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Peers Techno log ies Pv t. L td. Core Java & Core Java &Adv Adv Java Java

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Mind Q Systems Private Limited

Internet Application Developer

Core Java SYLLABUS COVERAGE SYLLABUS IN DETAILS

Page 1

CHAPTER 9 DESIGN ENGINEERING. Overview

Standard. Number of Correlations

Top Down Design vs. Modularization

Java 1.8 Programming

Java SE 8 Programming

DESIGN PATTERN - INTERVIEW QUESTIONS

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Absolute C++ Walter Savitch

JAVA. 1. Introduction to JAVA

This page intentionally left blank

Review sheet for Final Exam (List of objectives for this course)

Index. Animation. factors, 186. Abstract Window Toolkit (AWT), 111. frame rate, 185. Access modifiers. multiple objects. package-private, 27

Objects First with Java A Practical Introduction using BlueJ

Overview of Eclipse Lectures. Module Road Map

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

CSE 70 Final Exam Fall 2009

Java SE 7 Programming

Transcription:

Foreword Preface List of projects discussed in detail in this book Acknowledgments Part 1 Chapter 1 Chapter 2 Foundations of object orientation Objects and classes 1.1 Objects and classes 1.2 Creating objects 1.3 Calling methods 1.4 Parameters 1.5 Data types 1.6 Multiple instances 1.7 State 1.8 What is in an object? 1.9 Java code 1.10 Object interaction 1.11 Source code 1.12 Another example 1.13 Return values 1.14 Objects as parameters 1.15 Summary Understanding class definitions 2.1 Ticket machines 2.1.1 Exploring the behavior of a naive ticket machir xiii xiv xxiii xxv 1 3 3 4 5 6 7 8 8 9 10 12 12 14 14 14 16 18 18 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 Cl ockdi spl ay source code 67 3.8.1 Class NumberDi spl ay 68 3.8.2 String concatenation 72 3.8.3 The modulo operator 73 3.8.4 Class Cl ockdi spl ay 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 th is 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 ArrayLi st 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 4.9.3 A limitation of using strings 109 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

Chapter 5 4.14 Another example: An auction system 128 4.14.1 Getting started with the project 129 4.14.2 The n u ll keyword 130 4.14.3 The Lot class 130 4.14.4 The A ucti on 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 4.16.7 Arrays and the for-each loop 145 147 4.16.8 The for loop and iterators 148 4.17 Summary 150 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 5.3.1 Interfaces versus implementation 160 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 s ta tic 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 201

6.5 Making extensions 204 7.3 Unit testing within BlueJ 237 6.5.1 The task 205 7.3,1 Using inspectors 243 6.5.2 Finding the relevant 7.3,2 Positive versus negative source code 205 testing 245 6.6 Coupling 6,6.1 Using encapsulation to 207 7.4 Test automation 7.4.1 Regression testing 245 245 reduce coupling 207 7.4.2 Automated testing using 6.7 Responsibility-driven JUnit 246 design 212 7.4.3 Recording a test 248 6.7.1 Responsibilities and 7.4.4 Fixtures 251 coupling 212 7.5 Debugging 252 6.8 Localizing change 214 7.6 Commenting and style 254 6.9 Implicit coupling 215 7.7 Manual walkthroughs 255 6.10 Thinking ahead 218 7.7.1 A high-level 6.11 Cohesion 219 walkthrough 255 6.11.1 Cohesion of methods 219 7.7.2 Checking state with a 6.11.2 Cohesion of classes 220 walkthrough 257 6.11.3 Cohesion for 7.7.3 Verbal walkthroughs 260 readability 221 7.8 Print statements 260 6.11.4 Cohesion for reuse 221 7.8.1 Turning debugging 6.12 Refactoring 222 information on or off 262 6.12.1 Refactoring and 7.9 Debuggers 263 testing 223 7.10 Choosing a debugging 6.12.2 An example of strategy 265 refactoring 223 7.11 Putting the techniques 6.13 Refactoring for language into practice 265 independence 226 7.12 Summary 265 6.13.1 Enumerated types 227 6.13.2 Further decoupling of the command Part 2 Application structures 267 interface 229 6.14 Design guidelines 231 Chapter 8 Improving structure with 6.15 Executing without BlueJ 232 inheritance 269 6.15.1 Class methods 232 6.15.2 The main method 233 8.1 The network example 269 6.15.3 Limitations in class methods 234 6.16 Summary 234 8.1.1 The network project: classes and objects 270 8.1.2 Network source code 8.1.3 Discussion of the Chapter 7 Well-behaved objects 236 network application 282 7.1 introduction 236 8.2 Using inheritance 282 7.2 Testing and debugging 237 8.3 Inheritance hierarchies 284 273

8.4 Inheritance in Java 285 Chapter 10 Further abstraction 8.4.1 Inheritance and access techniques 326 rights 286 10.1 Simulations 326 8.4.2 Inheritance and 10.2 The foxes-and-rabbits initialization 286 simulation 327 8.5 Network: adding other post 10.2.1 The foxes-and-rabbits types 288 project 328 8.6 Advantages of inheritance 10.2.2 The Rabbit class 331 (so far) 290 10.2.3 The Fox class 334 8.7 Subtyping 291 10.2.4 The Si mul a to r class: 8.7.1 Subclasses and setup 337 subtypes 293 10,2.5 The Si mul a to r class: 8.7.2 Subtyping and a simulation step 341 assignment 293 10.2.6 Taking steps to 8.7.3 Subtyping and improve the parameter passing 295 simulation 342 8.7.4 Polymorphic variables 295 10.3 Abstract classes 342 8.7.5 Casting 296 10.3.1 The Animal 8.8 The O bject class 297 superclass 343 8.9 Autoboxing and wrapper 10.3.2 Abstract methods 344 classes 298 10.3.3 Abstract classes 346 8.10 The collection hierarchy 299 10.4 More abstract methods 348 8.11 Summary 299 10.5 Multiple inheritance 351 10.5.1 An A ctor class 351 Chapter 9 More about inheritance 302 10.5.2 Flexibility through 9.1 The problem: network s display abstraction 353 method 302 10.5.3 Selective drawing 353 9.2 Static type and dynamic type 304 10.5.4 Drawable actors: 9.2.1 Calling di spl ay from multiple inheritance 354 NewsFeed 305 10.6 Interfaces 354 9.3 Overriding 307 10.6.1 An A ctor interface 355 9.4 Dynamic method lookup 309 10.6.2 Multiple inheritance of 9.5 Super call in methods 311 interfaces 356 9.6 Method polymorphism 313 10.6.3 Interfaces as types 357 9.7 Object methods: to S trin g 313 10.6.4 Interfaces as specifications 358 9.8 Object equality: equal s and hashcode 316 10.6.5 Library support through abstract 9.9 Protected access 318 classes and 9.10 The i nstanceof operator 320 interfaces 359 9.11 Another example of 10.7 A further example of inheritance with overriding 321 interfaces 359 9.12 Summary 323 10.8 The Class class 361

Chapter 11 10.9 Abstract class or interface? 362 10.10 Event-driven simulations 362 10.11 Summary of inheritance 363 10.12 Summary 364 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 11.4.8 Anonymous inner classes 378 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 12.4.1 Throwing an exception 12.4.2 Checked and unchecked exceptions 12.4.3 The effect of an exception 12.4.4 Using unchecked exceptions 425 426 426 428 429 12.4.5 Preventing object creation 430 12.5 Exception handling 12.5.1 Checked exceptions: the throws clause 431 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 12.7.3 Guidelines for using assertions 440 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 12.9.1 Readers, writers, and streams 446 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 13.1.1 The verb/noun method 460 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