Object-Oriented Programming and Data Structures

Similar documents
Object-Oriented Programming and Data Structures

AP Computer Science A Course Syllabus

This page intentionally left blank

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

Object-Oriented Programming and Data Structures

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

Big Java Late Objects

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

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

WITH C+ + William Ford University of the Pacific. William Topp University of the Pacific. Prentice Hall, Englewood Cliffs, New Jersey 07632

College Board. AP CS A Labs Magpie, Elevens, and Picture Lab. New York: College Entrance Examination Board, 2013.

Murach s Beginning Java with Eclipse

AP Computer Science 4325

IMACS: AP Computer Science A

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

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

AP Computer Science AB

1 OBJECT-ORIENTED PROGRAMMING 1

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.

Fundamentals of the Java Programming Language

OracleAS 10g R3: Java Programming

Samples of Evidence to Satisfy the AP Computer Science AB Curricular Requirements

Oracle 10g: Java Programming

Introduction to Programming Using Java (98-388)

E Oracle 10g Java Bootcamp I v1

Building Java Programs

3D Graphics Programming Mira Costa High School - Class Syllabus,

5/23/2015. Core Java Syllabus. VikRam ShaRma

Data Structures and Abstractions with Java

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

AP COMPUTER SCIENCE AB

CLASSIC DATA STRUCTURES IN JAVA

DATA ABSTRACTION AND PROBLEM SOLVING WITH JAVA

C# Programming: From Problem Analysis to Program Design. Fourth Edition

AP Computer Science A Syllabus

Computer Programming C++ (wg) CCOs

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

Programming in Scala Second Edition

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

Subject to Change Drawing Application 1 Introducing Computers, the Internet and C#


Curriculum Map Grade(s): Subject: AP Computer Science

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

