Objects First with Java

Similar documents
Foundations of object orientation

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

Programming. In Ada JOHN BARNES TT ADDISON-WESLEY

Real-Time Systems and Programming Languages

Data Structures and Abstractions with Java

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

JAVA CONCEPTS Early Objects

PROBLEM SOLVING USING JAVA WITH DATA STRUCTURES. A Multimedia Approach. Mark Guzdial and Barbara Ericson PEARSON. College of Computing

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

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

CLASSIC DATA STRUCTURES IN JAVA

ony Gaddis Haywood Community College STARTING OUT WITH PEARSON Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto

Visual C# Tony Gaddis. Haywood Community College STARTING OUT WITH. Piyali Sengupta. Third Edition. Global Edition contributions by.

DATA ABSTRACTION AND PROBLEM SOLVING WITH JAVA

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

An Introduction to Object-Oriented Programming

Database Concepts. David M. Kroenke UNIVERSITATSBIBLIOTHEK HANNOVER

Oracle 10g: Java Programming

Objects First with Java A Practical Introduction using BlueJ

Quality Code. Software Testing Principles, Practices, and Patterns. Stephen Vance. AAddison-Wesley

Programming in Python 3

Prelude to Programming

DATA STRUCTURES AND PROBLEM SOLVING USING JAVA

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

OracleAS 10g R3: Java Programming

E Oracle 10g Java Bootcamp I v1

Objects First with Java A Practical Introduction using BlueJ

World Wide Web PROGRAMMING THE PEARSON EIGHTH EDITION. University of Colorado at Colorado Springs

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

Fit for Developing Software

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

WA1278 Introduction to Java Using Eclipse

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

Designing applications. Main concepts to be covered

Contributor. International Edition contributions by. Piyali Sengupta PEARSON. New York San Francisco Upper

Chapter 5 Object-Oriented Programming

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

Access ComprehGnsiwG. Shelley Gaskin, Carolyn McLellan, and. Nancy Graviett. with Microsoft

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

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

CO Java SE 8: Fundamentals

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

1 OBJECT-ORIENTED PROGRAMMING 1

SQL Queries. for. Mere Mortals. Third Edition. A Hands-On Guide to Data Manipulation in SQL. John L. Viescas Michael J. Hernandez

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

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

MariaDB Crash Course. A Addison-Wesley. Ben Forta. Upper Saddle River, NJ Boston. Indianapolis. Singapore Mexico City. Cape Town Sydney.

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

Programming in Scala Second Edition

Supporting Materials

PYTHON. p ykos vtawynivis. Second eciitiovl. CO Ve, WESLEY J. CHUN

The Java Tutorial. A Short Course on the Basics. Raymond Gallardo. Sowmya Kannan. AAddison-Wesley. Sharon Biocca Zakhour.

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

Essentials of Database Management

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

Business Driven Data Communications

Murach s Beginning Java with Eclipse

Compaq Interview Questions And Answers

JAVA. Duration: 2 Months

Big Java Late Objects

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

