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

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

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.

Database Concepts. David M. Kroenke UNIVERSITATSBIBLIOTHEK HANNOVER

Data Structures and Abstractions with Java

THE AVR MICROCONTROLLER AND EMBEDDED SYSTEMS. Using Assembly and С

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

Prelude to Programming

Programming. In Ada JOHN BARNES TT ADDISON-WESLEY

FUNDAMENTALS OF. Database S wctpmc. Shamkant B. Navathe College of Computing Georgia Institute of Technology. Addison-Wesley

Objects First with Java

Essentials of Database Management

Anany Levitin 3RD EDITION. Arup Kumar Bhattacharjee. mmmmm Analysis of Algorithms. Soumen Mukherjee. Introduction to TllG DCSISFI &

Business Driven Data Communications

FUNDAMENTALS OF SEVENTH EDITION

CJT^jL rafting Cm ompiler

Data Structures and Abstractions with Java

CLASSIC DATA STRUCTURES IN JAVA

Integrated Approach. Operating Systems COMPUTER SYSTEMS. LEAHY, Jr. Georgia Institute of Technology. Umakishore RAMACHANDRAN. William D.

Search Engines Information Retrieval in Practice

DATA ABSTRACTION AND PROBLEM SOLVING WITH JAVA

MACHINES AND MECHANISMS

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

DATA AND COMPUTER COMMUNICATIONS

Real-Time Systems and Programming Languages

This page intentionally left blank

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

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

HCS12 Microcontroller and Embedded Systems: Using Assembly and C with CodeWarrior 1 st Edition

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

Programming in Python 3

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

Fundamentals of. Database Systems. Shamkant B. Navathe. College of Computing Georgia Institute of Technology PEARSON.

The Unified Modeling Language User Guide

CRYPTOGRAPHY AND NETWORK SECURITY

JAVASCRIPT FOR PROGRAMMERS

Networking Security Essentials 4th Edition Solution Manual

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

AND ASSURANCE AN INTEGRATED APPROACH SIXTEENTH EDITION GLOBAL EDITION

An Introduction to Search Engines and Web Navigation

Application Programming

Web Development and Design Foundations with HTML5

DATA STRUCTURES AND PROBLEM SOLVING USING JAVA

ECLIPSE MODELING PROJECT

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

Programming Guide. Aaftab Munshi Dan Ginsburg Dave Shreiner. TT r^addison-wesley

Workbook for C++ THIRD EDITION. Yedidyah Langsam, Ph.D. Brooklyn College of the City University of New York. Custom Publishing

Cloud Computing and SOA Convergence in Your Enterprise

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

Fit for Developing Software

Harvey Deitel. Abbey Deitel

DB2 SQL Tuning Tips for z/os Developers

Essentials. Oracle Solaris Cluster. Tim Read. Upper Saddle River, NJ Boston Indianapolis San Francisco. Capetown Sydney Tokyo Singapore Mexico City

MODERN DATABASE MANAGEMENT

Big Java Late Objects

Software Engineering Ian Sommerville Pearson Education File Type

Introduction to Computer Graphics

Preface A Brief History Pilot Test Results

Modern Information Retrieval

Starting Out With C From Control Structures To Objects Plus Myprogramminglab With Pearson Etext Access Card Package 8th Edition

Domain-Specific. Languages. Martin Fowler. AAddison-Wesley. Sydney Tokyo. With Rebecca Parsons

DATABASE SYSTEM CONCEPTS

Simulation Modeling and Analysis

AssEivibly LANquAqE for

Core Java Volume Ii Advanced Features 10th Edition

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

FrontPage 98: The Complete Reference

ECLIPSE RICH CLIENT PLATFORM

Refactoring HTML. Improving the Design of Existing Web Applications. Elliotte Rusty Harold. TT rvaddison-wesley

IPHONE FOR PROGRAMMERS: AN APP-DRIVEN APPROACH

OpenGL SUPERBIBLE. Fifth Edition. Comprehensive Tutorial and Reference. Richard S. Wright, Jr. Nicholas Haemel Graham Sellers Benjamin Lipchak

JAVA CONCEPTS Early Objects

Digital System Design with SystemVerilog

LATEX. Leslie Lamport. Digital Equipment Corporation. Illustrations by Duane Bibby. v ADDISON-WESLEY

Programming. Principles and Practice Using C++ Bjarne Stroustrup. / Addison-Wesley. Second Edition

Oracle Certified Associate Java Se 7 Programmer Study Guide

CS 1316 Exam 2 Summer 2009

\ Smart Client 0" Deploymentwith v^ ClickOnce