Chapters are PDF documents posted online at the book s Companion Website (located at

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

JAVA CONCEPTS Early Objects

About this exam review

AP Computer Science A Skyline High School Mr. Coupland

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

[CHAPTER] 1 INTRODUCTION 1

Absolute C++ Walter Savitch

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Table of Contents. Preface... xxi

Contents Chapter 1 Introduction to Programming and the Java Language

TeenCoder : Java Programming (ISBN )

3 Welcome Application 41 Introduction to Visual Programming

Acknowledgments I INTRODUCTION 1

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Chapters and Appendix F are PDF documents posted online at the book s Companion Website (located at

C++ (Non for C Programmer) (BT307) 40 Hours

Pace University. Fundamental Concepts of CS121 1

CompuScholar, Inc. Alignment to Nevada "Computer Science" Course Standards

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

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

Microsoft Visual C# Step by Step. John Sharp

Before You Begin 1 Graphing Application 1 Introducing Computers, the Internet and Visual Basic.NET

182 review 1. Course Goals

APCS Semester #1 Final Exam Practice Problems

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

AP Computer Science AB

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Problem Solving with C++

CHAPTER 1: A GENERAL INTRODUCTION TO PROGRAMMING 1

CERTIFICATE IN WEB PROGRAMMING

CompuScholar, Inc. 9th - 12th grades

Welcome to Starting Out with Programming Logic and Design, Third Edition.

15CS45 : OBJECT ORIENTED CONCEPTS

CS 445: Data Structures Final Examination: Study Guide

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

Foundations of object orientation

JAVA. Duration: 2 Months

SRE VIDYASAAGAR HIGHER SECONDARY SCHOOL. TWO MARKS

School of Computing and Information Sciences. Course Title: Data Structures Date: 3/30/2010 Course Number: COP 3530 Number of Credits: 3

Objects First with Java

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

Java SE7 Fundamentals

Java Programming. Price $ (inc GST)

DATA STRUCTURES AND PROBLEM SOLVING USING JAVA

ENGINEERING PROBLEM SOLVING WITH C++

A Web-Based Introduction

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

WA1278 Introduction to Java Using Eclipse

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

HTML p. 1 Introduction to HTML p. 1 Getting Started p. 1 The Structure of an HTML Page p. 1 HTML Basics p. 3 The Weather Forecast Page p.

CHAPTER 1: INTRODUCING C# 3

Contents Introduction 1


Java Software Solutions for AP Computer Science 3rd Edition, Lewis et al. 2011

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Contents. Introduction

Transcription:

Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin Phillips Academy, Andover, Massachusetts Gary Litvin Skylight Software, Inc. Skylight Publishing Andover, Massachusetts

Skylight Publishing 9 Bartlet Street, Suite 70 Andover, MA 01810 web: e-mail: http://www.skylit.com sales@skylit.com support@skylit.com Copyright 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the authors and Skylight Publishing. Library of Congress Control Number: 2005910949 ISBN-10: 0-9727055-7-0 ISBN-13: 978-0-9727055-7-8 * AP and Advanced Placement are registered trademarks of The College Board, which was not involved in the production of and does not endorse this book. The names of commercially available software and products mentioned in this book are used for identification purposes only and may be trademarks or registered trademarks owned by corporations and other commercial entities. Skylight Publishing and the authors have no affiliation with and disclaim any sponsorship or endorsement by any of these products manufacturers or trademarks owners. Sun, Sun Microsystems, Java, and Java logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SCRABBLE is the registered trademark of HASBRO in the United States and Canada and of J.W. Spear and Sons, PLC, a subsidiary of Mattel, Inc., outside the United States and Canada. 1 2 3 4 5 6 7 8 9 10 11 10 09 08 07 06 Printed in the United States of America

To Marg and Aaron

Brief Contents Preface xvii How to Use This Book xxi Chapter 1 An Introduction to Hardware, Software, and the Internet 1 Chapter 2 An Introduction to Software Development 11 Chapter 3 Objects and Classes 43 Chapter 4 Algorithms 75 Chapter 5 Java Syntax and Style 103 Chapter 6 Data Types, Variables, and Arithmetic 121 Chapter 7 Boolean Expressions and if-else Statements 153 Chapter 8 Iterative Statements: while, for, do while 193 Chapter 9 Implementing Classes and Using Objects 211 Chapter 10 Strings 259 Chapter 11 Class Hierarchies and Interfaces 287 Chapter 12 Arrays and ArrayLists 321 Chapter 13 Searching and Sorting 363 Chapter 14 Streams and Files 393 Chapter 15 Graphics 409 Chapter 16 GUI Components and Events 433 Chapter 17 Mouse, Keyboard, Sounds, and Images 441 Chapter 18 Big-O Analysis of Algorithms 445 Chapter 19 The Java Collections Framework 465 Chapter 20 Lists and Iterators 507 Chapter 21 Stacks and Queues 527 Chapter 22 Recursion Revisited 547 Chapter 23 Binary Trees 575 Chapter 24 Lookup Tables and Hashing 603 Chapter 25 Heaps and Priority Queues 623 Chapter 26 Design Patterns 637 Appendices 645 Solutions to Selected Exercises 645 Index of Tables and Figures 647 Index 653 v

Start Chapter 1 Chapter 2 AB Chapter 3 Chapter 4 Chapter 18 Chapter 5 Chapter 6 Chapter 7 Chapter 8 A Chapter 9 Chapter 10 Chapter 11 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 19 Chapter 20 Chapter 21 Chapter 22 Chapter 23 Chapter 24 Chapter 25 Chapter 12 Chapter 26 Chapter 13 Finish

Contents Preface xvii How to Use This Book xxi Chapter 1. An Introduction to Hardware, Software, and the Internet 1 1.1 Prologue 2 1.2 Hardware Overview 1.2.1 The CPU 1.2.2 Memory 1.2.3 Secondary Storage Devices 1.2.4 Input and Output Devices 1.3 Software Overview 1.4 What Do Programmers Do? 1.5 Representation of Information in Computer Memory 1.5.1 Numbers 1.5.2 Characters 1.6 The Internet 1.7 Summary Exercises 5 Chapter 2. An Introduction to Software Development 11 2.1 Prologue 12 2.2 Compilers and Interpreters 14 2.3 Software Components and Packages 20 2.4 Lab: Three Ways to Say Hello 21 2.5 Object-Oriented Programming 27 2.6 Lab: More Ways to Say Hello 30 2.7 Summary 37 Exercises 38 vii

viii CONTENTS Chapter 3. Objects and Classes 43 3.1 Prologue 44 3.2 Objects in a Program: An Example 45 3.3 Classes 49 3.4 Fields, Constructors, and Methods 54 3.5 Case Study and Lab: First Steps 59 3.6 Inheritance 62 3.7 Summary 67 Exercises 69 Chapter 4. Algorithms 75 4.1 Prologue 76 4.2 Properties of Algorithms 77 4.3 Iterations 82 4.4 Recursion 83 4.5 Case Study: Euclid s GCF Algorithm 86 4.6 Working with Lists 88 4.7 Case Study: File Manager 91 4.8 Summary 94 Exercises 95 Chapter 5. Java Syntax and Style 103 5.1 Prologue 104 5.2 Using Comments 105 5.3 Reserved Words and Programmer-Defined Names 107 5.4 Syntax vs. Style 110 5.5 Statements, Blocks, Indentation 114 5.6 Lab: Correcting Syntax Errors 115 5.7 Summary 117 Exercises 118

CONTENTS ix Chapter 6. Data Types, Variables, and Arithmetic 121 6.1 Prologue 122 6.2 Declaring Fields and Local Variables 124 6.3 Primitive Data Types 128 6.4 Strings 129 6.5 Constants 130 6.6 Scope of Variables 133 6.7 Arithmetic Expressions 134 6.8 Compound Assignment and Increment Operators 138 6.9 Converting Numbers and Objects into Strings 140 6.10 Lab: Pie Chart 143 6.11 Summary 145 Exercises 146 Chapter 7. Boolean Expressions and if-else Statements 153 7.1 Prologue 154 7.2 if else Statements 156 7.3 boolean Data Type 157 7.4 Relational Operators 158 7.5 Logical Operators 160 7.6 Order of Operators 162 7.7 Short-Circuit Evaluation 163 7.8 if else if and Nested if else 164 7.9 Case Study and Lab: Rolling Dice 169 7.10 The switch statement 178 7.11 Enumerated Data Types 180 7.12 Case Study and Lab: Rolling Dice Concluded 183 7.13 Summary 186 Exercises 188 Chapter 8. Iterative Statements: while, for, do-while 193 8.1 Prologue 194 8.2 The while and for Loops 194 8.3 The do while Loop 198 8.4 return and break in Loops 199 8.5 Nested Loops 201 8.6 Lab: Perfect Numbers 204 8.7 Summary 205 Exercises 206

x CONTENTS Chapter 9. Implementing Classes and Using Objects 211 9.1 Prologue 212 9.2 Public and Private Features of a Class 216 9.3 Constructors 218 9.4 References to Objects 222 9.5 Defining Methods 223 9.6 Calling Methods and Accessing Fields 226 9.7 Passing Parameters to Constructors and Methods 229 9.8 return Statement 232 9.9 Case Study and Lab: Snack Bar 235 9.10 Overloaded Methods 240 9.11 Static Fields and Methods 243 9.12 Case Study and Lab: Snack Bar Concluded 248 9.13 Summary 249 Exercises 251 Chapter 10. Strings 259 10.1 Prologue 260 10.2 Literal Strings 260 10.3 String Constructors and Immutability 261 10.4 String Methods 264 10.5 Formatting Numbers into Strings 271 10.6 Extracting Numbers from Strings 274 10.7 Character Methods 275 10.8 Lab: Lipograms 276 10.9 The StringBuffer Class 278 10.10 Summary 280 Exercises 281 Chapter 11. Class Hierarchies and Interfaces 287 11.1 Prologue 288 11.2 Class Hierarchies and Polymorphism 289 11.3 Abstract Classes 294 11.4 Invoking Superclass s Constructors 298 11.5 Calling Superclass s Methods 300 11.6 Case Study: Dance Studio 302 11.7 Interfaces 306 11.8 Case Study: Dance Studio Concluded 310 11.9 Summary 314 Exercises 317

CONTENTS xi Chapter 12. Arrays and ArrayLists 321 12.1 Prologue 322 12.2 One-Dimensional Arrays 323 12.3 Lab: Fortune Teller 327 12.4 The ArrayList Class 327 12.5 ArrayList s Constructors and Methods 331 12.6 ArrayList s Pitfalls 333 12.7 Iterations and the For Each Loop 336 12.8 Inserting and Removing Elements 339 12.9 Lab: Creating an Index for a Document 340 12.10 Two-Dimensional Arrays 344 12.11 Case Study and Lab: Chomp 346 12.12 Summary 352 Exercises 354 Chapter 13. Searching and Sorting 363 13.1 Prologue 364 13.2 equals, compareto, and compare 365 13.3 Sequential and Binary Search 371 13.4 Lab: Keeping Things in Order 375 13.5 Selection Sort 376 13.6 Insertion Sort 377 13.7 Mergesort 379 13.8 Quicksort 382 13.9 Lab: Benchmarks 384 13.10 java.util.arrays and java.util.collections 386 13.11 Summary 388 Exercises 390 Chapter 14. Streams and Files 393 14.1 Prologue 394 14.2 Pathnames and the java.io.file Class 396 14.3 Reading from a Text File 398 14.4 Writing to a Text File 401 14.5 Lab: Choosing Words 403 14.6 Summary 404 Exercises 405

xii CONTENTS Chapter 15. Graphics 409 15.1 Prologue 410 15.2 paint, paintcomponent, and repaint 412 15.3 Coordinates 415 15.4 Colors 418 15.5 Drawing Shapes 419 15.6 Fonts and Text 421 15.7 Case Study and Lab: Pieces of the Puzzle 422 15.8 Summary 429 Exercises 430 Chapter 16. GUI Components and Events 433 16.1 Prologue 434 16.2 Pluggable Look and Feel 16.3 Basic Swing Components and Their Events 16.4 Layouts 16.5 Menus 16.6 Case Study and Lab: the Ramblecs Game 16.7 Summary Exercises 436 Chapter 17. Mouse, Keyboard, Sounds, and Images 441 17.1 Prologue 442 17.2 Mouse Events Handling 17.3 Keyboard Events Handling 17.4 Case Study and Lab: Drawing Editor 17.5 Sounds and Images 17.6 Case Study and Lab: Ramblecs Concluded 17.7 Summary Exercises 443 Chapter 18. Big-O Analysis of Algorithms 445 18.1 Prologue 446 18.2 The Big-O Concept 447 18.3 Big-O of Sorting Algorithms 455 18.4 Summary 459 Exercises 461

CONTENTS xiii Chapter 19. The Java Collections Framework 465 19.1 Prologue 466 19.2 Collection and Iterator 468 19.3 Lists and ListIterator 472 19.4 The Stack Class 478 19.5 The Queue Interface 480 19.6 The PriorityQueue Class 482 19.7 Sets 484 19.8 Maps 487 19.9 Case Study and Lab: Stock Exchange 492 19.10 Summary 499 Exercises 499 Chapter 20. Lists and Iterators 507 20.1 Prologue 508 20.2 Singly-Linked List 508 20.3 Traversals 514 20.4 Lab: Implementing a Singly-Linked List 516 20.5 Linked List with a Tail 517 20.6 Doubly-Linked List and Circular List 518 20.7 Lab: Teletext 521 20.8 Summary 522 Exercises 523 Chapter 21. Stacks and Queues 527 21.1 Prologue 528 21.2 Implementations of Stacks 528 21.3 Lab: Browsing 531 21.4 The Hardware Stack 533 21.5 Implementations of Queues 535 21.6 Case Study and Lab: Actors World 536 21.7 Summary 541 Exercises 542

xiv CONTENTS Chapter 22. Recursion Revisited 547 22.1 Prologue 548 22.2 Three Examples 548 22.3 When Not to Use Recursion 556 22.4 Understanding and Debugging Recursive Methods 559 22.5 Lab: the Tower of Hanoi 562 22.6 Case Study and Lab: the Game of Hex 563 22.7 Summary 568 Exercises 568 Chapter 23. Binary Trees 575 23.1 Prologue 576 23.2 Implementations of Binary Trees 581 23.3 Traversals 583 23.4 Binary Search Trees 584 23.5 A Do-It-Yourself BST 586 23.6 Lab: Morse Code 591 23.7 Case Study and Lab: Java Messenger 592 23.8 Summary 594 Exercises 595 Chapter 24. Lookup Tables and Hashing 603 24.1 Prologue 604 24.2 Lookup Tables 604 24.3 Lab: Cryptogram Solver 607 24.4 Hash Tables 610 24.5 java.util s HashSet and HashMap 613 24.6 Lab: Search Engine 615 24.7 Summary 617 Exercises 618 Chapter 25. Heaps and Priority Queues 623 25.1 Prologue 624 25.2 Implementations of Priority Queues 624 25.3 Binary Trees: Non-Linked Representation 626 25.4 A Do-It-Yourself Priority Queue 628 25.5 Lab: Heapsort 632 25.6 Summary 633 Exercises 634

CONTENTS xv Chapter 26. Design Patterns 637 26.1 Prologue 638 26.2 Façade 26.3 Strategy 26.4 Singleton 26.5 Decorator 26.6 Composite 26.7 MVC (Model-View-Controller) 26.8 Summary Exercises 640 Appendices 645 A. The 17 Bits of Style B. Common Syntax Error Messages C. HTML Tutorial D. GUI Examples Index E. The EasyReader, EasyWriter, EasySound, EasyDate, and EasyFormat Classes F. Computing in Context: Responsible and Ethical Computer Use Solutions to Selected Exercises 645 Index of Tables and Figures 647 Index 653