^l^s^^^^^^^^^^s^^^ ^.1^L^ gs *^gs (s^s^^^^s^^ ^S^^^^ls

This page intentionally left blank

Data Structures and Abstractions with Java

Programming II (CS300)

Agile Principles, Patterns, and Practices in C#

Programming II (CS300)

Internet Application Developer

The Unified Modeling Language User Guide

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Complete Java Contents

The Power of Events. An Introduction to Complex Event Processing in Distributed Enterprise Systems. David Luckham

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

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

Building Java Programs

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

DATA AND COMPUTER COMMUNICATIONS

Contents Chapter 1 Introduction to Programming and the Java Language

CJT^jL rafting Cm ompiler

Concepts 6. OOAD Part II. BlueJ ex 5.1. OO and inheritance. Intro to inheritance. The DoME project is a database for multimedia entertainment.

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

Preface. The Purpose of this Book and Its Audience. Coverage and Approach

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

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

Computers as Components Principles of Embedded Computing System Design

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

Search Engines Information Retrieval in Practice

Overview of Eclipse Lectures. Module Road Map

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

CompuScholar, Inc. 9th - 12th grades

Microsoft. Microsoft Visual C# Step by Step. John Sharp

MECHATRONICS. William Bolton. Sixth Edition ELECTRONIC CONTROL SYSTEMS ENGINEERING IN MECHANICAL AND ELECTRICAL PEARSON

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

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

Java Object Oriented Design. CSC207 Fall 2014

Java Programming. Price $ (inc GST)

JAVA. 1. Introduction to JAVA

Systems:;-'./'--'.; r. Ramez Elmasri Department of Computer Science and Engineering The University of Texas at Arlington

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

Transcription:

^ Objects First with Java A Practical Introduction using BlueJ David J. Barnes and Michael Kolling Second edition PEARSON Prentice Hall Harlow, England London New York Boston San Francisco Toronto Sydney Tokyo Singapore Hong Kong Seoul Taipei New Di Cape Town Madrid Mexico C ty Amsterdam Munich Paris Milan

Foreword Preface to the instructor List of projects discussed in detail in this book Acknowledgements Part 1 Chapter 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13 1.14 Chapter 2 2.1 2.2 2.3 2.4 Foundations of object orientation Objects and classes Objects and classes Creating objects Calling methods Parameters Data types Multiple instances State What is in an object? Object interaction Source code Another example Return values Objects as parameters Understanding class definitions Ticket machines 2.1.1 Exploring the behavior of a naive ticket machine Examining a class definition Fields, constructors, and methods 2.3.1 Fields 2.3.2 Constructors Passing data via parameters

viii Contents 2.5 Assignment 2.6 Accessor methods 2.7 Mutator methods 2.8 Printing from methods 2.9 of the naive ticket machine 2.10 Reflecting on the design of the ticket machine 2.11 Making choices: the conditional statement 2.12 A further conditional-statement example 2.13 Local variables 2.14 Fields, parameters, and local variables 2.15 of the better ticket machine 2.16 Reviewing a familiar example 2.17 Chapter 3 Object interaction 3.1 The clock example 3.2 Abstraction and modularization 3.3 Abstraction in software 3.4 Modularization in the clock example 3.5 Implementing the clock display 3.6 Class diagrams versus object diagrams 3.7 Primitive types and object types 3.8 The ClockDisplay source code 3.8.1 Class Number-Display 3.8.2 String concatenation 3.8.3 The modulo operator 3.8.4 Class ClockDisplay 3.9 Objects creating objects 3.10 Multiple constructors 3.11 Method calls 3.11.1 Internal method calls 3.11.2 External method calls 3.11.3 of the clock display 3.12 Another example of object interaction 3.12.1 The mail system example 3.12.2 The this keyword 3.13 Using a debugger 3.13.1 Setting breakpoints 3.13.2 Single stepping 3.13.3 Stepping into methods 3.14 Method calling revisited 3.15

Contents ix Chapter 4 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 4.12 Grouping objects Grouping objects in flexible-size collections A personal notebook A first look at library classes 4.3.1 An example of using a library Object structures with collections Numbering within collections Removing an item from a collection Processing a whole collection 4.7.1 The while loop 4.7.2 Iterating over a collection 4.7.3 Index access versus iterators of the notebook example Another example: an auction system 4.9.1 The Lot class 4.9.2 The Auction class 4.9.3 Casting 4.9.4 Anonymous objects 4.9.5 Using collections Flexible collection summary Fixed-size collections 4.11.1 A log-file analyzer 4.11.2 Declaring array variables 4.11.3 Creating array objects 4.11.4 Using array objects 4.11.5 Analyzing the log file 4.11.6 The for loop 79 79 80 80 81 82 84 85 86 87 89 90 91 92 92 93 96 96 97 99 100 100 102 103 104 105 106 110 Chapter 5 5.1 5.2 More sophisticated behavior Documentation for library classes The TechSuDDort svstem 5.2.1 Exploring the TechSupport system 5.2.2 Reading the code 5.3 Reading class documentation 5.3.1 Interfaces versus implementation 5.3.2 Using library-class methods 5.3.3 Checking string equality 5.4 Adding random behavior 5.4.1 The Random class 5.4.2 Random numbers with limited range 5.4.3 Generating random responses 112 113 113 114 115 119 120 121 123 124 124 125 126

Contents 5.5 Packages and import 5.6 Using maps for associations 5.6.1 The concept of a map 5.6.2 Using a HashMap 5.6.3 Using a map for the TechSupport system 5.7 Using sets 5.8 Dividing strings 5.9 Finishing the TechSupport system 5.10 Writing class documentation 5.10.1 Using javadoc in BlueJ 5.10.2 Elements of class documentation 5.11 Public versus private 5.11.1 Information hiding 5.11.2 Private methods and public fields Learning about classes from their interfaces Class variables and constants 5.13.1 The static key word 5.13.2 Constants pter6 6.1 6.2 6.3 6.4 Well-behaved objects Introduction Testing and debugging Unit testing within BlueJ 6.3.1 Using inspectors 6.3.2 Positive versus negative testing Test automation 6.4.1 Regression testing 6.4.2 Automated checking of test results 6.4.3 Recording a test 6.4.4 Fixtures 6.5 Modularization and interfaces 6.6 A debugging scenario 6.7 Commenting and style 6.8 Manual walkthroughs 6.8.1 A high-level walkthrough 6.8.2 Checking state with a walkthrough 6.8.3 Verbal walkthroughs 6.9 Print statements 6.9.1 Turning debugging information on or off Choosing a test strategy Debuggers 128 130 130 131 132 133 134 136 138 138 138 140 141 141 142 145 145 146 147 150 150 151 151 156 157 158 158 161 163 165 166 168 169 170 170 172 174 175 177 178 179

Contents xi 6.12 6.13 Putting the techniques into practice 179 180 Chapter 7 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 7.10 7.11 7.12 7.13 7.14 7.15 Designing classes Introduction The world-of-zuul game example Introduction to coupling and cohesion Code duplication Making extensions 7.5.1 The task 7.5.2 Finding the relevant source code Coupling 7.6.1 Using encapsulation to reduce coupling Responsibility-driven design 7.7.1 Responsibilities and coupling Localizing change Implicit coupling Thinking ahead Cohesion 7.11.1 Cohesion of methods 7.11.2 Cohesion of classes 7.11.3 Cohesion for readability 7.11.4 Cohesion for reuse Refactoring 7.12.1 Refactoring and testing 7.12.2 An example of refactoring Design guidelines Executing without BlueJ 7.14.1 Class methods 7.14.2 The main method 7.14.3 Limitations of class methods 181 182 183 185 186 189 189 189 191 191 195 196 198 198 201 202 202 203 204 205 206 206 207 209 211 211 211 212 212 Part 2 Application structures 215 Chapter 8 Improving structure with inheritance 8.1 The DoME example 8.1.1 DoME classes and objects 8.1.2 DoME source code 8.1.3 Discussion of the DoME application 8.2 Using inheritance 8.3 Inheritance hierarchies 217 217 218 220 226 227 228

ii Contents Chapter 9 Chapter 10 8.4 Inheritance in Java 8.4.1 Inheritance and access rights 8.4.2 Inheritance and initialization 8.5 DoME: adding other item types 8.6 Advantages of inheritance (so far) 8.7 Subtyping 8.7.1 Subclasses and subtypes 8.7.2 Subtyping and assignment 8.7.3 Subtyping and parameter passing 8.7.4 Polymorphic variables 8.8 The Object class 8.9 Polymorphic collections 8.9.1 Element types 8.9.2 Casting revisited 8.9.3 Wrapper classes 8.10 The collection hierarchy 8.11 More about inheritance 9.1 The problem: DoME's print method 9.2 Static type and dynamic type 9.2.1 Calling print from Database 9.3 Overriding 9.4 Dynamic method lookup 9.5 Super call in methods 9.6 Method polymorphism 9.7 Object methods: tostring 9.8 Protected access 9.9 Another example of inheritance with overriding 9.10 Further abstraction techniques 10.1 Simulations 10.2 The foxes-and-rabbits simulation 10.2.1 The foxes-and-rabbits project 10.2.2 The Rabbit class 10.2.3 The Fox class 10.2.4 The Simulator class: setup 10.2.5 The Simulator class: a simulation step 10.2.6 Taking steps to improve the simulation 10.3 Abstract classes 10.3.1 The Animal superclass 229 230 230 232 234 235 236 237 238 239 239 240 241 241 242 243 244 246 246 248 249 250 252 255 256 256 259 261 264 266 266 267 268 270 273 276 276 280 281 281

Contents xiii 10.3.2 Abstract methods 282 10.3.3 Abstract classes 284 10.4 More abstract methods 286 10.5 Multiple inheritance 288 10.5.1 An Actor class 288 10.5.2 Flexibility through abstraction 290 10.5.3 Selective drawing 290 10.5.4 Drawable actors: multiple inheritance 290 10.6 Interfaces 291 10.6.1 An Actor interface 291 10.6.2 Multiple inheritance of interfaces 293 10.6.3 Interfaces as types 294 10.6.4 Interfaces as specifications 294 10.6.5 A further example of interfaces 296 10.6.6 Abstract class or interface? 297 10.7 of inheritance 297 10.8 298 Chapter 11 Building graphical user interfaces 300 11.1 Introduction 300 11.2 Components, layout, and event handling 301 11.3 AWT and Swing 301 11.4 The ImageViewer example 302 11.4.1 First experiments: creating a frame 303 11.4.2 Adding simple components 305 11.4.3 Adding menus 306 11.4.4 Event handling 307 11.4.5 Centralized receipt of events 307 11.4.6 Inner classes 310 11.4.7 Anonymous inner classes 311 11.5 ImageViewer 1.0: the first complete version 313 11.5.1 Image-processing classes 314 11.5.2 Adding the image 315 11.5.3 Layout 316 11.5.4 Nested containers 319 11.5.5 Image filters 322 11.5.6 A Help menu 325 11.5.7 Dialogs 326 11.6 ImageViewer 2.0: improving program structure 327 11.7 ImageViewer 3.0: more interface components 332 11.7.1 Buttons 332

xiv Contents 11.7.2 Borders 335 11.8 Further extensions 336 11.9 Another example: SoundPlayer 338 11.10 341 Chapter 12 Handling errors 343 12.1 The address-book project 344 12.2 Defensive programming 348 12.2.1 Client-server interaction 348 12.2.2 Argument checking 349 12.3 Server error reporting 350 12.3.1 Notifying the user 351 12.3.2 Notifying the client object 351 12.4 Exception-throwing principles 354 12.4.1 Throwing an exception 355 12.4.2 Exception classes 355 12.4.3 The effect of an exception 357 12.4.4 Unchecked exceptions 358 12.4.5 Preventing object creation 359 12.5 Exception handling 360 12.5.1 Checked exceptions: the throws clause 360 12.5.2 Catching exceptions: the try statement 361 12.5.3 Throwing and catching multiple exceptions 363 12.5.4 Propagating an exception 364 12.5.5 The finally clause 365 12.6 Defining new exception classes 366 12.7 Using assertions 367 12.7.1 Internal consistency checks 367 12.7.2 The assert statement 368 12.7.3 Guidelines for using assertions 369 12.7.4 Assertions and the BlueJ unit testing framework 370 12.8 Error recovery and avoidance 371 12.8.1 Error recovery 371 12.8.2 Error avoidance 372 12.9 Case study: text input/output 373 12.9.1 Readers, writers, and streams 374 12.9.2 The address-book-io project 374 12.9.3 Text output with FileWriter 377 12.9.4 Text input with FileReader 378 12.9.5 Object serialization 379 12.10 380

Contents Chapter 13 Designing applications 13.1 Analysis and design 13.1.1 The verb/noun method 13.1.2 The cinema booking example 13.1.3 Discovering classes 13.1.4 Using CRC cards 13.1.5 Scenarios 13.2 Class design 13.2.1 Designing class interfaces 13.2.2 User interface design 13.3 Documentation 13.4 Cooperation 13.5 Prototyping 13.6 Software growth 13.6.1 Waterfall model 13.6.2 Iterative development 13.7 Using design patterns 13.7.1 Structure of a pattern 13.8 13.7.2 13.7.3 13.7.4 13.7.5 13.7.6 Decorator Singleton Factory method Observer Pattern summary Chapter 14 14.1 14.2 14.3 14.4 A case study The case study 14.1.1 The problem description Analysis and design 14.2.1 14.2.2 14.2.3 Class design 14.3.1 14.3.2 14.3.3 14.3.4 14.3.5 Discovering classes Using CRC cards Scenarios Designing class interfaces Collaborators The outline implementation Testing Some remaining issues Iterative development 14.4.1 Development steps 14.4.2 A first stage 14.4.3 Testing the first stage

xvi Contents 14.4.4 A later stage of development 14.4.5 Further ideas for development 14.4.6 Reuse 14.5 Another example 14.6 Taking things further 417 419 419 420 420 Appendices A B C D LJJ F G H I J K Working with a BlueJ project Java data types Java control structures Operators Running Java without BlueJ Configuring BlueJ Using the debugger JUnit unit-testing tools Javadoc Program style guide Important library classes 421 423 425 430 432 435 437 441 443 446 450 Index 455 Website resources For Students: Program style guide for all examples in the Links to further material of interest book For Lecturers: Powerpoint slides that can be downloaded and used as OHTs Solutions to exercises Additional activities, exercises and projects for use in teaching