Problem Solving with Data Structures: A Multimedia Approach

Opengl Programming On Mac Os X Architecture Performance

An Introduction to Object-Oriented Programming

Fundarnentals of. Sharnkant B. Navathe College of Computing Georgia Institute of Technology

Virtualization from the Trenches

Programming HD DVD and Blu-ray Diso The HD Cookbook

Software Engineering Ian Sommerville 7th Edition

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Network Programming With Go Essential Skills For Using And Securing Networks

Elements Of Programming Interviews In Python The Insiders Guide

Programming Wireless Devices with the Java 2 Platform, Micro Edition

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

Framework Design Guidelines

Secure Coding in C and C++

Beginning Programming (Two Semesters) Semester One. Module One: Intro to Beginning Programming. Module Two: Computer Careers

NETWORKING KEITH W. ROSS. Polytechnic Institute of NYU. Addison-Wesley

CCNA Cisco Certified Network Associate Study Guide

Microsoft Visual Studio 2010

Homeschool Programming, Inc.

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

A Document Preparation System. User's Guide and Reference Manual. Leslie Lamport

Developer's HTML5. Cookbook. AAddison-Wesley. Chuck Hudson. Tom Leadbetter. Upper Saddle River, NJ Boston Indianapolis San Francisco

Transcription:

PROBLEM SOLVING WITH DATA STRUCTURES USING JAVA A Multimedia Approach Mark Guzdial and Barbara Ericson College of Computing Georgia Institute of Technology PEARSON Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo

Contents Preface 13 About the Authors 19 1 INTRODUCTION TO JAVA: OBJECT-ORIENTED PROGRAMMING FOR MODELING A WORLD 23 1 Objects for Modeling a World 25 1.1 Making Representations of a World 27 1.2 Why Java? 33 Getting Java Set Up 33 2 Introduction to Java 37 2.1 What's Java About? 37 2.2 Basic (Syntax) Rules of Java 39 Declarations and "types 39 Strings 41 ObjectVariables and Primitive Variables 42 Assignment 44 Semicolons 44 Conditionals 44 Arrays 46 Iteration 47 Strings versus Arrays and Substrings 48 2.3 Using Java to Model the World 48 Discourse Rules for Java 53 Defining tostring 53 Defining Student as Subclass of Person 54 Creating a main() Method 57 Exploring Inheritance 58 2.4 Manipulating Pictures in Java 61 2.5 Exploring 2.6 Exploring Sound in Java 67 Music in Java 68

Methods in Java: Manipulating Pictures 73 3.1 Reviewing Java Basics 73 Assignment 73 Arrays 75 Conditionals 75 Iteration: For each, While, and For 76 3.2 Changing the Picture Class 77 Pictures Are About Arrays and Pixels 79 A Method for Decreasing Red 79 Method with an Input 82 3.3 Methods That Return Something: Compositing Images 84 Composing by Chromakey 90 3.4 Creating Classes That Do Something 93 Objects as Agents: Manipulating Turtles 97 4.1 Turtles: An Early Computational Object 97 4.2 Drawing with Turtles 98 When Cascades Don't Work 100 Making Lots of Turtles 101 Composing Pictures with Turtles 103 4.3 Creating Animations with Turtles and Frames 105 The Data Structure within FrameSequencer 108 4.4 Making a Slow Moving Turtle with sleep and Exceptions 110 Arrays: A Static Data Structure for Sounds 115 5.1 Manipulating Sampled Sounds 115 5.2 Inserting and Deleting in an Array 120 5.3 How Slow Does It Get? 124 INTRODUCING LINKED LISTS 127 Structuring Music Using Linked Lists 129 6.1 JMusic and Imports 129 6.2 Making a Simple Song Object 134 6.3 Making a Song Something to Explore as a Linked List 135 Walking through SongNode 143 Improving insertafter 147

The Contents 9 Helping Others Use Our Classes 148 Computing Phrases 149 Repeating and Weaving to Make Music 151 Linked Lists versus Arrays 154 Creating a Music Tree 156 7 Structuring Images Using Linked Lists 163 7.1 Simple Arrays of Pictures 164 7.2 Listing the Pictures, Left-to-Right 165 Generalizing Moving the Element 168 7.3 Listing the Pictures, Layering 171 Reordering Elements in a List 176 7.4 Reversing a List 178 7.5 Animation 179 7.6 Lists with Two Kinds of Elements 181 Structuring Our Multi-Element Lists 191 3 TREES: HIERARCHICAL STRUCTURES FORMEDIA 199 8 Trees of Images 201 8.1 Representing Scenes with Trees 201 8.2 OurFirstSceneGraph:AttackoftheKillerWolvies 204 8.3 Classes in the Scene Graph 206 8.4 Building a Scene Graph 208 8.5 Implementing the Scene Graph 215 Implementing the Abstract Superclass for the Scene Graph: DrawableNode 217 Implementing the Leaf Nodes: PictNode and BlueScreenNode 219 Implementing the Branches: Branch, MoveBranch, VBranch, andhbranch 221 The Line between Structure and Behavior 227 9 Lists and Trees for Structuring Sounds 231 9.1 Composing with Sampled Sounds and Linked Lists: Recursive Traversals 231 Tracing a Recursive Traversal 237 Testing and Replacing 239

10 Contents 9.2 Using Trees to Structure Sampled Sounds 245 Implementing a Sound Tree 249 Tracing a Recursive Tree Traversal 255 Where We're Going Next 261 10 Generalizing Lists and Trees 264 10.1 Refactoring a General Linked List Node Class 264 Making Wol fattackmovi e Work Again 267 Making Sound Trees Work Again 270 10.2 Making a New Kind of List 273 10.3 The Uses and Characteristics of Arrays, Lists, and Trees 275 Examples of Tree Uses 277 10.4 Binary Search Trees: Trees That Are Fast to Search 280 Traversals of Trees 287 Trees Can Do Anything 289 11 Abstract Data Types: Separating the Meaning from the Implementation 293 11.1 Introducing Stacks 293 Defining an Abstract Data Type 294 A Stack Interface 296 Multiple Implementations of a Stack 297 Uses of a Stack 303 11.2 Introducing Queues 305 Implementing a Queue 307 Creating an Abstract Queue Class 312 11.3 Using an ArrayList 315 11.4 Using a Map ADT 317 12 Circular Linked Lists and Graphs: Lists and Trees That Loop 322 12.1 Making Sprite Animation with Circular Linked Lists 322 12.2 Generalizing a Circular Linked List 327 12.3 Graphs: Trees with Loops 329 13 User Interface Structures 337 13.1 A Toolkit for Building User Interfaces 337 Building the Simplest Possible Swing User Interface 338

Contents 11 13.2 Building and Rendering User Interfaces 340 Building a Simple User Interface 340 Java Swing Layout Managers: GUI Tree Renderers 344 13.3 Creating an Interactive User Interface 347 Making Our GUItree Interactive 349 Creating a Picture Tool 351 13.4 Running from the Command Line 356 4 SIMULATIONS: PROBLEM SOLVING WITH DATA STRUCTURES 361 14 Using an Existing Simulation Package 363 14.1 Introducing Simulations 363 14.2 Overview of Greenfoot 365 14.3 Greenfoot Basics 370 14.4 Creating New Classes 372 14.5 Breakout 377 15 Introducing UML and Continuous Simulations 387 15.1 Our First Model and Simulation: Wolves and Deer 387 Modeling the Wolves and Deer 389 15.2 Modeling in Objects 390 15.3 Implementing the Simulation Class 394 15.4 Implementing 15.5 Implementing a Wolf 397 Deer 402 15.6 Implementing AgentNode 403 15.7 Extending the Simulation 404 Making Hungry Wolves 404 Writing Results to a File 407 Getting Results from a Simulation 411 16 Abstracting Simulations: Creating a Simulation Package 413 16.1 Creating a Generalized Simulation Package 413 Real Programmers Rarely Build Data Structures 414

Real Programmers Make Models and Choices 415 The Structure of the Simulation Package 416 Using a Li nkedli st from the Java Collection Classes 419 16.2 Re-Making the Wolves and Deer with Our Simulation Package 420 16.3 Making a Disease Propagation Simulation 428 Exploring Scenarios in Disease Propagation 432 16.4 Walking through the Simulation Package 434 16.5 Finally! Making Wildebeests and Villagers 439 Going Beyond the Wildebeests 447 Discrete Event Simulation 456 17.1 Describing a Marketplace 456 17.2 Differences between Continuous and Discrete Event Simulations Introducing Resources 458 17.3 Different Kinds of Random 458 Random Distributions in the World 459 Generating Different Random Distributions 460 Generating Useful Random Distributions 466 17.4 Ordering Events by Time 469 Sorting Objects 473 Inserting into a Sorted List 474 Using a Min-Heap 475 17.5 Implementing a Discrete Event Simulation 477 Building a Discrete Event Simulation 480 17.6 The Final Word: The Thin Line between Structure and Behavior 493 Bibliography 497 Index 499